Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
How Z-index Works5:41 with Guil Hernandez
When you use relative, absolute or fixed positioning on elements, you may end up with several elements occupying the same space. This can make elements overlap or completely cover up other elements from view. In this video, you'll learn to use the z-index property to determine which elements display above or below other elements.
- Fork this workspace to view the z-index demo
- MDN - z-index
- MDN - The Stacking Context
- What No One Told You About Z-Index
- CSS Tricks - z-index
- Positioned elements follow a stacking order that determines which elements display above or below other elements.
- By default, the stacking order of positioned elements is the order they appear in the source code.
- Elements appearing later in the code sit on top on elements appearing earlier in the code.
z-index propertyis directly related to stacking order and it's what prevents elements from overlapping other elements.
- An element with a higher
z-indexvalue overlaps an element with a lower
- Positioned elements have a
z-indexworks only on elements with a
positionproperty set to
- If you set a
z-indexon an element with no position, it will do nothing.
When you give a positioned element a
z-index, you establish a new stacking context for any descendants -- or children -- of that element. It's possible to have a higher
z-index element that's underneath another element with a lower
z-index. Learn more about stacking contexts here.
When you use relative, absolute, or fixed positioning on elements,
you may end up with several elements occupying the same space.
This can make elements overlap or
completely cover up other elements from view.
For example, when you scroll down The Best City guide page, the entire
figure element overlaps the fixed header when it reaches the top of the page.
So why isn't it scrolling under the header like the rest of the content on the page?
You see, positioned elements like the header and figure follow a stacking
order that determines which elements display above or below other elements.
And by default, the stacking order of positioned elements,
is the order they appear in the source code.
So elements that appear later in the code,
sit on top of elements that appear earlier in the code.
In the HTML, the figure element appears below main header in the source code.
Because of this, the figure element is higher than main-header in the stacking
order so it overlaps it.
We can adjust the stacking order of relative, absolute,
and fixed positioned elements with the z-index property.
The z-index property is directly related to stacking order and
it's what prevents elements from overlapping other elements.
It's called z-index because it affects an element's position along the z axis.
When you position an element top, right, bottom, or left,
you're positioning the element in two dimensional space on the x and y axis.
So, z-index adds a third axis to your page, called the z-axis.
And this axis moves elements towards the front and back of the page.
An element with a higher z-index value
overlaps an element with a lower z-index value.
For example, an element with a z-index of 20 will appear
over the top of one with a z-index of ten.
And an element with a z-index of 30 will appear over one with a z-index of 20 or
Positioned elements have a z-index of zero by default.
So to position main-header over the figure element,
I can give it any z-index value higher than zero.
So right below the top declaration,
I'll add a z-index property, and give it a zindex value of one.
So now I have established a new stacking order for main header.
And when I scroll the page, notice how the figure scrolls under the header.
So great, it's fixed.
Back in my style sheet, if I give icon location a z-index
that's slightly higher, so let's say z-index two,
now the icon has a higher stacking order than the main header,
so it overlaps the header when they occupy the same space.
So, I'm going to define a much higher z-index for main-header just in case I
decide to add more positioned elements to the page later.
So, I'll change the z-index value from 1 to 1000.
This way I know my header will be the highest in the stacking order.
Remember, z-index will only work on an element with a position property set to
absolute, fixed, or relative.
If you try to set a z-index on an element with no position, it will do nothing.
Also, when you give a position element a z-index, you establish a new
stacking context for any descendents, or children, of that element.
For example, if I apply relative or absolute positioning to the name or
main-nav elements inside main-header and give them a z-index.
That z-index value will be relative to main headers z index of 1,000.
So it is possible to have a higher z-index element that's
underneath another element with a lower z-index.
And you can read more about stacking context in the teacher's notes.
I suggest experimenting with the z-index values on your own.
It's a concept that's best understood by adding positioned and non-positioned
elements to a page and experimenting with the different z-index values.
In the teachers notes, you'll find a link to a work space demo on z-index.
Congrats, you've come a long way as a designer and developer of CSS layouts.
Now that you understand the strengths and benefits of
using layout methods like inline block, floats, and positioning you should start
thinking about how to use them in your projects to build amazing layouts.
So what's next?
Well to practice what you've learned in this course right now you can
build the other pages of the Best City Guide website.
Or use the project from this course to create your own portfolio site.
You can redesign the layout of your website using a mobile first approach, or
revisit recent projects and
see if you notice any of the strange browser behaviors covered in this course
like collapsing containers or overlapping elements.
And if you notice any, now you know how to fix them.
The Treehouse community is always here to help.
So if you have questions about anything covered in this course feel free to
reach out to the Treehouse staff, other students,
or you can get ahold of me on Twitter, I'm @gilage.
You need to sign up for Treehouse in order to download course files.Sign up