Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css: Position element by it's bottom relative to it's container's top

Tags:

html

css

position

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:

  • Searching with Google for css bottom top position relative but that's not the best search terms in the world...
  • Normally I would put a container around h1 and give it a height of 100px but then I would need to change the html and that I can't
  • using bottom: somevalue but that positions the element's bottom relative to the container's bottom.
  • slain some vampires
like image 275
Scheintod Avatar asked Jan 16 '15 21:01

Scheintod


People also ask

How do you set position relative to other element in CSS?

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; } .

How do you position an element relative to a parent?

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.

How do you position an element to the bottom in CSS?

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.


2 Answers

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>
like image 182
Weafs.py Avatar answered Oct 16 '22 12:10

Weafs.py


Depending on browser support requirements:

#container {
    position: relative;
}

#container h1 {
    position: absolute;
    bottom: calc(100% - 100px);
}

Example

like image 43
Jesse Kernaghan Avatar answered Oct 16 '22 10:10

Jesse Kernaghan