Reputation: 299
Why is the absolute positioned DIV not inheriting the width of its parent? Div has
div {
position: absolute;
top:100px;
}
<html>
<body>
<div>This DIV</div>
</body>
</html>
In static positioning the DIV takes all available space, in absolute the width of the DIV is as long as the length of its contents.
Upvotes: 5
Views: 13795
Reputation: 465
Read @DaniP docs link for the answer.
Add style width : inherit for inheriting the width of its parent
#divParent{
width:300px;
}
#divAbsolute {
position: absolute;
top:100px;
background:red;
width:inherit;
}
<html>
<body>
<div id="divParent">
<div id="divAbsolute">This DIV</div>
</div>
</body>
</html>
Upvotes: 3
Reputation: 135
Because absolutely positioned elements do not behave as block level elements and do not flow after each other like normal a div does.
absolute position affects width?
Upvotes: 4
Reputation: 51
The default width of a div is auto. It is automatically the width of the content inside. It does not inherit the width from the parent. To do this you need to specify inherit on the width of the div
div {
position: absolute;
top:100px;
width: inherit;
}
http://codepen.io/anon/pen/BLPNrm
Upvotes: 4