Positioning in CSS helps you control where elements appear on a web page. Letβs break it down in a simple and fun way! π
position Property: What Does It Do?The position property tells the browser how an element should be placed. Here are the main values:
| Position Value | Behavior π |
|---|---|
static (default) |
Follows normal document flow π |
relative |
Moves relative to itself π§© |
absolute |
Moves relative to the nearest positioned ancestor π¦ |
fixed |
Stays in the same spot on the screen π₯οΈ |
sticky |
Switches between relative and fixed while scrolling π |
Behavior:
β Follows the normal document flow.
β Does NOT respond to top, left, right, or bottom.
div {
position: static; /* Default positioning */
}
π When to use? If you don't need special positioning.
Behavior:
β Moves relative to its original position.
β Other elements donβt move to fill the space.
div {
position: relative;
top: 10px; /* Moves 10px down */
left: 20px; /* Moves 20px right */
}
π When to use? When you want a slight adjustment without affecting other elements.