An HTML document is a living document
Even without any CSS applied, an HTML document already has its own rules:
- fluidity: how the content adapts to browser dimensions
- ordering: in which order elements appear
- stacking: how elements appear on top of each other
This natural behavior is logical.
Fluidity
In HTML, the content is King.
All block elements are fluid. They will naturally adapt their layout to accommodate their inner content:
- width: 100%
a block will take up the whole width available - word wrap
if a block’s inline content doesn’t fit on a single line, it will continue on a new line - height: auto
a block’s height varies automatically to match its content’s size
- A
blockis by default in full width - Its height is the height of its content
Ordering
HTML elements are displayed in the order in which they are written in the code. First in the code -> first in the browser.
Each block appears in the order in which they appear in the HTML code, from top to bottom.
<p>First</p>
<p>Second</p>
<p>Third</p>
<p>Fourth</p>
<p>Fifth</p>First
Second
Third
Fourth
Fifth
Stacking
A browser has 3 dimensions.
Each HTML element belongs to an imaginary layer.
The stack order depends on how elements are nested: child elements appear on top of their respective parents.
- Each nested element appears on top of its parent.
- The deeper in the hierarchy, the higher in the stack.
<div>
This parent is behind
<p>
This nested child appears <strong>on top</strong> of its parent
</p>
</div>This nested child appears on top of its parent
Breaking the flow
While the browser’s default behavior is efficient, it may not be sufficient for your design needs.
Several CSS properties allow to disrupt the Flow:
heightandwidthcan alter an element’s fluidityfloatdisrupts an element’s behavior as well as its surroundingspositionabsoluteandfixedremove an element from the Flowz-indexcan alter the order in which elements are stacked