Positions in CSS
static position | Relative position | Absolute Position | Fixed position | Sticky position
Table of contents
No headings in the article.
Positioning is an essential aspect of web development that allows you to control the position of HTML elements on a webpage. CSS (Cascading Style Sheets) provides different types of positioning techniques that you can use to position HTML elements accurately.
In this blog, we will discuss the different types of positioning in CSS and how you can use them effectively to position your HTML elements.
Types of Positioning
There are five types of positioning in CSS:
- Static Positioning
This is the default positioning for all HTML elements. In static positioning, the HTML element is positioned according to the normal flow of the document. You cannot use the top, bottom, left, or right properties with static positioning.
Example:
div {
position: static;
}
- Relative Positioning
In relative positioning, an HTML element is positioned relative to its original position in the document. You can use the top, bottom, left, or right properties to move the element from its original position.
Example:
div {
position: relative;
top: 10px;
left: 20px;
}
- Absolute Positioning
In absolute positioning, an HTML element is positioned relative to its closest positioned ancestor. If no positioned ancestor is found, the element is positioned relative to the document. You can use the top, bottom, left, or right properties to position the element.
Example:
div {
position: absolute;
top: 50px;
left: 100px;
}
- Fixed Positioning
In fixed positioning, an HTML element is positioned relative to the browser window. The element will remain in the same position even if the page is scrolled. You can use the top, bottom, left, or right properties to position the element.
Example:
div {
position: fixed;
top: 10px;
right: 10px;
}
- Sticky Positioning
In sticky positioning, an HTML element is positioned based on the user's scroll position. The element will remain in its normal position until the user scrolls to a certain point, after which it becomes fixed in place.
Example:
div {
position: sticky;
top: 0;
}
Conclusion:
Positioning is an essential aspect of web development that allows you to position HTML elements accurately on a webpage. CSS provides different types of positioning techniques that you can use to control the position of your HTML elements. By using these techniques effectively, you can create visually appealing webpages that are easy to navigate and use.