Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
This course is designed to quickly lead you through the steps of building an HTML website, from creating a new page to building links and tables. Author James Williamson simplifies the coding process, with straightforward steps you can recreate on your own. The course explains the basic structure of an HTML document, shows how to add text and images, and introduces font styling with CSS. James also offers a bonus design challenge at the end of each chapter, where he asks you to think of a solution before offering his own.
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.
There are currently no FAQs about Up and Running with HTML.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.