Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App

CSS Positioning Elements

Last Updated : 04 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 PropertyDescription
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.

RelativeElements 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.

StickyCombines 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.


S

Saptarshi_Das
Improve
Article Tags :
  • Web Technologies
  • CSS
  • CSS-Properties

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences