Stacking and float
« CSS « Understanding CSS z-index
Stacking and float
For floating blocks the stacking order is a bit different. Floating blocks are placed between non-positioned blocks and positioned blocks:
- Background and borders of the root element
- Descendant blocks in the normal flow, in order of appearance (in HTML)
- Floating blocks
- Descendant positioned elements, in order of appearance (in HTML)
Actually, as you can see in the following example, the background and border of the non-positioned block (DIV #4) is completely unaffected by floating blocks, while the content is affected. This happens according to CSS standard float behaviour.
This behaviour can be explained with an improved version of the previous ordered list:
- Background and borders of the root element
- Descendant blocks in the normal flow, in order of appearance (in HTML)
- Floating blocks
- Inline descendants in the normal flow
- Descendant positioned elements, in order of appearance (in HTML)
Note: In the example below, all the blocks except the non-positioned one, are translucent show the stacking order. If the opacity of the non-positioned block (DIV #4) is reduced, then something strange happens: the background and border of that block pops up above the floating blocks, but still under positioned blocks. I was not able to understand whether this is a bug or a peculiar interpretation of the specifications. (Applying opacity could implicitly create a stacking context.)
Example source code
HTML
Copy Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Stacking and float</title> <style type="text/css"> div { font: 12px Arial; } span.bold { font-weight: bold; } #absdiv1 { opacity: 0.7; position: absolute; width: 150px; height: 200px; top: 10px; right: 140px; border: 1px dashed #990000; background-color: #ffdddd; text-align: center; } #normdiv { /* opacity: 0.7; */ height: 100px; border: 1px dashed #999966; background-color: #ffffcc; margin: 0px 10px 0px 10px; text-align: left; } #flodiv1 { opacity: 0.7; margin: 0px 10px 0px 20px; float: left; width: 150px; height: 200px; border: 1px dashed #009900; background-color: #ccffcc; text-align: center; } #flodiv2 { opacity: 0.7; margin: 0px 20px 0px 10px; float: right; width: 150px; height: 200px; border: 1px dashed #009900; background-color: #ccffcc; text-align: center; } #absdiv2 { opacity: 0.7; position: absolute; width: 150px; height: 100px; top: 130px; left: 100px; border: 1px dashed #990000; background-color: #ffdddd; text-align: center; } </style> </head> <body> <br /><br /> <div id="absdiv1"> <br /><span class="bold">DIV #1</span> <br />position: absolute; </div> <div id="flodiv1"> <br /><span class="bold">DIV #2</span> <br />float: left; </div> <div id="flodiv2"> <br /><span class="bold">DIV #3</span> <br />float: right; </div> <br /> <div id="normdiv"> <br /><span class="bold">DIV #4</span> <br />no positioning </div> <div id="absdiv2"> <br /><span class="bold">DIV #5</span> <br />position: absolute; </div> </body> </html>
See also
- Stacking without z-index : Default stacking rules
- Adding z-index : Using z-index to change default stacking
- The stacking context : Notes on the stacking context
- Stacking context example 1 : 2-level HTML hierarchy, z-index on the last level
- Stacking context example 2 : 2-level HTML hierarchy, z-index on all levels
- Stacking context example 3 : 3-level HTML hierarchy, z-index on the second level
Original Document Information
- Author(s): Paolo Lombardi
- This article is the english translation of an article I wrote in italian for YappY. I grant the right to share all the content under Creative Commons: Attribution-Sharealike license
- Last Updated Date: November 3rd, 2014
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/css/css_positioning/understanding_z_index/stacking_and_float