The Wayback Machine - https://web.archive.org/web/20240811161357/https://www.geeksforgeeks.org/why-margin-on-child-element-moves-the-parent-element-in-css/
Open In App

Why margin on child element moves the parent element in CSS ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In CSS, when a margin is applied to a child element, it can affect the position of the parent element. This happens because the margins of an element collapse with the margins of its adjacent siblings or its parent, based on certain rules. If the parent element has no padding or border, and the child element has a top margin, then the child element’s margin collapses with the parent element’s margin, causing the parent element to move along with the child element. 

In the below code, we have a parent div element with a blue background color, a child div element with a coral background color, and a 50-pixel top margin. When we run the HTML file in a browser and inspect the elements, we’ll see that the child element’s margin has pushed the parent element down by 50 pixels, causing it to appear lower on the page.

Example:

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" />

    <style>
        .parent {
            background-color: lightblue;
            width: 300px;
        }

        .child {
            background-color: lightcoral;
            width: 150px;
            margin-top: 50px;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">
            I am a child element.
        </div>
    </div>
</body>

</html>

Output:


To solve the above problem, two common approaches can be used. Let’s see them one by one.

Approach 1: Add padding or border to the parent element

One way to prevent the child element’s margin from collapsing with the parent element’s margin is to add padding or a border to the parent element. This creates a “barrier” between the parent and child margins, preventing them from collapsing.

Syntax:

.parent {
padding: 1px;
/* or */
border: 1px solid;
}

Example: In the example, we added a 1px solid border to the parent element. This prevents the child element’s margin from collapsing with the parent element’s margin. As a result, the parent element stays in place, and the child element is positioned below it with a 50px margin.

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" />

    <style>
        .parent {
            background-color: lightblue;
            width: 300px;
            border: 1px solid;
        }

        .child {
            background-color: lightcoral;
            width: 150px;
            margin-top: 50px;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">
            I am a child element.
        </div>
    </div>
</body>

</html>

Output:


Approach 2: Using Overflow Property on Parent Element

Another way to prevent the parent element from moving due to a child element’s margin is to use the overflow property on the parent element. By setting the overflow property to “auto” or “hidden” we can create a new block formatting context for the parent element, which prevents its margin from collapsing with the child element’s margin.

Syntax:

.parent {
overflow: auto;
/* or */
overflow: hidden;
}

Example: In this example, we set the overflow property of the parent element to “auto”, which creates a new block formatting context for the parent element. This prevents the margin of the child element from collapsing with the margin of the parent element. As a result, the parent element stays in place, and the child element is positioned below it with a 50px margin.

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" />

    <style>
        .parent {
            background-color: lightblue;
            width: 300px;
            overflow: auto;
        }

        .child {
            background-color: lightcoral;
            width: 150px;
            margin-top: 50px;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">
            I am a child element.
        </div>
    </div>
</body>

</html>

Output:




Similar Reads

How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to Style Parent Element when Hover Over a Child Element in CSS ?
In this article, we will learn how to style the parent element when hovering over a child element. Styling the parent element when hovering over a child element is a useful CSS technique that allows you to change the appearance of the parent element when the mouse cursor is hovering over one of its child elements. This can be useful for adding inte
2 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to remove parent element except its child element using jQuery ?
Given an HTML document and the task is to remove the parent element except for its child element. Approach 1: Use contents() method to select all the direct children, including text and comment nodes for the selected element.Selected elements are stored in a variable.Now, use replaceWith() method to replace the content of parent element by its all
2 min read
How to return true if the parent element contains the child element in JavaScript ?
In JavaScript, children property returns an array of those elements which are children of a given element. In order to check whether the parent element (say x) contains a child element (say y), we can use the children property to get the array of all children of x and then check whether the array obtained contains y. To get the child element of a p
2 min read
How to apply CSS in last child of parent using jQuery ?
In this article, we will see how to set styles in the last child of the parent element using jQuery. To set the style on the last child of parent, we use jQuery :last-child selector. The :last-child selector is used to select every element that is the last child of its parent element. Syntax: $("Selector:last-child") Approach: First, we create an H
1 min read
How to expand floated child div's height to its parent's height in CSS ?
Making the height of the floating child div equal to the height of the parent div is referred to as expanding the height of the floated child div to the height of the parent div. Approach 1: Using the CSS "clearfix" method is the first approach to increase a floated child div's height to match its parent's height. When an element is floated, it is
4 min read
How to apply style to parent if it has child with CSS?
While we are familiar with applying styles to child elements when a parent class is present, sometimes we need to apply styles to a parent element based on its child elements. Although CSS does not natively support parent selectors, there are ways to achieve similar effects using combinators and JavaScript. Understanding Child CombinatorsA child co
2 min read
How to get the child element of a parent using JavaScript ?
In this article, we will learn to get the child element of the parent using JavaScript. Given an HTML document, the task is to select a particular element and get all the child elements of the parent element with the help of JavaScript. There are 2 ways to get the child element: Table of Content By using the children propertyBy using the querySelec
3 min read
How to Check if an element is a child of a parent using JavaScript?
In this article, we are going to see the methods by which we can Check if an element is a child of a parent using JavaScript. These are the following methods: Table of Content Using the Node.contains() methodLooping through the parents of the given childUsing the hasChildNodes() methodMethod 1: Using the Node.contains() methodThe Node.contains() me
4 min read
three90RightbarBannerImg