Reputation: 8105
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.Upvotes: 9
Views: 6239
Reputation: 4634
Depending on browser support requirements:
#container {
position: relative;
}
#container h1 {
position: absolute;
bottom: calc(100% - 100px);
}
Upvotes: 6
Reputation: 22992
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>
Upvotes: 17
Reputation: 1071
#container
{
position: absolute;
height: 100px;
bottom:0px;
}
This code is not affecting html at all. I added css for id-container. An absolute position element is positioned relative to the first parent element that has a position other than static. You can change it to fixed it you wants to.
Height of the container, help you to calculate spacing from bottom.
Upvotes: 0
Reputation: 144
Only way through it is to add a height to the h1 unless you want to go with calc which isn't supported yet by some browsers. Then set your top margin to be top: 100px - h1's height. Hope this works
<div id="container">
<h1>Some Text<br/>more...</h1>
</div>
#container {
width: 300px;
height: 300px;
background: #222;
overflow: hidden;
}
#container h1 {
background: #444;
position:relative;
height:80px;
top:20px;
}
Upvotes: 0