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.

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.

Controlling stacking order

You may have noticed in several of our exercises so far that positioningelements can often result in them overlapping each other.To make sure that your layouts behave the way that you expect them to, you needto understand the rules that govern the stacking of these elements and how youcan control stacking through CSS.So I have the stacking.htm file open, and there are a few changes. For themost 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 haveour 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 usingabsolute positioning. And if I preview the file in a browser, you can see thesethree elements are stacking one right on top of the other because their offsetsaren'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'scontrolling 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 ontop of the other objects.You can think of them as sort of like the first one will be on the bottom, thenthe second, and then the third.In fact, if we change that source order, so if I go down here to our elements andif I take the first element here, and I am just going to cut that out and thenmove it down here below the third one,now it's not going to change their position in the layout at all, becausethey're being absolutely positioned.What it does change, however, is the order in which they're encountered by thebrowser in the code.

So now, if I save that and go back into the browser and refresh, you can see thatnow One is actually on top of Two.It would be on top of Three actually as well, if they were overlapping, becausenow 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 ofsort 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 useid does the Z-index properties.

You can think of Z-index, like, you have an X axis and a Y axis that controlhorizontal and vertical positioning.You can think of the Z axis as controlling stacking, and Z-index allows us totell 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 aheadand 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 togive 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 usuallydependent 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 thatnow 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 exactlythe 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'tappear like anything else has happened, but what's actually happening here is toresolve the conflict between One and Two who have the same z-index value, sourceorders is then used, so it falls back source order and since Two comes after One, Twowins 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 from3 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 innormal document flow.I mean one of the ways that I like to describe this to people is think about apiece of paper, just being two- dimensional, flat. All of your elements in normaldocument flow are kind of painted on the flat canvas. And then if you positionelements 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 isit's going to send it below everything except for what it calls the initialstacking context, which is really fancy and will impress people at the cocktailparties 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 initialstacking context, which is why it's below the body, this white box here, but notbelow the HTML tag, which is the gray box.

Stacking contexts are another thing that you really need to understand, and it'snot the easiest thing in the world to understand.Every element, once that element is given a z-index rating, it establishes anew stacking context.So initially the only stacking context you have on the page is the initial roottag, which is the HTML element.But once you apply a z-index rating to an element, that element begins anew stacking context.Now, if it contains child elements, they are now all considered part of thatone 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, youcan 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 itcomes up is that now it belongs to the stacking context of the containingelement, 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 thatnow 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 wehave right here in the browserthat talks about the rules behind stacking context and how stackingconflicts are resolved.

So the first the first bullet point and the very first thing you see here isthat 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 wouldhave 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 innormal 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 orauto. 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 isconsidered to be zero or auto. And then right after that, any child-stackedelements with a positive z- index value, lowest to highest.So that's how stacking order is determined as the page is being paintedwithin 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, butevery now and then you are going to find yourself needing to tweak your layoutsand use z-index property.Now it's especially true if you're working with heavily positioned layouts orfor 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.

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.

Already a member ?

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

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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:

new course releases

newsletter

general communications

special notices

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:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.