Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML

    What are the different types of HTML tags?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor
  • JavaScript

    What is one-way data binding in React?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Difference between document.createElement and document.createElementFragement in JavaScript?

    By Chief Editor

    What is the Event Loop in JavaScript?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor
  • Frontend Interview

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor
  • Backend Interview

    What is localization in React?

    By Chief Editor

    What is Higher Order Component in React?

    By Chief Editor

    What is a Higher-Order Component (HOC) in React?

    By Chief Editor

    What is state in React, and what are its properties?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is NodeJS and its main features?

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • πŸ”₯
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
Β© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog Difference Between position: relative and position: absolute in CSS
CSSFrontend Interview

Difference Between position: relative and position: absolute in CSS

Chief Editor
Last updated: February 16, 2025 6:40 pm
Chief Editor
Share
SHARE
Propertyposition: relativeposition: absolute
DefinitionThe element is positioned relative to its original position in the normal document flow.The element is positioned relative to its nearest positioned ancestor (or the <html> if no ancestor has position: relative, absolute, or fixed).
Affects Document Flow?βœ… Yes, it remains in the document flow and occupies space.❌ No, it is removed from the document flow, meaning other elements behave as if it does not exist.
Reference PointThe original position of the element.The nearest ancestor with position: relative, absolute, or fixed. If none, it is positioned relative to the <html> (viewport).
Usage of top, right, bottom, leftMoves the element from its normal position without affecting surrounding elements.Moves the element relative to the nearest positioned ancestor, completely detached from the normal document flow.
Exampleβœ… Used when you want to shift an element slightly from its normal position while keeping the surrounding layout intact.βœ… Used when you need to place an element precisely, often for overlays, tooltips, dropdowns, etc.

Example of position: relative

cssCopyEdit.container {
  position: relative;
  width: 300px;
  height: 200px;
  background: lightblue;
}

.child {
  position: relative;
  top: 20px;
  left: 30px;
  background: yellow;
}

πŸ“Œ Result:

  • The .child element moves 20px down and 30px right from its normal position inside .container.
  • Other elements are not affected.

Example of position: absolute

cssCopyEdit.container {
  position: relative; /* Acts as the reference for absolute positioning */
  width: 300px;
  height: 200px;
  background: lightblue;
}

.child {
  position: absolute;
  top: 20px;
  left: 30px;
  background: yellow;
}

πŸ“Œ Result:

  • The .child element is positioned 20px down and 30px right, but relative to .container (not its normal position).
  • If .container did not have position: relative, the .child would be positioned relative to the entire page (viewport).

Key Takeaways

  • relative keeps the element in flow, moving it slightly from its original position.
  • absolute removes the element from flow and positions it relative to the closest positioned ancestor.

Share This Article
Email Copy Link Print
Previous Article What is Symentic HTML What is Symentic HTML?
Next Article What is Block level Element and Inline Level Element?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

What is state in React, and what are its properties?

State in React is a built-in object that is used to store and manage data…

By Chief Editor

What are the different types of HTML tags?

HTML tags are the building blocks of an HTML document. They define the structure and…

By Chief Editor

What is React Router Dom in React?

React Router DOM is a popular library in React that is used to handle navigation…

By Chief Editor

You Might Also Like

What is Symentic HTML
Frontend InterviewHTML

What is Symentic HTML?

By Chief Editor
Frontend InterviewJavaScript

Explain Deep Copy and Shallow Copy in JavaScript.

By Chief Editor
CSS

What is Flex Box in CSS?

By Chief Editor
CSS

Difference between display none and visibility hidden in CSS?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance β€” all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach β€” we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today β€” because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

Β©2025Β  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?