The static
value is the default for the position
property. An element with position: static;
is not positioned in any special way; it is always positioned according to the normal flow of the page. The top
, right
, bottom
, left
, and z-index
properties have no effect on a statically positioned element.
Example 1: Default Behavior
HTML
<div class="static-element">
This is a static element. It just sits here.
</div>
<p>This paragraph follows the natural flow of the document.</p>
CSS
/* No special positioning is applied to this div */
.static-element {
position: static;
background-color: lightblue;
}
Explanation: The div
with the class .static-element
has position: static;
. It appears on the page exactly where it falls in the HTML document order, right before the paragraph.
Example 2: Ignoring Offset Properties
HTML
<div class="static-offset-ignored">
I have top and left properties, but they are ignored.
</div>
CSS
/* The top and left properties will have no effect */
.static-offset-ignored {
position: static;
top: 50px; /* This property is ignored */
left: 50px; /* This property is ignored */
background-color: lightcoral;
}
Explanation: Even though top
and left
properties are set, the browser ignores them because the element's position is static
. The element remains in its default place in the document flow.
Example 3: Z-index Ineffectiveness
HTML
<div class="static-zindex">I'm static.</div>
<div class="another-element">I'm another element.</div>
CSS
/* The z-index property has no effect on static elements */
.static-zindex {
position: static;
z-index: 10; /* This property will not work */
background-color: lightgreen;
}
.another-element {
background-color: gold;
margin-top: -20px; /* Overlap for demonstration */
}
Explanation: The z-index
property is intended to control the stacking order of positioned elements. Since the first div
is static
, the z-index
value is ignored, and it cannot be brought in front of the overlapping second element.
Example 4: Stacking in Normal Flow
HTML
<div class="static-block">Block 1</div>
<div class="static-block">Block 2</div>
CSS
/* Static elements stack vertically by default */
.static-block {
position: static;
background-color: #f0f0f0;
border: 1px solid #ccc;
padding: 10px;
margin: 5px;
}
Explanation: These div
elements are block-level and have position: static
. They stack vertically, one after the other, respecting their margins as part of the normal document flow.
Example 5: Inline Elements
HTML
<p>
This is a sentence with a <span class="static-inline">static span</span> inside it.
</p>
CSS
/* This span remains in its natural inline position */
.static-inline {
position: static;
background-color: yellow;
}
Explanation: The span
element is an inline element with position: static
. It remains in its natural position within the flow of the text inside the paragraph, without any special positioning.
Example 6: No New Stacking Context
HTML
<div class="static-parent">
<div class="child">Child</div>
</div>
CSS
/* The parent does not create a new stacking context */
.static-parent {
position: static;
background-color: #e0e0e0;
}
.child {
/* This element is positioned relative to the initial containing block, not the parent */
position: absolute;
top: 0;
left: 0;
background-color: lightpink;
}
Explanation: An element with position: static
does not create a new stacking context. Therefore, the absolutely positioned child is not contained by the .static-parent
and is instead positioned relative to the initial containing block (usually the <html>
element).
Example 7: Inside a Flex Container
HTML
<div class="flex-container">
<div class="static-flex-item">Item 1</div>
<div class="static-flex-item">Item 2</div>
</div>
CSS
/* Flexbox controls the layout, but the items themselves are static */
.flex-container {
display: flex;
}
.static-flex-item {
position: static;
background-color: turquoise;
margin: 5px;
}
Explanation: Even within a flex container, the flex items themselves retain their default position: static
. While their layout is controlled by the flexbox rules of the parent, their positioning scheme remains in the normal flow.