Normally the browser places elements one after another in the order of the HTML, which is called the normal flow. CSS can move an element out of that flow when the design needs it.
The Position Property
The position property has five values, which Table 2.6 summarises. With static, the default, the element follows the normal flow. With relative, the element stays in the flow but can be shifted from its normal place. With absolute, the element is taken out of the flow and placed relative to its nearest positioned ancestor, or to the page if there is none. With fixed, the element is placed relative to the browser window and stays in place while the page scrolls. With sticky, the element scrolls normally until it reaches a set point, and then sticks there.
Definition: Positioning. The way an element is placed on the page, set with the position property.
Table 2.6: The five values of position
| Value | Placed relative to | Typical use |
|---|---|---|
| static | Normal flow | Default |
| relative | Its normal place | Small shifts, parent for absolute |
| absolute | Nearest positioned ancestor | Badges, labels |
| fixed | Browser window | Fixed menus, chat buttons |
| sticky | Scroll position | Headers that stay visible |
Offsets and Stacking
The properties top, right, bottom and left set how far a positioned element is moved. Overlapping elements are stacked in order, and the z-index property decides which one is on top. A larger number appears above a smaller one.
Floating and Clearing
The float property moves an element to the left or right edge of its container and lets text flow around it. It was designed for pictures in articles. A floated element is taken out of the normal flow, so the following content may run up beside it. The property clear: both makes an element start below any floated elements. Floats were once used for whole-page layouts, but Flexbox and Grid have replaced them for that job.
Definition: Float. A property that moves an element left or right so that text wraps around it.
Example 2.6: A sticky header and a floated image
<style>
header { position: sticky; top: 0; background: #1F3864;
color: white; padding: 8px; }
.pic { float: left; width: 60px; height: 60px; background: #70AD47;
margin: 0 10px 5px 0; }
</style>
<header>Site header (sticky)</header>
<div class="pic"></div>
<p>The green square floats to the left, and this text flows
around it. Longer text wraps beneath the square.</p>
Output in the browser:
Common mistakes
- Using absolute positioning without a positioned parent, so that the element is placed against the whole page.
- Forgetting the offsets, so that position has no visible effect.
- Floating many elements for page layout instead of using Flexbox or Grid.
- Forgetting to clear floats, so that a container collapses around them.
Key takeaways
- Position has five values: static, relative, absolute, fixed and sticky.
- Absolute positioning uses the nearest positioned ancestor as its reference.
- Fixed elements stay in place on screen, and sticky elements stick after scrolling to a point.
- Float lets text wrap around an element, and clear ends the wrapping.
Practice questions
- Which position value keeps an element visible while the page scrolls?
- What does z-index control?
- What is the purpose of clear: both?
Answers to the practice questions
- fixed, and sticky after it reaches its set point.
- Which of several overlapping elements is shown on top.
- It makes an element start below any floated elements.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit II
