Viewers: in countries Watching now:
CSS: Page Layouts introduces basic layout concepts, gives advice on how to create properly structured HTML based on prototypes and mockups, and goes into critical page layout skills such as floats and positioning. Author James Williamson shows how to combine these techniques to create fixed, fluid, and responsive layouts. Designers are also shown how to enhance their pages through the creative use of CSS techniques like multi-column text, opacity, and the background property. Exercise files are included with this course.
The display property is by far one of my favorite CSS properties. Now when I first learned about it, I remember thinking to myself, "There is no way you can really do that, can you?" So what does it allow you to do? Well, the display property allows you to change how an element displays within the browsers. Block-level elements can become inline. A div tag can display as a table cell, or you can even tell elements to not display at all. As I'm sure you can imagine, controlling how an element displays within the browser is one of the most powerful CSS capabilities a designer has when creating page layouts. It's also surprisingly simple to do.
So what I have right here is I've got the page that we are going to be working on, I've got it previewed within one of my browsers. And I did that because I wanted to show you what the possible values for the display property are, and talk about them a little bit. So, this little group of values right here are the options that we have when setting the display property. We have inline, block, list-item, inline-block, and then a whole host of table- related properties as well. Then we have none, and we have inherit, which basically just says, hey, whatever the display property of my parent element is, that's what I want as well.
Okay, so if you want something to display as an inline-level element, such it's an image or a span tag or you know just an element within a line of text, you would use inline. You want it to display as a block-level element, like a div tag or paragraph or heading, then you'll use block. List item would display as a single item within a list. Inline-block is really interesting. We are going to take a look at that in just a moment. What that allows you to do is to display elements as inline elements but still be able to control their box model properties as if they were block-level elements, so that's a very interesting one. These table ones, this is really sort of outside the scope of this course, but what they allow you to do is to take an element and essentially display it as if it were a table cell or a table row.
You can even create CSS table-based layouts by using that, and that's something that we are going to be doing in an upcoming title. So with all of that sort of in mind, we've got some elements down here, this list, we've got some images, we have some headings down here that we are going to control. So we are going to go ahead and experiment with a lot of these different properties. So I am going to jump into my code. You can find that in the file display.htm, which is found in the 01_07 directory in your exercise files. Okay, so I am going to scroll down into my code and you'll see at about line 61 or so we have little area in our styles where we can begin to add some styles.
Now to just again, talk about the structure of this a little bit, we have a list here that we are going to control, we have these images right here that we are going to control just underneath this headline, and then we have some headings down here that we are going to change to the display on as well. Notice that they all have the inlineB class applied to them as well. And then our final headline has a class of none applied to it. Okay, so just in that layer where we are going to add some styles, the first thing we'll do is we are going to change how these list items are going to display. So I am going to create an li selector and we're going to use the display property, and I am just going to tell it to display as an inline element.
Now list items, those individual elements within a list, by default they display as block-level items, meaning they stack one on top of another. So if I save my file, go back into my browser, and refresh it, you can see that the list now displays as inline elements. They are all on the same line. So they are indeed individual elements, but the bullets go away and they all display as inline elements. A lot of people use this technique to create menus out of lists where they have links in them, and that way you can create horizontal menus. Just add a little bit of spacing between them now, you have everything on a single horizontal line.
Next up, let's take a look at our images. Right now they're all displaying as inline elements, meaning they're stacking right to left right next to each other, and are they on the same line because they have enough room to fit. If we had more images they'd break down to another line. Okay, so I am going to go down into my code and right after the list item I am just going to do a selector for image. And for image I am just going to say display: block. And then just to help us out with spacing a little bit, underneath that I'm going to do a margin-bottom on them of 1 em.
So if I save that and preview my page, you can see what happens to our images. They now display, each on their on line because they are now block-level elements, and that margin between them gives us the spacing that we are seeing there. Okay, so now I am going to go down to these headings here. With these headings what we're going to do is we are going to experiment a little bit with the inline block display property. And this is the one that I really like and I've been using a lot lately. Well, what this allows you to do is to take block-level elements and make them display on the same line as if they were inline-level elements.
The downside to telling block-level elements to display as inline-level elements is that you start to lose control over some of the box model properties, simply because of the very nature of inline elements. But displaying something as an inline- block element sort of allows you to still control most of those block-level properties, and I'll show you what they mean in just a moment. So I am going to go into my code and we are going to write another selector, and this selector is going to be for the inlineB class selector, so .inlineB. And what we are going to do here is we are going to do display: inline-block and then just to help us out with our spacing, we are going to give it a margin right and we are going to do a margin right of one ems.
So we are going to save that and when I preview that, you can see that now these headings are displaying on one single line as if they were inline-level elements and then the margin that I am giving them is helping space them. Now remember what I said earlier about being able to control certain block-level properties. We can still do that. So if I go into my code and create another rule, let's just say .tall, so we'll just create a class called tall, and we'll go ahead and give that a height of 200 pixels. So if I save this and then go down to the very last heading within that group and also apply the class of tall--now if you've never done this before, if you want to apply multiple values to a class attribute, you just separate them with a white space.
So, inlineB tall. If I save this and test it, you can see that our third one is now 200 pixels tall and then while it has pushed the other content down, it didn't affect these guys at all. So you were allowed to control these individual box model properties that might be a little more difficult otherwise. Now I want to turn our attention to our last element here. You can also tell elements not to display as well. Now I am going to take a break from just using only the display property for just a moment, because I want to compare and contrast this method of causing something to disappear with another CSS property, and that property would be visibility.
Okay, so this h4 class has a class of none. The first thing I want to do is go back up and write the class selector for none, and I want to compare and contrast methods of sort of making this content go away. The first one I want to use is the visibility property. Now visibility allows us to set three properties on it. We can have--all right, so we have visible, hidden, or collapse. Now collapse deals with table rows or table columns, so if it's not applying to a table, then collapse is the same as hidden. So if I say hidden and save it and go back and preview this again, you can see the element goes away.
And what I want you to notice about this, however, is that even though I told the visibility be hidden, notice that the space that the element was taking up is still there. Okay, so that's one of things that the visibility hidden property does for us. It does hide the element visually, but the space that the element would take up in the layout, and how it's affecting other elements, remains. So if we were to increase, say, the height of that, for example, the rest of the layout would react to that. Okay, now I want you to contrast that with using the display property of none. So if I save this and preview it, you can see what happens.
That element is gone. Not only does it not display, but it's physically removed from the layout, and it's no longer affecting any other elements, and other elements aren't affecting it. It is as if it were never there. Now one thing I do want to point about this is that even though the element is not there visually anymore, it's still available through the document object model. So, that element would still be available for scripting and you get still affect it through that. Another thing I want to point out too: if you were thinking about using this as a method of maybe hiding text and then putting something in its place, screen readers, accessibility devices, are actually programmed now to ignore objects that have display of none.
So if you are wanting a screen reader to still read this content while maybe not displaying it visually, this probably is not the method that you want to be using. So as you can see, the display property gives us an extremely powerful way to control these elements on our page. Now like almost all the CSS capabilities that we are talking about, it's just a tool, and like most tools, you are going to need to decide if it's the right one for the job. Now later on we are going to use the display property to enhance various layouts that we are going to be working on, and we are going to explore different ways to use it within the context of a wider layout strategy.
There are currently no FAQs about CSS: Page Layouts.
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.
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.