Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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
Next Article:
Relative vs Absolute vs Fixed Position in CSS
Next article icon

How to Set Position Absolute but Relative to Parent in CSS ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The position: relative property changes the position of the element relative to the parent. The position-relative property can be applied to any section then the elements in that section are positioned in the normal flow of the document. It is relative to the original position with respect to the parent. To modify the position of elements, the offset can be applied to the elements by specifying the left, right, top, and bottom.

Syntax:

.classname{
top: 10px;
bottom: 10px;
left: 10px;
right: 10px;
}

Properties: 

  • left: If the left side offset is applied to the particular section or element then it moves the right side of the element's parent container.
  • right: If the right side offset is applied to the element then it moves left to the element's parent container.
  • top: If the top side offset is applied to the element, it moves the element to the bottom of the element's parent container.
  • bottom: If the bottom side offset is applied to the element, it moves the element to the top of the element's parent container.

Approach: Here we use position: relative

syntax:

.classname {
position: relative;
left: 40px;
}

Example 1: In this example, we are using the above-explained approach.

HTML
<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible"            content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">     <title>         How to Set Position Absolute but         Relative to Parent in CSS ?     </title>      <style>         .child {             position: relative;             left: 40px;         }          .one {             background-color: black;             width: 60px;             height: 60px;             padding: 10px;             margin: 10px;         }          .two {             background-color: aqua;             width: 60px;             height: 60px;             padding: 10px;             margin: 10px;         }          p {             color: darkgreen;             font-weight: bolder;             font-size: xx-large;         }     </style> </head>  <body>     <div class="parent">         <div class="child">             <p>GeekForGeeks</p>         </div>         <div class="child1 one"></div>         <div class="chile2 two"></div>     </div> </body> </html> 

Output: The GeeksforGeeks text is positioned according to the parent element as it is relative to it and an offset of 40px is applied.

 

Approach: In the below example, we added other classes for your better understanding, if there is a position relative then you can change the position of the element respective to its parent.

Syntax:

:classname:hover 
//it applies the property when we hover on that particular class

Example: In this example, we are using the above-explained approach.

HTML
<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible"            content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">     <title>CSS tutorial</title>     <!-- inline css -->     <style>         .parent {             margin: 30px;             margin-top: 40px;             background-color: floralwhite;         }          .child:hover {             position: relative;             left: 35px;             cursor: pointer;          }          p {             font-weight: bolder;             font-size: larger;         }          .main {             color: darkgreen;             font-size: xx-large;             font-weight: bolder;         }     </style> </head>  <body>     <div class="parent">         <p>             Hello programmer welcome to GFG         </p>         <div class="child">             <p class="main">                 GeekForGeeks             </p>         </div>         <p>hover above</p>     </div> </body> </html> 

Output: Here the child element is changing its position and it is changed relative to the parent

 

position: absolute (Always relative to parent): The element is removed from normal document flow no space is allocated for the element if the absolute property is applied to that element, If the absolute property is applied to the element its parent should be relative, and it is positioned automatically (top-left-corner), It should be positioned relative to its closest positioned ancestor if at all there is no relative element the ancestor would be its body tag, Simply the position: relative property Is applied to its body. Its position is determined by the classes like top, bottom, left, and right for offset.

Approach: Here the position absolute element is removed from the document flow other elements on the page are affected by it.

Syntax:

.parent_classname{
position:relative;
}
.child_classname{
position:absolute;
}

Example 1: Here we apply the position of absolute property to the first child. (child_one) and second.child

HTML
<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible"             content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">     <link rel="stylesheet" href="style.css">      <title>         How to Set Position Absolute but         Relative to Parent in CSS ?     </title>     <!-- inline CSS -->     <style>         .parent {             position: relative;         }          .child_one {             position: absolute;             background-color: black;             width: 25vw;             height: 30vh;         }          .child_two {             background-color: bisque;             position: absolute;             cursor: pointer;         }          p {             color: white;             font-size: large;             font-weight: bold;         }          .main {             font-weight: bolder;             font-size: xx-large;             color: darkgreen;         }     </style> </head>  <body>     <div class="parent">         <div class="child_one">             <p>                 hello programmer Welcome to GFG             </p>         </div>         <div class="child_two">             <p class="main">                 GeekForGeeks             </p>         </div>     </div> </body> </html> 

Output: When the position absolute property is applied to the second child it is overlayed on the other element in the top left corner, which is not in the document flow

 

Approach:  For your better understanding, we added extra properties as a child: hover

Syntax:

.parent_class {
position: relative;
}

.child_one:hover {
position: absolute;
cursor: pointer;
}

.child_two {
background-color: bisque;
position: absolute;
cursor: pointer;
}

Example 2: In this example, we are using child:hover property

HTML
<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible"           content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">     <link rel="stylesheet" href="style.css">     <title>         How to Set Position Absolute but         Relative to Parent in CSS ?     </title>     <!-- inline CSS -->     <style>         .parent {             position: relative;         }          .child_one {             background-color: black;             width: 30vw;             height: 40vh;         }          .child_one:hover {             position: absolute;             cursor: pointer;         }          .child_two {             background-color: bisque;             position: absolute;             cursor: pointer;         }          p {             color: white;             font-size: large;             font-weight: bold;         }          .main {             font-weight: bolder;             font-size: xx-large;             color: darkgreen;         }     </style> </head>  <body>     <div class="parent">         <div class="child_one">             <p>                 hello programmer Welcome to GFG             </p>         </div>         <div class="child_two">             <p class="main">                 GeekForGeeks             </p>         </div>     </div> </body> </html> 

Output:


Next Article
Relative vs Absolute vs Fixed Position in CSS
author
shivanampalli
Improve
Article Tags :
  • Web Technologies
  • CSS
  • CSS-Properties
  • CSS-Questions

Similar Reads

  • How to set fixed position but relative to container in CSS ?
    In CSS, setting an element’s position as "fixed" relative to a container, rather than the entire viewport, can be done by using a combination of CSS properties. While position: fixed works relative to the viewport, there are other methods like position: sticky and position: absolute that can help ac
    3 min read
  • Relative vs Absolute vs Fixed Position in CSS
    CSS positioning is a fundamental concept in web design and development that allows precise control over how elements are arranged on a webpage. There are three main types of CSS positioning: relative, absolute, and fixed positioning. What is Relative Positioning?Relative Positioning is a CSS techniq
    4 min read
  • How to position absolute rendering the button in a new line ?
    The position CSS property sets how an element is positioned in a document. Absolute positioning allows you to place your element where you want. The positioning is done relative to the first absolutely positioned element. The element we position absolute will be removed from the normal flow of the d
    2 min read
  • What is the Position of an element relative to its container in CSS ?
    In this article, we will learn what is the position of an element relative to its container. The position property in CSS tells about the method of positioning for an element or an HTML entity. There are five different types of position properties available in CSS: Fixed: Any HTML element with posit
    4 min read
  • How to Set Position of an Image in CSS?
    To change the position of an image in CSS, properties like object position and float are used to control the placement of an image within its container. 1. Using object-position PropertyThe object-position property in CSS is used to set the position of an image within its container when using the ob
    2 min read
  • How to set a rotated element's base placement in CSS ?
    In this article, we will learn how to set a rotated element's base placement in CSS. This can be used to rotate an element from a certain point to its origin. Here we use the transform-origin property. The CSS transform-origin property defines the origin point around which an element is transformed
    2 min read
  • How to center the absolutely positioned element in div using CSS?
    Centering an absolutely positioned element in a div using CSS involves adjusting the position both vertically and horizontally. By using a combination of the top, left, and transform properties, we can ensure the element is perfectly centered within its container. This method creates a balanced and
    3 min read
  • What is Position Relative in CSS?
    The position property in CSS can determine how an element is positioned on the webpage. The relative value for this property can allow you to position the element relative to its normal position in the document flow. It means that you can move the element from its default position without affecting
    2 min read
  • How to use the position property in CSS to align elements ?
    In this article, we will learn how to use the position property in CSS to align elements. We can align elements using position property using CSS with some helper property of it. Approach: The position property is used to set the position of the element. We can align elements using position property
    2 min read
  • How to Fix Button Position in CSS?
    Position of elements on a webpage allows us to place elements in any particular position we want. The button is an interactive element that requires a specific placement for better usability. We can place buttons using several Position methods, in CSS each serving different purposes. Below are some
    3 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