You want to overlay a Show
<div class="container"> <div class="item item1">item 1</div> <div class="item item2">item 2</div> </div> You want the second item to be on top of the first item. How do you do this? The SolutionYou can set the .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } 0, .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } 1, .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } 2, and .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } 3 CSS properties so that it is placed on top of item 1: .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } We moved item 2 20px from the top and 40px from the left of the container that item 2 is positioned relative to. This places item 2 on top of item 1. If you have multiple items, you can change the stacking order of items using the .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } 4 property. A new stacking context is created by an element when the element has a .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } 4 value is not .container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; } 9, which is the default value. The stacking order of elements is relative to their stacking context. Note that new stacking contexts can also be created by specific CSS properties such as I have a png of a word: INSPIRE, whose lettering is transparent. Behind the letters are coloured rectangles. In front of the coloured rectangles are white rectangles that give the impression of the INSPIRE letters being filled up, depending on how high the rectangles reach. The example (under the link) has the white rectangles set to 50% height. In the original, these heights are determined by numbers pulled from a database and not set the way they are in the script. The filling of the INSPIRE happens thus: There is a 'w3 div' called w3-third, within this sits another div called w3-card, within this is an ordinary div - styled in the header - called back and within this sits the divs for each letter block and finally the div for the INSPIRE png. I make use of z values so the png sits on top. Now it’s time to turn your attention to the second pair of html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 0 property values— html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 1 and html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 2. The first pair of values— html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 3 and html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 4—are closely related, and we looked into those in great detail in the last article. Absolutely positioned elements are removed entirely from the document flow. That means they have no effect at all on their parent element or on the elements that occur after them in the source code. An absolutely positioned element will therefore overlap other content unless you take action to prevent it. Sometimes, of course, this overlap is exactly what you desire, but you should be aware of it, to make sure you are getting the layout you want! Fixed positioning is really just a specialized form of absolute positioning; elements with fixed positioning are fixed relative to the viewport/browser window rather than the containing element; even if the page is scrolled, they stay in exactly the same position inside the browser window. In this article of the Web Standards Curriculum I’ll give you some practical examples of using both html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 1 and html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 2 positioning, look at some browser support quirks, and explore the concept of z-index. Before I talk about all this though, I’ll cover an essential prequisite concept—containing blocks. Containing blocksAn essential concept when it comes to absolute positioning is the containing block: the block box that the position and dimensions of the absolutely positioned box are relative to. For static boxes and relatively positioned boxes the containing block is the nearest block-level ancestor—the parent element in other words. For absolutely positioned elements however it’s a little more complicated. In this case the containing block is the nearest positioned ancestor. By “positioned” I mean an element whose html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 0 property is set to html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 4, html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 1 or html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;} 2—in other words, anything except normal static elements. So, by setting The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.1 for an element you make it the containing block for any absolutely positioned descendant (child elements), whether they appear immediately below the relatively positioned element in the hierarchy, or further down the hierarchy. If an absolutely positioned element has no positioned ancestor, then the containing block is something called the “initial containing block,” which in practice equates to the The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.2 element. If you are looking at the web page on screen, this means the browser window; if you are printing the page, it means the page boundary. Elements with fixed positioning differ from this slightly—they always have the initial containing block as their containing block. So, let’s summarize this in a set of easy steps—to find the containing block for an element with The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.3 , this is what you need to do:
Absolute positioningFixed positioning is a special form of absolute positioning, so we’ll study that later, and concentrate on the more generalized case here. Unless otherwise stated, when I use the term “absolutely positioned” from now until the end of the article, I’ll be referring both to elements with inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 0 and elements with The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.3 . Specifying the positionWith relative positioning, you learned that the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 3, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 4 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5 properties could be used to specify the position of the box. You use the same properties to specify the position of an absolutely positioned box, but the way you use them is quite different. For a relatively positioned element, the four properties specify the relative distance to shift the generated box. Remember that in the case of relative positioning they complement one another, so that inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 6 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 7 means the same, and it’s not meaningful to specify both inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 4 (or inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' }
These points are not true in the case of absolute positioning. Here, all four properties can be used at the same time, to specify the distance from each edge of the positioned element to the corresponding edge of the containing block. You can also specify the position of one of the corners of the absolutely positioned box—say by using inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5—and then specify the dimensions of the box using outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 4 and outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 5 (or just use no outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 4 and outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 5 if you want to let it shrink-wrap to fit its contents). Microsoft Internet Explorer version 6 and older don’t support the method of specifying all four edges, but they do support the method of specifying one corner plus the dimensions. / This method works in IE6 / foo {position: absolute; top: 3em; left: 0; width: 30em; height: 20em; } / This method doesn't work in IE6 / foo {position: absolute; top: 3em; right: 0; bottom: 3em; left: 0; } The thing to remember here is that the values you set for the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 3, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 4 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5 properties specify the distances from the element’s edges to their corresponding containing block edges. It’s not like in a co-ordinate system where each value is relative to one point of origin. For instance, outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 2 means that the right edge of the absolutely positioned box will be 2em from the right edge of the containing block. It’s absolutely crucial to know what your containing block is when you’re using absolute positioning. That’s why setting The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.1 on your containing block is so useful, even if you are not actually shifting the position of the box. It allows you to make an element the containing block for its absolutely positioned descendants—it gives you control. Let’s try an example out to see how it works.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>
html, body { margin: 0; padding: 0; } outer {margin: 5em; border: 1px solid f00;} inner {width: 6em; height: 4em; background-color: 999;}
The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 absolutely positioned! Add the following highlighted declaration to the outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 rule: inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' }
First of all, making outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 absolutely positioned removed it entirely from the document flow. That means its parent, outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7, now has no children that are in the normal flow, so therefore its height collapses to zero. What looks like a 2px-thick red line is actually a 1px border around an element with zero height—you’re seeing the top and bottom borders with nothing inbetween. The second interesting thing is that the absolutely positioned box didn’t move. The default value for the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 3, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 4 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5 properties is inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2, which means the absolutely positioned box will appear exactly where it would have had if it wasn’t positioned. Since it’s removed from the flow it will overlap any elements in the normal flow that follow it, though. This is actually very useful—you can rely on this if you only want to move a generated box in one dimension. For instance, in a CSS-driven drop-down menu, the “drop-down” panes can be absolutely positioned with only the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2 property specified. They will then appear at the expected co-ordinate along the X axis (the same as their parent), automatically.
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7 element so that it looks like a rectangle again, and move outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 sideways. Add the following highlighted lines to your CSS rules: outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' }
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7 element is now a rectangle again, since you set a height for it. The outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 element has shifted sideways, but not to where you might have expected it to go. It’s not 1em from the left border of its parent, but 1em from the left edge of the window! The reason is that, as explained above, outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 has no positioned ancestor, so its containing block is the initial containing block. If you specify a position other than inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2, it’s relative to the corresponding edge of the containing block. When you set outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: static;''' } 0, the left edge of outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 ended up 1em from the left edge of the window.
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7 rule: outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' }
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7 rule has made it positioned and set it as the containing block for any absolutely positioned descendants it might have. The outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: static;''' } 0 you set for outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 now counts from the left edge of outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7, not the left edge of the browser window. Specifying dimensionsAbsolutely positioned elements will shrink-wrap to fit their contents unless you specify their dimensions. You can specify the width by setting the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 3 properties, or by setting the outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 4 property. You can specify the height by setting the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 4 properties, or by setting the outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 5 property. Any of these six properties can be specified as a percentage. Percentages are, by their very nature, relative to something else. In this case they are relative to the dimensions of the containing block. For an absolutely positioned element, percentage values for the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 3 and outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 4 properties are relative to the width of the containing block. Percentage values for the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 4 and outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 5 properties are relative to the height of the containing block. Internet Explorer 6 and older, and also Opera 8 and older, got this entirely wrong and used the dimensions of the parent block instead. Let’s experiment with another example to see how that can make a big difference.
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 using percentage values—make the following changes to the outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 rule: inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; }
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7, since it has The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.1 . The outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 element’s width is now half that of outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7, and its height is half the height of outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7.
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7: outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: static;''' }
The third dimension—z-indexIt’s natural to regard a web page as two-dimensional. Technology hasn’t evolved far enough that 3D displays are commonplace, so we have to be content with width and height and fake 3D effects. But CSS rendering actually happens in three dimensions! That doesn’t mean you can make an element hover in front of the monitor—yet—but you can do some other useful things with positioned elements. The two main axes in a web page are the horizontal X axis and the vertical Y axis. The origin of this co-ordinate system is in the upper left-hand corner of the viewport, ie where both the X and Y values are 0. But there is also a Z axis, which we can imagine as running perpendicular to the monitor’s surface (or to the paper, when printing). Higher Z values indicate a position “in front of” lower Z values. Z values can also be negative, which indicate a position “behind” some point of reference (I’ll explain this point of reference in a minute). Before we continue, I should warn you that this is one of the most complicated topics within CSS, so don’t get disheartened if you don't understand it on your first read. Positioned elements (including relatively positioned elements) are rendered within something known as a stacking context. Elements within a stacking context have the same point of reference along the Z axis. I’ll explain more about this below. You can change the Z position (also called the stack level) of a positioned element using the <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 property. The value can be an integer number (which may be negative) or one of the keywords inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2 or <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>10. The default value is inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2, which means the element has the same stack level as its parent. You should note that you can only specify an index position along the Z axis. You can’t make one element appear 19 pixels behind or 5 centimetres in front of another. Think of it like a deck of cards: you can stack the cards and decide that the ace of spades should be on top of the three of diamonds—each card has its stack level, or Z index. If you specify the <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 as a positive integer, you assign it a stack level “in front of” other elements within the same stacking context that have a lower stack level. A <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 of 0 (zero) means the same as inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2, but there is a difference to which I will come back in a minute. A negative integer value for <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 assigns a stack level “behind” the parent’s stack level. When two elements in the same stacking context have the same stack level, the one that occurs later in the source code will appear on top of its preceding siblings. There can in fact be no less than seven layers in one stacking context, and any number of elements in those layers, but don't worry—you are unlikely to have to deal with seven layers in a stacking context. The order in which the elements (all elements, not only the positioned ones) within one stacking context are rendered, from back to front is as follows:
inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2 or (zero)
The highlighted entries are the elements whose stack level we can change using the <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 property. This whole thing can be rather difficult to imagine, so let’s do some practical experiments to illustrate Z-index.
<body> <div id="outer"> </div>
</body>
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7 is the containing block and set non-percentage dimensions of outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4. Let’s make outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7 a little taller, too, to give you more room to experiment. Make the following highlighted changes to the two rules: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>0
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>1
inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2, the initial value, which means stack level 0) but the blue box is rendered in front of the grey box, because it appears later in the source code. You can make the grey box appear in front by giving it a positive stack level. You only have to set it larger than 0—there’s no need to go overboard and use a value like 10000. Add the following highlighted line to the outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 rule: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>2
Local stacking contextsThe rest of this section discusses local stacking contexts. This probably isn’t something you will encounter in your normal design work unless you attempt to do some really advanced things with absolute positioning, but I thought I’d include it for completeness. You can elect to skip this if you wish. Every element whose <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 is specified as an integer establishes a new, “local”, stacking context in which the element itself has stack level 0. This is the difference I mentioned before between <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>25 and <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>26. The former doesn’t establish a new stacking context, but the latter does. When an element establishes a local stacking context, the stack levels of its positioned descendants apply within this local context only. These descendants can be re-stacked with respect to one another, and with respect to their parent, but not with respect to the parent’s siblings. It’s like the parent forms a “cage” around its descendants, so that they cannot escape from it. The descendants may be moved up and down within this cage, but they can’t get out of the cage. The parent and its descendants will form an indivisible unit within the stacking context that surrounds the parent. Imagine you’re sorting out your paperwork before delivering it to the accountant who does your taxes. You have expense reports, receipts, order confirmations and whatnot, and you stack one paper on top of another—to make life easier for you accountant, you insert types of papers that belong together in different envelopes. A local stacking context is analogous to such an envelope. It keeps related elements together and prevents other elements from being inserted between them. You can sort the contents within each envelope as you like, but that sort order only applies within that envelope and has no bearing on the stack of papers as a whole. Your stack now contains a mix of loose papers (elements with stack level inner {'''width: 50%;''' '''height: 50%;''' background-color: 999;position: absolute; left: 1em; } 2), and envelopes (elements with an integer stack level). Envelopes with positive stack levels lie on top of the loose papers, while envelopes with negative stack levels appear at the bottom of the pile. Each time you assign an integer value to the <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 property for an element, you create an “envelope” that contains that element and its descendants. Let’s look at how those local stacking contexts work. It may look confusing, but it’s really not much different from what you’ve already seen. If you follow the examples, you should be able to get a feel for how things work.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>3
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>4 This makes the <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>29 elements absolutely positioned and sets their positions and dimensions. Wait a second though— <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>29 elements are inline—how can you specify dimensions for inline elements? The answer is that absolutely positioned elements, like floated elements, automatically generate block boxes. The positions you specify will apply relative to each <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>29’s containing block. Since both <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>29 elements have an absolutely positioned <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>34 as a parent, those parents take on the role of containing blocks.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>5
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>6
Confused yet? The next experiment should make things clearer.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>7
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>8
Figure 1: Illustration of different stacking contexts. The elements appearing inside "2" will always appear in front of all of the elements inside "1". Then within each stacking context, elements with a higher z-index number appear in front of elements with a small z-index number. If two elements have the same z-index number, the one appearing later in the markup will appear in front. This part was probably quite confusing, especially if you’re new to CSS. The point is that you need to know your stacking contexts if you’re trying to change the stack levels of different elements. If an element belongs to a local stacking context you can only change its position along the Z axis within that local context. An element in one local stacking context cannot slide in between two elements in another local stacking context. The good news is that you’ll most likely never encounter these problems. Changes in <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 are not very common in good layouts, and if they occur at all it is usually within one stacking context. Fixed positioningAn element with inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 0 is fixed with respect to the viewport. It stays where it is, even if the document is scrolled. For <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>41 a fixed element will be repeated on each printed page. Note that Internet Explorer versions 6 and older do not support fixed positioning at all. If you use one of those browsers you will not be able to see the results of the examples in this section. Whereas the position and dimensions of an element with The ` inner` element has a specified width and height and a grey background colour. The `outer` element, which is the structural parent of `inner`, has a red border. It also has a 5em margin all around, to shift it away from the edges of the browser window and let us see more clearly what is going on. Nothing surprising so far, right? The height of the `outer` element is given by its child element (`inner`) and the width by the horizontal margins.3 are relative to its containing block, the position and dimensions of an element with inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 0 are always relative to the initial containing block. This is normally the viewport: the browser window or the paper’s page box. To demonstrate this, in the example below you will make one of your elements fixed. You will make the other one very tall in order to cause a scrollbar, to make it easier to see the effect it has.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>9
outer {margin: 5em; border: 1px solid f00;'''height: 4em;''' } inner {width: 6em; height: 4em; background-color: 999;position: absolute; '''left: 1em;''' } 5 value for <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>
21. (What kind of giant monitor do you have, anyway?) The tall blue element extends beyond the bottom of the window. Scroll the page downward, and keep an eye on the grey square in the top left-hand corner. outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 4 is now fixed in position 1em from the top of the window and 1em from the left side, therefore as you scroll, the grey box stays in the same place on the screen. SummaryAbsolutely positioned elements are removed entirely from the document flow. They will overlap other content unless you make room for them. If all child elements of a container are absolutely positioned, the parent’s height will collapse to zero. Absolutely positioned elements are positioned with respect to a containing block, which is the nearest postioned ancestor. If there is no positioned ancestor, the viewport will be the containing block. Elements with fixed positioning are fixed with respect to the viewport—the viewport is always their containing block. They always appear at the same place inside the browser window when viewed on screen; when printed, they will appear on each page. The positions of each edge of an absolutely positioned element can be specified with the inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 2, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 3, inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 4 and inner {width: 6em; height: 4em; background-color: 999;'''position: absolute;''' } 5 properties. The value of each property specifies the distance of that edge to the corresponding edge of the element’s containing block. All positioned elements are rendered at a certain stack level within a stacking context. You can change the stack level of a positioned element using the <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 property. When <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head>
<body> </body>
</html>08 is specified as an integer value, the element establishes a local stacking context for its descentants. Exercise questions
outer {margin: 5em; border: 1px solid f00;height: 4em; '''position: relative;''' } 7 element, so that the red border appears across the yellow square.
Note: This material was originally published as part of the Opera Web Standards Curriculum, available as 37: CSS absolute and fixed positioning, written by Tommy Olsson. Like the original, it is published under the Creative Commons Attribution, Non Commercial - Share Alike 2.5 license. How do I make a div overlap another div?Add CSS. Specify the width and height of the "container" class. Set the position to "relative" and add the margin property.. Set both the width and height of the "box" class to "100%". Specify the position with the "absolute" value. ... . Style the "overlay" class by using the z-index, margin and background properties.. How do I make a div stay on top of another div?Using Z-index property The z-index property of CSS can be used to overlay one div over another. Add z-index value to the div element with position attributes. The z-index determine the order in which div stacks. How do I make a div float over another div?To float a <div> over another <div> , you can use CSS positioning and z-index. Here's an example of how you can achieve this: HTML: <div class="container"> How to put a div in another div?Steps to Position a Child DIV. Create an HTML file with two DIV tags.. Use CSS to give them height, width, and background color.. Now center the child DIV inside the parent DIV using three different ways.. |