HTML and CSS course Β· Module 3: CSS Basics
CSS Positioning
In this lesson5
Imagine you're building the interior of an Egyptian pyramid. Every decoration, every hieroglyph, and every statue must be in a precisely planned location. This is exactly what positioning in CSS is for - it gives us full control over where our elements appear on the page.
The position Property
The position property in CSS determines how an element is placed on the page. It has five main values, each behaving differently.
position: static (default)
Every HTML element by default has position: static. This means the element displays in the normal document flow - one below the other (blocks) or next to each other (inline). The top, left, right, and bottom properties don't work on static elements.
.element {
position: static; /* default value */
}It's like stones laid naturally in a pyramid - each lies in its place, following the building order.
position: relative
An element with position: relative stays in the normal document flow, but you can shift it relative to its original position using top, left, right, and bottom. Importantly - the space the element occupied is preserved.
.hieroglyph {
position: relative;
top: 20px; /* shifts down by 20px */
left: 30px; /* shifts right by 30px */
}Imagine a hieroglyph carved on a wall that we slightly shift - its "place" in the row of other hieroglyphs still exists, but the symbol itself is slightly offset.
position: absolute
An element with position: absolute is taken out of the normal document flow. It positions itself relative to the nearest ancestor that has a position other than static (i.e., relative, absolute, fixed, or sticky). If no such ancestor exists - it positions itself relative to the initial containing block (an area the size of the window).
.pyramid-container {
position: relative; /* positioning context */
width: 500px;
height: 400px;
}
.treasure {
position: absolute;
top: 50px;
right: 30px;
}It's like placing a treasure in a specific spot within a pyramid chamber. The treasure doesn't affect the positions of other objects - it simply lies where you put it.
position: fixed
An element with position: fixed is positioned relative to the browser window (viewport). It stays in its place even while scrolling the page.
.navigation {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #2C1810;
z-index: 100;
}Think of it like a star in the Egyptian sky - no matter where you walk in the desert, the star is always in the same place in the firmament. Similarly, a fixed element is always visible at the same point on the screen.
position: sticky
A sticky element is a hybrid between relative and fixed. It behaves like relative until the user scrolls the page to a certain point - then it "sticks" and behaves like fixed.
.section-header {
position: sticky;
top: 0;
background-color: #FFD700;
padding: 10px;
}It's like a guard watching the entrance to the pharaoh's chamber. They stand in their place, but when you approach the threshold, they block the passage and don't move further.
z-index and Stacking Context
When elements overlap (which happens with absolute, relative, fixed, and sticky positioning), the z-index property determines their order.
.background-layer { position: absolute; z-index: 1; }
.middle-layer { position: absolute; z-index: 2; }
.front-layer { position: absolute; z-index: 3; }Imagine an Egyptian wall painting consisting of layers: first the desert background (z-index: 1), then pyramids (z-index: 2), and on top the pharaoh figures (z-index: 3). The higher the z-index, the closer to the viewer (on top) the element is.
Important z-index rules:
- Only works on elements with
positionother thanstatic(and on flex and grid items, which you'll meet in the next modules) - Elements without
z-indexhave a default value ofauto(equivalent to 0) - Higher
z-indexmeans displaying "above" other elements - Elements with the same
z-indexdisplay in the order they appear in HTML
The overflow Property
When an element's content is larger than the element itself, the overflow property controls what happens to it.
.scroll-container { width: 300px; height: 200px; overflow: hidden; }
.scroll-container-visible { overflow: scroll; }
.scroll-container-auto { overflow: auto; }overflow: hidden- hides content going beyond the element (like a pyramid wall behind which the interior isn't visible)overflow: scroll- always shows scrollbarsoverflow: auto- scrollbars appear only when content is larger than the container
Float and Clearfix (Historical Context)
Before the era of Flexbox and Grid, the float property was used to create column layouts. Although this approach is outdated today, it's worth knowing because you'll encounter it in older code.
.column-left { float: left; width: 50%; }
.column-right { float: right; width: 50%; }
/* Clearfix - fixes the float problem */
.container::after { content: ""; display: table; clear: both; }float removes an element from the normal flow and "floats" it to the left or right side of the container. The problem is that the parent container "forgets" about the dimensions of children with float - that's why clearfix is needed.
Today, instead of float, we use display: flex or display: grid, which are much simpler and more predictable. Float remains useful mainly for wrapping text around images.
Summary
| Value | Flow | Positioning relative to | Scrolling |
|---|---|---|---|
| static | normal | - | yes |
| relative | normal | own position | yes |
| absolute | removed | positioned ancestor | yes |
| fixed | removed | viewport | no |
| sticky | normal (its space stays) | viewport (after reaching threshold) | sticks |
Mohamed says: "Just as pyramid builders had to precisely plan the placement of every stone, you must understand CSS positioning. With position and z-index, you can place elements exactly where you want - like an architect of the Karnak temple!"
Code for this lesson: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Positioning - Pyramid</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>CSS Positioning in Egypt</h1>
<h2>1. position: relative</h2>
<div class="demo-box">
<div class="stone">Normal</div>
<div class="stone moved">Moved (relative)</div>
<div class="stone">Normal</div>
</div>
<h2>2. position: absolute</h2>
<div class="pyramid-room">
<div class="treasure treasure-1">Treasure 1</div>
<div class="treasure treasure-2">Treasure 2</div>
<div class="treasure treasure-3">Treasure 3</div>
</div>
<h2>3. z-index - painting layers</h2>
<div class="painting">
<div class="layer layer-bg">Background (z-index: 1)</div>
<div class="layer layer-mid">Pyramids (z-index: 2)</div>
<div class="layer layer-front">Pharaoh (z-index: 3)</div>
</div>
<h2>4. overflow</h2>
<div class="scroll-demo">
<p>This text is too long for its container. Overflow: auto adds a scrollbar when the content does not fit in the element. Ancient Egyptians wrote long texts on papyrus scrolls - CSS allows us to scroll content in a similar way!</p>
</div>
</body>
</html>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is the default value of the position property in CSS?
2. Relative to what is an element with position: absolute positioned?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Uncomment the CSS rules in the style.css file to arrange the treasures in the pharaoh's chamber. The .chamber container needs position: relative, and each .item needs position: absolute with the appropriate top/left/right/bottom values.
- Vertical ordering
Arrange the elements from displayed lowest (at the back) to displayed highest (at the front):
- Click in order
Build a CSS declaration that sets absolute positioning:
- Code editor
Uncomment the CSS rules so the .temple-header sticks to the top of the page (position: sticky, top: 0, z-index: 100). Also set .room-title as sticky with top: 60px. Add position: relative to .badge-container and position: absolute to .badge.