How to Set Position Absolute but Relative to Parent in CSS ?
Last Updated : 25 Jul, 2024
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:
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