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

Exploring the style element

From: Up and Running with HTML

Video: Exploring the style element

In our last movie we discussed why it was such a bad idea to style HTML elements directly using inline styles. In this exercise, I want to talk about why it's much more efficient to write styles so that they can affect either an entire page or even an entire site all at once. Now, to introduce that concept, we're going to take a look at using the style element to move our styles into the head of our document. So here I'm using the style.htm file from the 06_03 directory.

Exploring the style element

In our last movie we discussed why it was such a bad idea to style HTML elements directly using inline styles. In this exercise, I want to talk about why it's much more efficient to write styles so that they can affect either an entire page or even an entire site all at once. Now, to introduce that concept, we're going to take a look at using the style element to move our styles into the head of our document. So here I'm using the style.htm file from the 06_03 directory.

What I want to do is I want to go up to the head of my document and just underneath the title there, I'm going to go ahead and open up a style tag. So the style element is always found, if you're to use it, up in the head of the document, and just inside of it is where we're going to put all of our CSS. So essentially what this allows you to do, if you will, is to interrupt the HTML for just a moment, write all the CSS you need to write for that page, and then go back to writing your HTML. Within this tag I can throw all of the styling I want. Any styles that you place here are going to apply to the entire page.

What we want to do is we want to move the styling from here up to here, but one of things we have to do is remember, inline styles don't require the use of a selector. They don't need the target element on the page because, well, they're applied to an element on the page. So what we need to do is we need to first write a selector to do this. Now, selector syntax is fairly simple. You just type in what you want to affect. In this case we want to style an h2 or all h2s, so I'll just type in h2.

To be clear, there is no way within the course of a single chapter like this I can teach you everything that you need to know about writing selectors in CSS. That's really not the purpose of this chapter. Just know this: if you want to write a selector that's going to target all of those types of elements on the page, all you really have to do is just type in the character for that particular tag. You don't need the opening and closing brackets; you just need the tag itself--in this h2. CSS syntax; right after the selector we have a curly brace, and those curly braces can be found right beside the P key.

You want to have an opening curly brace and a closing curly brace. Most people do what I just did, which was you put the opening curly brace on one line, leave some empty space below it, and put the closing curly brace on another. So basically, this is saying that all the properties inside these curly braces apply to the heading 2. And so now what I'm going to do is I'm going to go down here to these properties and I'm just going to cut them and then I'm just going to paste them directly inside those curly braces. And again, just to make things a little more readable, I'm going to paste them inside of it.

And I don't know if I like that spacing. There we go; that's better. And when I do that I'm going to get rid of the style attribute. There we go. Save that. So now we've taken the styling, we've moved it from this h2, and we've moved it up into this selector. Here's the selector. It says target all the h2s on the page. The curly braces are going to surround all the properties for that rule, and then we have individual properties and then the values that we're setting for those properties. So if I save this and preview this in a browser, you can see that nothing really changed. It's still red.

It's still a heading. So our styling is still being applied, but what makes this a lot more powerful than using inline styles is if you had another heading 2 on the page, for example, maybe have one here that says, "Global styles apply to all instances of the element," and then close the h2 tag. If I save this and refresh my browser, you can see that it goes ahead and gets the same exact styling because it's an h2 element. So this is saying, hey, any h2 element on the page, I want to go ahead and change that.

Selectors are extremely powerful, but there are obviously going to be times when you want to be a little bit more targeted. Now again, we're not going to get too deep into selectors in this chapter, but I just wanted to show you that you certainly have a lot more filtering mechanisms available to you in these selectors. I could come down to this h2 and I can say class ="style." And then all I have to do is come up to this h2, and class selectors are preceded by a period when you write them in CSS, so I could say h2.style.

So what is this saying? This is saying find an h2 that has the class style applied to it. So now if I save that, go into the browser, and refresh, you can see that now it's only applying to the first one because it's being filtered now by that. So selectors are very flexible. And as you learn CSS, that's one of the most important parts for you to focus is on learning how to write those selectors. For what we did in this particular exercise, we moved the styles so that they're applying to the entire page all at once.

That's pretty powerful. Now, a little bit later on, however, we're going to go even further than that. We're going to explore how you can control an entire site with only one set of styles. Now, before we do that, I'm going to go over some of the basic styling properties that we can control, and we're going to start in our next exercise by exploring basic font styling.

Show transcript

This video is part of

Image for Up and Running with HTML
Up and Running with HTML

49 video lessons · 26734 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 2m 12s
    1. Welcome
      55s
    2. Using the exercise files
      1m 17s
  2. 29m 30s
    1. Learning HTML
      2m 47s
    2. Choosing a code editor
      5m 2s
    3. Exploring basic HTML syntax
      8m 18s
    4. Do I need to learn HTML5?
      5m 6s
    5. Exploring HTML references
      8m 17s
  3. 35m 40s
    1. Exploring an HTML document
      5m 19s
    2. Working with doctype declarations
      4m 3s
    3. Examining the document head
      8m 20s
    4. Looking at the document body
      3m 21s
    5. Adding document structure
      8m 52s
    6. Lab: Coding a basic page
      3m 9s
    7. Solution: Coding a basic page
      2m 36s
  4. 1h 23m
    1. How does HTML format text?
      5m 51s
    2. Adding headings
      7m 24s
    3. Formatting paragraphs
      4m 54s
    4. Controlling line breaks
      3m 50s
    5. Creating lists
      10m 37s
    6. Emphasizing text
      6m 42s
    7. Displaying special characters
      5m 8s
    8. Controlling whitespace
      4m 35s
    9. Inserting images
      9m 20s
    10. Lab: Controlling page content
      13m 57s
    11. Solution: Controlling page content
      10m 55s
  5. 31m 54s
    1. Linking to pages within your site
      6m 45s
    2. Linking to external pages
      3m 2s
    3. Linking to downloadable resources
      2m 25s
    4. Linking to page regions
      8m 0s
    5. Lab: Creating Links
      5m 57s
    6. Solution: Creating Links
      5m 45s
  6. 40m 27s
    1. Examining basic table structure
      5m 10s
    2. Adding content to tables
      6m 20s
    3. Setting table attributes
      7m 42s
    4. Adding table captions
      4m 3s
    5. Defining table headers
      2m 13s
    6. Making table data accessible
      5m 46s
    7. Lab: Building tables
      4m 13s
    8. Solution: Building tables
      5m 0s
  7. 43m 23s
    1. Understanding the relationship between HTML and CSS
      4m 58s
    2. Creating inline styles
      4m 53s
    3. Exploring the style element
      5m 13s
    4. Basic font styling
      9m 24s
    5. Changing color
      4m 55s
    6. Taking styles further
      5m 24s
    7. Lab: Controlling basic styles
      5m 10s
    8. Solution: Controlling basic styles
      3m 26s
  8. 5m 44s
    1. Next steps
      2m 56s
    2. Additional resources
      2m 48s

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.

Join now "Already a member? Log in

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 Up and Running with HTML.

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


OK
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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.