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

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

Positioning elements absolutely relative to their parent is quite difficult when working with the CSS positioning properties. By default, the child element is positioned to its nearest ancestor with positioning applied. In this blog, we will discuss how to set position: absolute on an element and ensure it aligns with its parent container. 

Table of Contents:

Methods to Set Absolute Position Relative to Parent

You can use properties such as position: relative, display: grid; or display: flex and parent with overflow: hidden to set position absolute but relative to parent in CSS. Let’s discuss them below. 

Method 1: Using position: relative; on the Parent

You can set the parent container to position: relative to ensure the child elements are absolutely positioned relative to it. 

Example: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute Position Relative to Parent</title>
    <style>
        .parent {
            position: relative;
            width: 300px;
            height: 200px;
            background-color: lightblue;
        }
        .child {
            position: absolute;
            bottom: 10px;
            right: 10px;
            background-color: coral;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">Intellipaat</div>
    </div>
</body>
</html>

Output: 

using position relative on the parent

Explanation: In this code, you can see the parent element is set to position: relative to make sure the parent element is positioned relative to it. The child with position: absolute is placed 10px from the right bottom of the parent container.     

Method 2: Using display: grid or display: flex

Although the parent is a flex/grid container, the child element does not participate in the flex/grid layout because it is absolutely positioned. Instead, the parent acts as a reference point for positioning.

Example: Using Grid

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute Position Relative to Parent</title>
    <style>
        .parent {
    display: grid;
    place-items: center;
    position: relative;
    width: 300px;
    height: 200px;
    background-color: lightblue;
}
        .child {
    position: absolute;
    background-color: coral;
    padding: 10px;
}
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">Intellipaat</div>
    </div>
</body>
</html>

Output: 

using display grid

Explanation: The position: absolute property is set on the child element to get positioned relative to their parent. The parent container has a height and width of 300px and 200px and the child element has the padding property to get placed neatly inside the parent container.   

Example: Using Flex

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute Position Relative to Parent</title>
    <style>
        .parent {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    width: 300px;
    height: 200px;
    background-color: lightblue;
}
        .child {
    position: absolute;
    background-color: coral;
    padding: 10px;
}
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">Intellipaat</div>
    </div>
</body>
</html>

Output: 

using display flex

Explanation: This code uses display: flex to center the child element. The child element is placed relative to the parent using position: relative. The child element also has the padding property to make sure it looks organized.  

Method 3: Using overflow: hidden in Parent

You can set the parent element to overflow: hidden to make sure the child element is inside its container.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute Position Relative to Parent</title>
    <style>
        .parent {
    position: relative;
    width: 300px;
    height: 200px;
    overflow: hidden;
    background-color: lightblue;
}
        .child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: coral;
    padding: 10px;
}
    </style>
</head>
<body>
    <div class="parent">
        <div class="child">Intellipaat</div>
    </div>
</body>
</html>

Output: 

using overflow hidden in parent

Explanation: You can set the parent as overflow: hidden to ensure the child element is inside the parent container and it avoids the overflow of the content. The properties such as top: 50% and left: 50% are used to center the child. You can also use transform: translate(-50%, -50%) for the perfect positioning of the child element inside the parent container.   

Why do These Methods Work?

When you are setting the .parent to the CSS properties such as position: relative, display: grid, display: flex, or overflow: hidden, it turns into the reference point to all the absolutely positioned elements inside them. If you are not setting these properties, the element inside the parent container gets positioned with respect to the HTML document, the .child element is likely to be positioned outside the parent container.

Conclusion 

The child element can be positioned absolutely but relative to its parent element using CSS properties such as position: relative, display: grid, or display: flex. These properties make sure the child elements are positioned inside the parent container rather than the entire document. This ensures your web pages are clean and well-structured.  

How to Set Position Absolute but Relative to Parent in CSS – FAQs 

1. Why is my absolutely positioned element not relative to the parent?

Make sure the parent element is set to any one of the following properties position: relative, display: grid, or display: flex. Without these properties, your absolutely positioned element is not relative to the parent.

2. What happens if the parent doesn't have a positioning property?

The child element with the position: absolute property will be positioned relative to the entire document. 

3. Can I center a child element inside the parent using position absolute?

Yes, you can use properties like top: 50%, left: 50%, and transform: translate(-50%, -50%) to center the child element inside the parent.

4. How does display: grid or display: flex affect absolute positioning?

These properties allow you to easily manage the layout of the parent and center the child. However, since the child is absolute, it does not take part in the grid or flex layout itself.

5. What is the use of overflow: hidden in a parent?

It prevents the child element from overflowing outside the parent container.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner