CSS positioning defines how elements are placed within a web page. It allows you to control the layout, stacking order, and alignment of elements. The primary positioning types in CSS are:
Position Property | Description |
---|
Fixed | An element with position: fixed property remains in the same position relative to the viewport even when the page is scrolled. |
Static | Default positioning method. Elements with position: static are positioned according to the normal flow of the document. |
Relative | Elements with position: relative are positioned relative to their normal position in the document flow. Other elements will not fill the gap left by this element when adjusted. |
Absolute | Positioned concerning its nearest non-static ancestor. Elements with position: absolute are taken out of the normal document flow. |
Sticky | Combines features of position: relative and position: fixed. The element is treated as position: relative until it reaches a specified threshold, then it becomes position: fixed. |
1. Static Positioning
Static is the default position of an element. It does not accept properties like top, left, right, or bottom.
HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> div { border: 1px solid black; padding: 10px; margin: 10px; } </style> <!--Driver Code Starts--> </head> <body> <div>Box 1</div> <div>Box 2</div> </body> </html> <!--Driver Code Ends-->
- The boxes follow the normal flow of the document.
- There’s no overlap or displacement of elements.
2. Relative Positioning
Relative positioning places an element relative to its normal position. You can move it using top, left, right, or bottom.
HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> div { border: 1px solid black; padding: 10px; margin: 10px; } .relative { position: relative; top: 20px; left: 30px; } </style> <!--Driver Code Starts--> </head> <body> <div>Box 1</div> <div class="relative">Box 2</div> </body> </html> <!--Driver Code Ends-->
Box 2 is shifted 20px down and 30px to the right from its normal position.
3. Absolute Positioning
Absolute positioning removes the element from the document flow and places it relative to the nearest ancestor with a positioning context (relative, absolute, or fixed).
HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> .container { position: relative; width: 300px; height: 200px; border: 2px solid black; margin: 20px auto; } .absolute { position: absolute; top: 50px; left: 50px; background-color: pink; padding: 10px; } </style> <!--Driver Code Starts--> </head> <body> <div class="container"> <p>Container with Relative Positioning</p> <div class="absolute">Absolutely Positioned Element</div> </div> </body> </html> <!--Driver Code Ends-->
- The pink box (.absolute) is positioned 50px down and 50px right within the .container.
- It does not affect other elements in the flow.
4. Fixed Positioning
Fixed positioning removes the element from the flow and positions it relative to the viewport. It remains in place even when the page scrolls.
HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> .fixed { position: fixed; top: 10px; right: 10px; background-color: lightgreen; padding: 20px; border: 2px solid black; } .content { height: 1200px; padding: 10px; } </style> <!--Driver Code Starts--> </head> <body> <h2>Fixed Positioning Example</h2> <div class="fixed">Fixed Box</div> <div class="content"> <p>Scroll down to see that the fixed box stays in place.</p> <p>This content simulates a long page.</p> </div> </body> </html> <!--Driver Code Ends-->
The fixed element stays at the top-right corner of the viewport even as the user scrolls.
5. Sticky Positioning
Sticky positioning switches between relative and fixed based on the scroll position.
HTML <!--Driver Code Starts--> <html> <head> <!--Driver Code Ends--> <style> .sticky { position: sticky; top: 0; background-color: yellow; padding: 10px; } </style> <!--Driver Code Starts--> </head> <body> <div class="sticky">I am sticky</div> <p>Scroll down to see the effect.</p> <div style="height: 1000px;"></div> </body> </html> <!--Driver Code Ends-->
The sticky element stays at the top of the viewport as you scroll but only within its containing element.
Similar Reads
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
CSS Introduction CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable.It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the
5 min read
CSS Syntax CSS is written as a rule set, which consists of a selector and a declaration block. The basic syntax of CSS is as follows:The selector is a targeted HTML element or elements to which we have to apply styling.The Declaration Block or " { } " is a block in which we write our CSS.HTML<html> <h
2 min read
CSS Selectors CSS Selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.Basic CSS Selectors: These are used to target elements by tag, .class, or # ID for fundamental styling needs.Combinato
7 min read
CSS Comments CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works.
2 min read
CSS Colors CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more.You can try different formats of colors here- #content-iframe
6 min read
CSS Borders Borders in CSS are used to create a visible outline around an element. They can be customized in terms ofWidth: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ he
5 min read
CSS Margins CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability.Syntax:body { margin: value;}HTML<html> <head>
4 min read
CSS Height and Width Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto.Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centim
4 min read
CSS Outline CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element.Syntaxselector{ outline: outline-width outline-type outline-color
4 min read