๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A

โ“ What is position in CSS?

The position property in CSS defines how an element is placed in the document layout. It controls whether an element follows the normal flow or is manually positioned using top, left, right, and bottom.


This content originally appeared on DEV Community and was authored by Vigneshwaralingam

โ“ What is position in CSS?

The position property in CSS defines how an element is placed in the document layout. It controls whether an element follows the normal flow or is manually positioned using top, left, right, and bottom.

๐Ÿ•’ When do you use position?

Use it when you want to:

  • Fix something on screen (like a navbar or button)
  • Layer items (like overlays, modals, tooltips)
  • Place elements relative to a parent or the page

๐Ÿ’ก Why is position important?

  • Helps create dynamic, interactive UIs
  • Essential for responsive layout behavior
  • Works with z-index, top, left, etc. for fine control
  • Used in modals, tooltips, sticky headers, buttons, etc.

๐ŸŒ Where is it applied?

You can apply it to any block, inline, or inline-block element:

.box {
  position: relative;
  top: 20px;
}

๐Ÿ” Which position values are there?

Value Description
static Default value. Element follows normal flow.
relative Moves element relative to its original place.
absolute Positioned relative to nearest positioned ancestor.
fixed Positioned relative to the browser window. Stays fixed while scrolling.
sticky Switches between relative and fixed based on scroll position.

โš™๏ธ How does each one work? + Example

๐Ÿ”น 1. static (Default)

.box {
  position: static;
}

โžก๏ธ No special behavior. Follows the natural document flow.

๐Ÿ”น 2. relative

.box {
  position: relative;
  top: 20px;
}

โžก๏ธ Moves 20px down from its original position, without affecting surrounding elements.

๐Ÿ”น 3. absolute

.container {
  position: relative;
}
.box {
  position: absolute;
  top: 10px;
  left: 20px;
}

โžก๏ธ .box is positioned 10px from the top and 20px from the left of .container.

๐Ÿ”น 4. fixed

.button {
  position: fixed;
  bottom: 10px;
  right: 10px;
}

โžก๏ธ Sticks the button to the bottom-right of the screen even while scrolling.

๐Ÿ”น 5. sticky

.header {
  position: sticky;
  top: 0;
}

โžก๏ธ .header scrolls normally but sticks to the top when it reaches it.

๐Ÿ” Difference Between All Positions

Property Scrolls with Page Affects Flow Relative To Use Case
static โœ… Yes โœ… Yes Document flow Default behavior
relative โœ… Yes โœ… Yes Its original position Slight offset
absolute โŒ No โŒ No Nearest positioned parent Overlays, tooltips
fixed โŒ No โŒ No Browser window Sticky footer, navbar
sticky โœ… Yes (then no) โœ… Yes Scroll position Sticky headers

๐Ÿง  Interview Questions & Answers โ€“ CSS position

โœ… 1. What is the default position of an element in CSS?

Answer:
static โ€” which means the element follows the normal document flow and cannot be positioned using top, left, etc.

โœ… 2. Whatโ€™s the difference between relative and absolute positioning?

Answer:

  • relative: moves the element based on its original location, keeping its space in the layout.
  • absolute: removes the element from the layout and positions it relative to the nearest positioned ancestor.

โœ… 3. What is position: fixed used for?

Answer:
To fix an element in the viewport โ€” it doesnโ€™t move when the page scrolls. Common use: floating buttons, sticky footers.

โœ… 4. What is the use of position: sticky?

Answer:
It allows an element to scroll with the content but stick to a position (like top: 0) when it reaches it. It's useful for sticky headers and menus.

โœ… 5. What does โ€œpositioned ancestorโ€ mean?

Answer:
Any parent element with a position other than static (i.e., relative, absolute, or fixed) is called a positioned ancestor.

โœ… 6. If a parent has position: relative, and the child has position: absolute, what happens?

Answer:
The child will position itself relative to the parentโ€™s edges, not the whole page.

โœ… 7. Can an inline element be positioned?

Answer:
Yes, but it's better to convert it to inline-block or block before applying position styles for better layout control.

โœ… 8. What happens if you apply top: 0 to a static element?

Answer:
Nothing. top, left, right, bottom only work with relative, absolute, fixed, or sticky positions.


This content originally appeared on DEV Community and was authored by Vigneshwaralingam


Print Share Comment Cite Upload Translate Updates
APA

Vigneshwaralingam | Sciencx (2025-06-11T17:22:43+00:00) ๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A. Retrieved from https://www.scien.cx/2025/06/11/%f0%9f%93%8c-css-position-property-what-when-why-where-which-how-examples-interview-qa/

MLA
" » ๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A." Vigneshwaralingam | Sciencx - Wednesday June 11, 2025, https://www.scien.cx/2025/06/11/%f0%9f%93%8c-css-position-property-what-when-why-where-which-how-examples-interview-qa/
HARVARD
Vigneshwaralingam | Sciencx Wednesday June 11, 2025 » ๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A., viewed ,<https://www.scien.cx/2025/06/11/%f0%9f%93%8c-css-position-property-what-when-why-where-which-how-examples-interview-qa/>
VANCOUVER
Vigneshwaralingam | Sciencx - » ๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/11/%f0%9f%93%8c-css-position-property-what-when-why-where-which-how-examples-interview-qa/
CHICAGO
" » ๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A." Vigneshwaralingam | Sciencx - Accessed . https://www.scien.cx/2025/06/11/%f0%9f%93%8c-css-position-property-what-when-why-where-which-how-examples-interview-qa/
IEEE
" » ๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A." Vigneshwaralingam | Sciencx [Online]. Available: https://www.scien.cx/2025/06/11/%f0%9f%93%8c-css-position-property-what-when-why-where-which-how-examples-interview-qa/. [Accessed: ]
rf:citation
» ๐Ÿ“Œ CSS `position` Property โ€“ What, When, Why, Where, Which, How + Examples & Interview Q&A | Vigneshwaralingam | Sciencx | https://www.scien.cx/2025/06/11/%f0%9f%93%8c-css-position-property-what-when-why-where-which-how-examples-interview-qa/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.