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

Controlling element display

From: CSS: Page Layouts

Video: Controlling element display

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.

Controlling element display

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.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40667 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

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 CSS: Page Layouts.

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.