CSS position Property
The CSS position
property is used to control the positioning of an element within its containing block. It can take on several values, each of which affects the element's positioning differently. Here’s a rundown of the most common values:
1. static
- Default Value: The default positioning method for all elements. Elements are positioned according to the normal flow of the document.
- Behavior: The
top
,right
,bottom
, andleft
properties have no effect.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Static Position</title>
<style>
.box {
position: static;
background-color: lightblue;
width: 200px;
height: 100px;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="box">Static Position</div>
</body>
</html>
2. relative
- Behavior: The element is positioned relative to its normal position. You can use the
top
,right
,bottom
, andleft
properties to adjust its position from where it would normally be. - Effect: This allows you to shift the element without changing the layout around it.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Relative Position</title>
<style>
.box {
position: relative;
top: 20px;
left: 30px;
background-color: lightgreen;
width: 200px;
height: 100px;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="box">Relative Position</div>
</body>
</html>
3. absolute
- Behavior: The element is positioned relative to its nearest positioned ancestor (i.e., an ancestor element with a position other than
static
). If no such ancestor exists, it is positioned relative to the initial containing block (usually the viewport). - Effect: The element is removed from the normal document flow, and the
top
,right
,bottom
, andleft
properties specify its position.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Absolute Position</title>
<style>
.container {
position: relative;
height: 200px;
background-color: lightgray;
border: 1px solid black;
}
.box {
position: absolute;
top: 10px;
right: 20px;
background-color: lightcoral;
width: 100px;
height: 50px;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Absolute Position</div>
</div>
</body>
</html>
4. fixed
- Behavior: The element is positioned relative to the viewport, and it stays in the same place even when the page is scrolled.
- Effect: The element is removed from the normal document flow, and the
top
,right
,bottom
, andleft
properties specify its position within the viewport.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fixed Position</title>
<style>
.box {
position: fixed;
bottom: 10px;
right: 10px;
background-color: lightgoldenrodyellow;
width: 150px;
height: 75px;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="box">Fixed Position</div>
<p>Scroll down to see the fixed position.</p>
<div style="height: 2000px;"></div>
</body>
</html>
5. sticky
- Behavior: The element is treated as relative until it crosses a specified threshold (using
top
,right
,bottom
, orleft
), at which point it is treated as fixed. - Effect: The element sticks to the specified position when the scroll reaches that threshold, but scrolls away with the rest of the content when you scroll past it.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sticky Position</title>
<style>
.box {
position: sticky;
top: 0;
background-color: lightpink;
width: 100%;
height: 50px;
border: 1px solid black;
z-index: 1000;
}
.content {
height: 2000px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="box">Sticky Position</div>
<div class="content"></div>
</body>
</html>
Each value of the position
property serves different use cases, so choosing the right one depends on the layout needs and behavior you want to achieve for your elements.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.