746

I have two divs inside another div, and I want to position one child div to the top right of the parent div, and the other child div to the bottom of the parent div using css. Ie, I want to use absolute positioning with the two child divs, but position them relative to the parent div rather than the page. How can I do this?

Sample html:

<div id="father">
   <div id="son1"></div>
   <div id="son2"></div>
</div>
Kara
  • 6,115
  • 16
  • 50
  • 57
BlaShadow
  • 11,075
  • 7
  • 39
  • 60
  • You want son1 to be in the top right corner of father but where on the bottom should son2 be? Bottom left, right, or center? – j08691 May 07 '12 at 18:45
  • 1
    In this case, you would need to set position: relative to the parent element, and position: absolute to the children elements. On the first child element, you should put top: 0 and right: 0 to position it on the top right of the parent element. On the second child, you should put bottom: 0 to position it on the bottom of the parent element. There is a great article here https://kolosek.com/css-position-relative-vs-position-absolute explaining the relative and absolute positioning in detail. – Nesha Zoric Feb 21 '18 at 08:49

5 Answers5

1292
#father {
   position: relative;
}
    
#son1 {
   position: absolute;
   top: 0;
}
  
#son2 {
   position: absolute;
   bottom: 0;
}

This works because position: absolute means something like "use top, right, bottom, left to position yourself in relation to the nearest ancestor who has position: absolute or position: relative."

So we make #father have position: relative, and the children have position: absolute, then use top and bottom to position the children.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
Domenic
  • 110,262
  • 41
  • 219
  • 271
  • 54
    Why is `#father { position: relative; }` required? – joshreesjones Aug 02 '14 at 05:18
  • 13
    is required to change the "position rule" for those who are inside him. – BlaShadow Aug 02 '14 at 17:28
  • 80
    @mathguy54 Because the spec says absolutely positioned elements are positioned relative to the first *positioned* parent, which means any parent that doesn't have a position value of `static`. – Alex W Aug 07 '14 at 13:32
  • what about such scenario: FATHER is relative and its height is 100% how to position son and son2 at, let's say 20% and 70% of teh father's height respectively? – Rossitten Mar 08 '15 at 05:56
  • This worked nicely for what I was aiming to do with my makeshift HP and MP bars. I wanted to display health/healthmax over the bar in the center based on the parent, but it only wanted to center over the leftover bar. I essentially had div > div > span with the text using absolute position and the top div using relative and it worked just as I had hoped! – Scrydan Mar 04 '16 at 22:34
  • @AlexW Could you tell me which "spec" are you referring to? Can you provide a link? – Bruce Sun Jan 12 '18 at 08:14
  • 1
    @BruceSun https://www.w3.org/TR/CSS2/visudet.html#containing-block-details – Alex W Jan 17 '18 at 14:34
  • If you right so what about this answer? https://stackoverflow.com/a/1137597/1830909 and this matter: "The absolute divs are taken out of the flow of the document"? – QMaster Apr 15 '18 at 21:59
  • In my case height is 100 with a max-height of 45rem. Normal top, bottom alignments not position well in different screen sizes. So I have to add min-height and max-height to solved my problem. – Dinith Rukshan Kumara Apr 30 '22 at 09:54
  • What is the use of 'static' position? It seems its only purpose is to cause this confusion with 'absolute'. The very concept of an unpositioned parent is nonsensical. If it has no position how can you see it at all? – swpalmer May 11 '22 at 15:21
  • what is gonna happen if the parent is absolute and the children are absolute too? not to forget body is static – Kick Buttowski Jun 14 '23 at 07:45
  • It's a shame that the [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/position) page does not mention that the closest ancestor needs to be `absolute` or `relative` itself for absolute to be relative to it. – cassepipe Sep 02 '23 at 15:56
42
div#father {
    position: relative;
}
div#son1 {
    position: absolute;
    /* put your coords here */
}
div#son2 {
    position: absolute;
    /* put your coords here */
}
Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
41

Incase someone wants to postion a child div directly under a parent

#father {
   position: relative;
}

#son1 {
   position: absolute;
   top: 100%;
}

Working demo Codepen

petergus
  • 1,192
  • 1
  • 12
  • 18
9

If you don't give any position to parent then by default it takes static. If you want to understand that difference refer to this example

Example 1::

http://jsfiddle.net/Cr9KB/1/

   #mainall
{

    background-color:red;
    height:150px;
    overflow:scroll
}

Here parent class has no position so element is placed according to body.

Example 2::

http://jsfiddle.net/Cr9KB/2/

#mainall
{
    position:relative;
    background-color:red;
    height:150px;
    overflow:scroll
}

In this example parent has relative position hence element are positioned absolute inside relative parent.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
anam
  • 3,905
  • 16
  • 45
  • 85
  • And, what about if you don't have the #mainall{height:150px}...? I mean, if mainall has dynamic height? – Albert Català Sep 08 '15 at 15:02
  • then your floating element will be relative to the position that the parent element had when the page (and the css) was loaded. If you want to update that after the page load, use javascript - clientX and clientY are a good place to start – Abraham Brookes Mar 21 '16 at 12:00
4

In a use case of needing a sticky floating "back button"/ "back to top" button. But you have the main content area that can shrink in favor of the side content area

You can use position: fixed; inside a container of position: absolute; to get a more flexible behavior similar to sticky but more powerful

function toggleOpen() {
  const element = document.getElementById("sideContnet");
  if (element.style.display === "none") {
    element.style.display = "block";
  } else {
    element.style.display = "none";
  }
}
.container {
  display: flex;
  margin: 0 auto;
    width: 600px;
}

.contentBig {
  width: 600px;
  min-width: 66%;
  position: relative;
  
}
.contentSmall {
  width: 230px;
  min-width: 33%;
}

.absolute {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 60px;
}

.fiexBack {
  position: fixed;
  background-color: red;
  bottom: 20px;
  padding: 5px;
}

.button {
  width: 200px;
  padding: 12px;
  background-color: coral;
  margin: 0 auto;
}
<button onclick="toggleOpen()" id="toggleSide" class="button">
  toggle open side content
</button>
<div class="container">
  <div class="contentBig">
    <div class="absolute">
      <div class="fiexBack">Back</div>
  </div>
    <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vehicula est at pretium venenatis. Morbi mauris justo, viverra non velit sit amet, accumsan cursus nisi. Pellentesque ut vulputate sem. Etiam sit amet quam diam. Nulla vel sodales est. Quisque sed accumsan urna. Ut tristique efficitur ante a congue. Cras elementum dignissim tellus, in rhoncus ex faucibus nec. Sed vitae mi eu leo interdum aliquam. Donec eleifend nisl sem, a tincidunt velit commodo sit amet. Nulla ex eros, tempor in fringilla ut, accumsan ac augue. Curabitur aliquet venenatis massa, ornare viverra lorem varius interdum.
     </p>
<p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vehicula est at pretium venenatis. Morbi mauris justo, viverra non velit sit amet, accumsan cursus nisi. Pellentesque ut vulputate sem. Etiam sit amet quam diam. Nulla vel sodales est. Quisque sed accumsan urna. Ut tristique efficitur ante a congue. Cras elementum dignissim tellus, in rhoncus ex faucibus nec. Sed vitae mi eu leo interdum aliquam. Donec eleifend nisl sem, a tincidunt velit commodo sit amet. Nulla ex eros, tempor in fringilla ut, accumsan ac augue. Curabitur aliquet venenatis massa, ornare viverra lorem varius interdum.
     </p>
<p>
Morbi leo nulla, varius nec dignissim quis, vestibulum quis mi. Sed dignissim lobortis magna. Ut erat nisl, varius id finibus at, faucibus scelerisque justo. Donec viverra purus eu ante volutpat iaculis. Donec vehicula ullamcorper urna ut egestas. Curabitur convallis at risus vitae fermentum. Nullam arcu ante, faucibus quis neque vel, pulvinar blandit est.
     </p>
<p>
Curabitur auctor ipsum ac interdum accumsan. Sed quis arcu mauris. Maecenas nibh ligula, tristique rhoncus pharetra vel, blandit non lectus. Suspendisse orci felis, faucibus sit amet rhoncus eu, ullamcorper et nulla. Ut in leo eu risus dignissim tempus sed sit amet leo. Etiam pulvinar lectus tincidunt turpis viverra maximus. Donec rutrum rutrum dui sit amet congue.
     </p>
<p>
Morbi leo nulla, varius nec dignissim quis, vestibulum quis mi. Sed dignissim lobortis magna. Ut erat nisl, varius id finibus at, faucibus scelerisque justo. Donec viverra purus eu ante volutpat iaculis. Donec vehicula ullamcorper urna ut egestas. Curabitur convallis at risus vitae fermentum. Nullam arcu ante, faucibus quis neque vel, pulvinar blandit est.
     </p>
  </div>
   <div id="sideContnet" class="contentSmall">
    <p>
       Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vehicula est at pretium venenatis. Morbi mauris justo, viverra non velit sit amet, accumsan cursus nisi. Pellentesque ut vulputate sem. Etiam sit amet quam diam. Nulla vel sodales est. Quisque sed accumsan urna. Ut tristique efficitur ante a congue. Cras elementum dignissim tellus, in rhoncus ex faucibus nec. Sed vitae mi eu leo interdum aliquam. Donec eleifend nisl sem, a tincidunt velit commodo sit amet. Nulla ex eros, tempor in fringilla ut, accumsan ac augue. Curabitur aliquet venenatis massa, ornare viverra lorem varius interdum.
     </p>
<p>
Morbi leo nulla, varius nec dignissim quis, vestibulum quis mi. Sed dignissim lobortis magna. Ut erat nisl, varius id finibus at, faucibus scelerisque justo. Donec viverra purus eu ante volutpat iaculis. Donec vehicula ullamcorper urna ut egestas. Curabitur convallis at risus vitae fermentum. Nullam arcu ante, faucibus quis neque vel, pulvinar blandit est.
     </p>
  </div>
</div>

Full Code Example

Avner Israel
  • 176
  • 1
  • 15