Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Understanding CSS placement

From: HTML Essential Training (2012)

Video: Understanding CSS placement

CSS styles can be applied to an HTML document in several different ways.

Understanding CSS placement

CSS styles can be applied to an HTML document in several different ways. Let's make a working copy of css.html. And I'll go ahead and rename this as css-working.html. Open that in my text editor, and you can see, this is a very simple html document. It's got a H1 element and a few paragraphs. And the paragraphs are just full of lorem ipsums, and so you can put whatever you like in them. Let's go ahead and open this in the browser and that's how it displays.

Now you'll notice that here, at the top of the document I'm actually linking in a style sheet. So let's go ahead and open that as well. That's main.css, it's a very simple style sheet with just a few styles in it. This is what I call a reset. It's basically giving me a baseline to start styling a document from, and if we come back over here in our editor, and we just comment out this style sheet. So that it's not loaded, I come back into the browser and hit reload you'll see this is what the document looks like without the style sheet.

And if I put the style sheet back in, save, and reload this is what the document looks like with the style sheet. So it's basically applying some simple styles, to this document. It's setting some fonts, it's setting some line heights and such. And you can see the line height is not actually very attractive. It's just there as a reset, as a consistent starting place to format the document from. So, using the link tag, you may apply a style sheet stored in a separate file. This is sometimes called an external style sheet, and these styles are applied to the entire document.

You can also apply style by typing it right into the HTML file, using the style tag. And so I can correct that line height like this. I can say I want it to be 140% of the size of the font. And now we come back into the browser and reload. You see that the line height has changed, for all the P elements, that's the paragraphs in the document. The style element goes in the head section and these styles are also applied to the entire document.

It's also possible to apply styles to individual elements, and you do this with the style attributes. So if I come in here and I say style equals say, font family, monospace, I save that, and come over to the browser and reload. Now this first paragraph, the entire paragraph, is in a monospace font. You may have different fonts on your computer, so this font may come up using a different font, but we'll use a monospace font and it ought to be monospaced. So the style attribute allows you to apply styles to individual elements in your document.

There are also a couple of elements that are designed specifically to apply style to sections of a document. Come up here above this H1 element, and I'll put in a div tag, and I'm going to close that div tag after this P. So, this div is a block level element and it contains now both this H1 and P elements. And so if I apply style to this div element like this. Sets color: #933; if I apply that style there, it's going to give us a reddish color, and if I reload over here, you see these two elements now have this reddish color.

So I've applied style to two different elements, all with one style using this div tag. So div is block level, there's also an inline level equivalent called span. And I'm just going to take a little bit of a sentence here out of this second paragraph and I'm going to put a span around it. So now, my style will apply just to those four latin words. I say style equals and color, I'm going to do the same red. When I come over here and reload, we see there's these four words, if I close up they'll all go up on one line there, there they are.

And those words are now in that same reddish color. So using div for blocks and span for inline level, you can apply style to sections of a document. So let's go ahead and remove these and we'll talk about, how styles are applied on top of each other. Elements tend to inherit properties from their parents. So if I specify a font family in a paragraph, any tags inside that paragraph will inherit that font family unless I specify a font for that text.

For example, if I come in here and I put in an A element here, and I'll just close that, end of this sentence. And let's say I do the same thing in the next paragraph. So the default formatting of A, or whatever I have in my global level style sheets, will apply. Now I'll go ahead and I'll reload this, and you see that these elements inherit the font from the paragraph, because I've not specified a font for the A element. So the one up here in the monospace paragraph gets a monospace font and the one down here in the serif font, gets the same serif font.

Now, if I come back up here into our global style sheet up here and I say A, and I apply a font. I'll say font family, sans-serif Now both of these will be in the same font. And when I reload here you see they are both in this same san sans-serif font. Because I've actually specified a font for that element. So if I don't specify one and just inherits whatever is there and that's actually a value called inherent. And if I do specify one, then even though I've specified later a font for this P element, it's not overriding that because that's got its own font defined.

The other thing you need to realize is that styles defined in a later context, take precedence over those defined in an earlier context. For example, I have this style sheet here that defines a line height for paragraphs of one times the size of a font. And here in the document I've specified a line height of 140%. Because this one is defined after this one, the later one takes precedence. If I take this link element and I move it to after the style element and I reload, now my line height is narrow again.

Because the style defined in later context is overriding the style defined in an earlier context. So there are several ways to apply CSS styles and there are rules as to how they cascade. Use consistent practices in your documents and this will make it easier for you to maintain your web pages later on.

Show transcript

This video is part of

Image for HTML Essential Training (2012)
HTML Essential Training (2012)

82 video lessons · 105076 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 5m 24s
    1. Welcome
      56s
    2. Using the exercise files
      1m 37s
    3. What you need to know about this course
      2m 51s
  2. 22m 0s
    1. What is HTML?
      4m 12s
    2. Examining the structure of an HTML document
      7m 50s
    3. Understanding tags and containers
      6m 4s
    4. Exploring content models in HTML5
      2m 23s
    5. Looking at obsolete elements
      1m 31s
  3. 27m 19s
    1. Understanding whitespace and comments
      3m 53s
    2. Displaying text with paragraphs
      3m 37s
    3. Applying style
      8m 5s
    4. Using block and inline tags
      6m 34s
    5. Displaying characters with references
      5m 10s
  4. 16m 36s
    1. Exploring the front matter of HTML
      2m 9s
    2. Applying CSS to your document
      3m 59s
    3. Adding scripting elements
      4m 54s
    4. Using the meta tag
      3m 34s
    5. Optimizing your page for search engines
      2m 0s
  5. 24m 59s
    1. Controlling line breaks and spaces
      2m 46s
    2. Exploring phrase elements
      1m 44s
    3. Using font markup elements
      1m 5s
    4. Highlighting text with mark
      1m 29s
    5. Adding headings
      1m 38s
    6. Using quotations and quote marks
      3m 2s
    7. Exploring preformatted text
      1m 45s
    8. Formatting lists
      2m 28s
    9. Forcing text direction
      3m 49s
    10. Suggesting word-break opportunities
      2m 29s
    11. Annotating East Asian languages
      2m 44s
  6. 29m 15s
    1. Introducing CSS
      55s
    2. Understanding CSS placement
      6m 55s
    3. Exploring CSS syntax
      10m 34s
    4. Understanding CSS units of measure
      3m 3s
    5. Some CSS examples
      7m 48s
  7. 22m 5s
    1. Using images
      4m 13s
    2. Flowing text around an image
      4m 55s
    3. Breaking lines around an image
      3m 3s
    4. Aligning images
      5m 25s
    5. Mapping links in an image
      4m 29s
  8. 22m 28s
    1. Understanding URLs
      2m 41s
    2. Working with hyperlinks
      3m 28s
    3. Using relative URLs
      4m 20s
    4. Specifying a base URL
      2m 19s
    5. Linking within a page
      4m 12s
    6. Using image links
      5m 28s
  9. 17m 2s
    1. Exploring list types
      3m 52s
    2. List elements in depth
      7m 44s
    3. Using text menus with unordered lists
      5m 26s
  10. 15m 30s
    1. Introduction to HTML semantics
      4m 9s
    2. Exploring an example
      4m 56s
    3. Marking up figures and illustrations
      2m 33s
    4. Creating collapsible details
      3m 52s
  11. 11m 18s
    1. Embedding audio
      5m 19s
    2. Embedding video
      5m 59s
  12. 11m 53s
    1. Creating ad-hoc Document Object Model (DOM) data with the data-* attribute
      4m 53s
    2. Displaying relative values with meter
      2m 57s
    3. Creating dynamic progress indicators
      4m 3s
  13. 4m 49s
    1. Overview of HTML5 microdata
      1m 8s
    2. Exploring an example with microdata
      3m 41s
  14. 7m 3s
    1. Understanding outlines
      52s
    2. A demonstration of outlining
      6m 11s
  15. 13m 1s
    1. Table basics
      7m 29s
    2. Exploring the semantic parts of a table
      2m 32s
    3. Grouping columns
      3m 0s
  16. 9m 55s
    1. Frames overview
      54s
    2. Using traditional frames
      4m 26s
    3. Exploring inline frames using iframe
      2m 7s
    4. Simulating frames with CSS
      2m 28s
  17. 53m 7s
    1. Introducing forms
      10m 24s
    2. Using text elements
      10m 12s
    3. Using checkboxes and radio buttons
      2m 37s
    4. Creating selection lists and dropdown lists
      5m 14s
    5. Submit and button elements
      8m 48s
    6. Using an image as a submit button
      2m 15s
    7. Keeping context with the hidden element
      3m 0s
    8. Setting tab order
      2m 7s
    9. Preloading an autocomplete list using the datalist feature
      5m 26s
    10. Displaying results with output
      3m 4s
  18. 19m 47s
    1. Touring a complete site
      2m 14s
    2. Touring the HTML
      8m 44s
    3. Touring the CSS
      8m 49s
  19. 29s
    1. Goodbye
      29s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML Essential Training (2012).

Return to your organization's learning portal to continue training, or close this page.


OK

Course retiring soon

HTML Essential Training (2012) will be retired from the lynda.com library on January 2, 2015. Training videos and exercise files will no longer be available, but the course will still appear in your course history and certificates of completion. For updated training, check out the all new HTML Essential Training in the lynda.com Online Training Library.


Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.