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

Controlling stacking order

From: CSS: Page Layouts

Video: Controlling stacking order

You may have noticed in several of our exercises so far that positioning elements can often result in them overlapping each other. To make sure that your layouts behave the way that you expect them to, you need to understand the rules that govern the stacking of these elements and how you can control stacking through CSS. So I have the stacking.htm file open, and there are a few changes. For the most part it's the same, but we have some text now that describes Z-index, which is the property that we are going to be controlling, and then we have our containing element and the three div tags inside of it, so that much is the same.

Controlling stacking order

You may have noticed in several of our exercises so far that positioning elements can often result in them overlapping each other. To make sure that your layouts behave the way that you expect them to, you need to understand the rules that govern the stacking of these elements and how you can control stacking through CSS. So I have the stacking.htm file open, and there are a few changes. For the most part it's the same, but we have some text now that describes Z-index, which is the property that we are going to be controlling, and then we have our containing element and the three div tags inside of it, so that much is the same.

But currently, now, all three of those elements have been positioned using absolute positioning. And if I preview the file in a browser, you can see these three elements are stacking one right on top of the other because their offsets aren't far enough to keep them from overlapping. So notice that by default right now we are not controlling the stacking in anyway. Notice that by default, they are overlapping one on top of the other, and what's controlling the order of the stacking right now is the source order of the code.

So the last object encountered in the code, the last one rendered, is stacked on top of the other objects. You can think of them as sort of like the first one will be on the bottom, then the second, and then the third. In fact, if we change that source order, so if I go down here to our elements and if I take the first element here, and I am just going to cut that out and then move it down here below the third one, now it's not going to change their position in the layout at all, because they're being absolutely positioned. What it does change, however, is the order in which they're encountered by the browser in the code.

So now, if I save that and go back into the browser and refresh, you can see that now One is actually on top of Two. It would be on top of Three actually as well, if they were overlapping, because now One is encountered last, so it is the topmost object. All right, I am going to undo that because I want us to work from the context of sort of the default one as we begin to control it. Now if we want to take control of the stacking process, the property that we use id does the Z-index properties.

You can think of Z-index, like, you have an X axis and a Y axis that control horizontal and vertical positioning. You can think of the Z axis as controlling stacking, and Z-index allows us to tell an element where it fits along that Z axis, if you will. All right, so I am going to go to element 1, and I am just going to do Z-index. That's the name of the property, z- index, z-index, and I am going to go ahead and give it an order of one, or a value of 1, I should say.

For element 2, I am going to give a z-index of 3, and for element 3 I am going to give it a z-index of 2. Now I am doing these in one-value increments, one, two, and three. The actual value doesn't matter; what matters is which value is higher. The higher the value, the higher it is in the stacking order. So I could have put element two at 400 and it wouldn't have mattered; it would still be on top of those other two. So how far away you space them in values is totally up to you, and is usually dependent upon how many objects you have stacking and how much control you need.

So if I save this, go back to the browser and refresh, boom, you can see that now element number Two is on top because it has the highest z-index value. If I go back into the code, let's see what happens if two elements have exactly the same z-index value. So, if I go to element1 and give it a z-index of 3 and leave element 2's z-index of 3, so if I save that and preview it, you can see that it doesn't appear like anything else has happened, but what's actually happening here is to resolve the conflict between One and Two who have the same z-index value, source orders is then used, so it falls back source order and since Two comes after One, Two wins the battle, even though the two of them have exactly the same z-index.

You can use negative values. If I go into element1 and change its z-index from 3 to -1, if I save this and go back to my browser, let's take a look at what happens. So that actually moves it down below elements that are considered in normal document flow. I mean one of the ways that I like to describe this to people is think about a piece of paper, just being two- dimensional, flat. All of your elements in normal document flow are kind of painted on the flat canvas. And then if you position elements using positioning, it pretty much takes them off of that flat canvas.

It sort of hovers them, if you will, over the page. Well, negative values send it below the page. But what you have to understand is it's going to send it below everything except for what it calls the initial stacking context, which is really fancy and will impress people at the cocktail parties if you use that term. So our HTML tag, or this gray background back here, is the initial stacking context. It's the first element, so it establishes the initial stacking order. So negative values will move it below everything except for that initial stacking context, which is why it's below the body, this white box here, but not below the HTML tag, which is the gray box.

Stacking contexts are another thing that you really need to understand, and it's not the easiest thing in the world to understand. Every element, once that element is given a z-index rating, it establishes a new stacking context. So initially the only stacking context you have on the page is the initial root tag, which is the HTML element. But once you apply a z-index rating to an element, that element begins a new stacking context. Now, if it contains child elements, they are now all considered part of that one stacking context, which is different.

It's easier to see than it is to sort of understand it the first time you hear, so let me show you. So we know that our div tags are wrapped in this section, so that section, you can think of that as a containing element. So if go up to my container selector and I add a z-index value here of 1 and save that, if I go back in the browser and refresh, watch what happens to the number One. All of a sudden, even though it has a negative value, it comes up.

Now it is still the bottom of our stacking order here, but the reason that it comes up is that now it belongs to the stacking context of the containing element, which is this sort of brown background here. So it no longer belongs to the stacking context of the HTML tag; it belongs to the stacking context of the container section, which means that now it's going to order itself based on that context. So now that we have seen this a little bit, let's go through this list that we have right here in the browser that talks about the rules behind stacking context and how stacking conflicts are resolved.

So the first the first bullet point and the very first thing you see here is that the backgrounds and borders of the element forming the stacking context is painted first. In this case, you can think of it as the gray background to the browser window. And then next, any child-stacked elements with a negatives z-index. Now that would have been our element number 1, but it's in a new stacking context now. Next after that, elements in normal document flow. That will be the body, all the text that you see on the page, that sort of thing. Then any non-positioned floats, so floated elements do hover over elements in normal flow, but they're not quite as high as elements that are positioned with z-index.

After that, it would paint any child- stacked elements with z- index of zero or auto. So if you position something, if you say, position absolute, for example, or position fixed, and you don't give it a z-index rating, the z-index is considered to be zero or auto. And then right after that, any child-stacked elements with a positive z- index value, lowest to highest. So that's how stacking order is determined as the page is being painted within the browsers. Now, in most cases you are not going to need to go into it that deep.

Most of the time the default stacking orders are going to work just fine, but every now and then you are going to find yourself needing to tweak your layouts and use z-index property. Now it's especially true if you're working with heavily positioned layouts or for when you're building complex widgets or interface components, things like that. Now in those cases, understanding how stacking context and the z-index property, understanding how those work, can be extremely important.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40604 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.