The position property in CSS defines how an element is positioned within a document.
Static Positioning
Static positioning is the default value for the position property. Elemnets with position: static are positioned according to the normal document flow, appearing where they naturally would. This value is often omitted in practice.
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Static Positioning Example</title>
</head>
<body>
<img src="img/core-java.jpg" style="position: static;">
</body>
</html>
Relative Positioning
Relative positioning moves an element relative to its original position in the document flow. It accepts left, right, top, and bottom properties to specify offsets.
Key Points:
- The element's original space is preserved, meaning other elements do not shift to fill the gap.
- Typically, only one horizontal (
leftorright) and one vertical (toporbottom) property are used simultaneously. - Priority:
topandlefttake precedence overbottomandright.
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Relative Positioning Example</title>
</head>
<body>
<div style="width: 500px; height: 500px;">
<div style="width: 100px; height: 100px;"></div>
<div style="width: 100px; height: 100px; position: relative; bottom: 10px; right: 20px;"></div>
<div style="width: 100px; height: 100px;"></div>
</div>
</body>
</html>
Use Cases for Relative Positioning:
- Minor adjustments to an element's position.
- Serving as a container for absolutely positioned child elements.
Z-Index Property
The z-index property controls the stacking order of positioned elements. A higher value brings an element to the front.
Note: z-index only applies to elements with a position value other than static.
Example with Z-Index:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Z-Index Example</title>
</head>
<body>
<div style="width: 500px; height: 500px;">
<div style="width: 100px; height: 100px; position: relative; left: 10px; z-index: 10;"></div>
<div style="width: 100px; height: 100px; position: relative; bottom: 10px; right: 20px; z-index: 90;"></div>
<div style="width: 100px; height: 100px;"></div>
</div>
</body>
</html>
Absolute Positioning
Absolute positioning removes an element from the normal document flow and positions it relative to its nearest positioned ancestor (or the document body if none exists).
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Absolute Positioning Example</title>
<style>
.container {
width: 500px;
height: 500px;
background-color: pink;
margin-left: 300px;
}
.absolute-box {
width: 100px;
height: 100px;
background-color: cornflowerblue;
position: absolute;
left: 30px;
top: 50px;
}
.normal-box {
width: 100px;
height: 100px;
background-color: coral;
}
</style>
</head>
<body>
<div class="container">
<div class="absolute-box">111</div>
<div class="normal-box">222</div>
</div>
</body>
</html>
In this example, the blue box is positioned relative to the document body, freeing its original space for the orange box to occupy.
Common Practice: To position an element within a specific container, set the parent to position: relative and the child to position: absolute.
Example with Parent Relative Positioning:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Absolute with Relative Parent Example</title>
<style>
.container {
width: 500px;
height: 500px;
background-color: pink;
margin-left: 300px;
position: relative;
}
.absolute-box {
width: 100px;
height: 100px;
background-color: cornflowerblue;
position: absolute;
left: 30px;
top: 50px;
}
.normal-box {
width: 100px;
height: 100px;
background-color: coral;
}
</style>
</head>
<body>
<div class="container">
<div class="absolute-box">111</div>
<div class="normal-box">222</div>
</div>
</body>
</html>
Summary:
- An absolutely positioned element searches up its ancestor chain for a positioned element (with
positionset torelative,absolute, orfixed). - If no such ancestor is found, it positions relative to the document body.
- The element's original space is released, allowing other elements to occupy it.
- Recommended approach: Use
position: relativeon the parent andposition: absoluteon the child.
Fixed Positioning
Fixed positioning anchors an element relative to the browser viewport, keeping it in place during scrolling.
Use Case: Cerating elements like sidebars or navigation bars that remain visible as the user scrolls.
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Fixed Positioning Example</title>
<style>
.fixed-sidebar {
width: 50px;
height: 400px;
background-color: cadetblue;
position: fixed;
right: 0px;
top: 300px;
}
</style>
</head>
<body>
<div class="fixed-sidebar"></div>
<p>Content paragraph 1</p>
<p>Content paragraph 2</p>
<!-- Additional paragraphs to enable scrolling -->
</body>
</html>