I have an div
element with variable height which I need to be positioned by it's bottom
relative to the containers top.
This must be done without changing the html.
e.g.
<div id="container">
<h1>Some Text<br/>more...</h1>
</div>
h1
's bottom should be 100px below #container
's top.
Thanks a lot
EDIT:
So by Request what I did (or didn't) tried:
css bottom top position relative
but that's not the best search terms in the world...h1
and give it a height of 100px but then I would need to change the html and that I can'tbottom: somevalue
but that positions the element's bottom relative to the container's bottom.Have your four divs nested inside the target div, give the target div position: relative , and use position: absolute on the others. And this CSS should work: #container { position: relative; } #container > * { position: absolute; } . left { left: 0; } .
Absolute In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.
Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.
You could make use of transform: translateY(-100%)
, to make the bottom of the element relative when you apply margin-top: 100px
to h1
.
#container {
width: 200px;
height: 200px;
background: tan;
overflow: hidden;
}
#container h1 {
transform: translateY(-100%);
margin-top: 100px;
background: papayawhip
}
<div id="container">
<h1>Some Text<br/>more...</h1>
</div>
Depending on browser support requirements:
#container {
position: relative;
}
#container h1 {
position: absolute;
bottom: calc(100% - 100px);
}
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With