0

I'm trying to center a group of relative positioned divs dynamically inside of a parent div that is also relative positioned.

The parent div is 620px wide, and the child divs are each 200px wide. There can be 1 to 3 child divs per line, thus what I meant by trying to center the group of child divs within the parent div dynamically. For example, if there is only 1 child div, that child div should be centered in the parent div, or if there are only 2 child divs, those child divs should be centered in the parent div.

I would use inline block for the child divs, however inside the child divs there are also divs that are absolute positioned to the child divs, so inline-block wouldn't work for the absolute positioning.

This is my css, or you can see a working example here: https://jsfiddle.net/y3ghpkvs/

.parentClass {
    position: relative;
    width: 620px;
    height: 500px;
    background-color: gray;
}

.childClass {
    position: relative;
    width: 200px;
    height: 400px;
    float: left;
    margin-left: 5px;
    background-color: white;
}

.insideChildDiv1 {
    position: absolute;
    width: 100%;
    height: 95px;
    top: 0;
    left: 0;
    background-color: black;
    color: white;
    text-align: center;
    line-height: 100px;
}

.insideChildDiv2 {
    position: absolute;
    width: 100%;
    height: 200px;
    top: 100px;
    left: 0;
    background-color: green;
}

.insideChildDiv3 {
    position: absolute;
    width: 100%;
    height: 95px;
    bottom: 0;
    left: 0;
    color: white;
    text-align: center;
    line-height: 100px;
    background-color: black;
}

I can't seem to figure out how to center the 2 childClass divs inside the parentClass div. Anyone have any tips?

Calvyno
  • 37
  • 1
  • 8
  • why you say this `so inline-block wouldn't work for the absolute positioning.` ? it's not true – Temani Afif Jan 14 '18 at 08:01
  • Possible duplicate of [How to horizontally center a
    in another
    ?](https://stackoverflow.com/questions/114543/how-to-horizontally-center-a-div-in-another-div)
    – Temani Afif Jan 14 '18 at 08:01

1 Answers1

1

Solution 1: Using Flex

Try to use flex css. Using flex it will be easy for you align the items vertically or horizontally center to the parent.

Use justify-content: center; to align the divs center.

Updated Fiddle

.parentClass {
  width: 620px;
  background-color: gray;
  display: flex;
  justify-content: center;
  margin: auto;
  flex-wrap: wrap;
}

.childClass {
  width: 200px;
  height: 400px;
  background-color: white;
  position: relative;
  margin: 3px;
}

.insideChildDiv1 {
  position: absolute;
  width: 100%;
  height: 95px;
  top: 0;
  left: 0;
  background-color: black;
  color: white;
  text-align: center;
  line-height: 100px;
}

.insideChildDiv2 {
  position: absolute;
  width: 100%;
  height: 200px;
  top: 100px;
  left: 0;
  background-color: green;
}

.insideChildDiv3 {
  position: absolute;
  width: 100%;
  height: 95px;
  bottom: 0;
  left: 0;
  color: white;
  text-align: center;
  line-height: 100px;
  background-color: black;
}
<div class="parentClass">
  <div class="childClass">
    <div class="insideChildDiv1">George</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">CEO</div>
  </div>
  <div class="childClass">
    <div class="insideChildDiv1">John</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">Vice President</div>
  </div>
  <div class="childClass">
    <div class="insideChildDiv1">John</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">Vice President</div>
  </div>
  <div class="childClass">
    <div class="insideChildDiv1">John</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">Vice President</div>
  </div>
</div>

Solution 2: Using display inline-block

.parentClass {
  width: 620px;
  background-color: gray;
  margin: auto;
  text-align: center;
}

.childClass {
  width: 200px;
  height: 400px;
  background-color: white;
  position: relative;
  margin: 4px 0;
  display: inline-block;
  margin-left: 2px;
}

.insideChildDiv1 {
  position: absolute;
  width: 100%;
  height: 95px;
  top: 0;
  left: 0;
  background-color: black;
  color: white;
  text-align: center;
  line-height: 100px;
}

.insideChildDiv2 {
  position: absolute;
  width: 100%;
  height: 200px;
  top: 100px;
  left: 0;
  background-color: green;
}

.insideChildDiv3 {
  position: absolute;
  width: 100%;
  height: 95px;
  bottom: 0;
  left: 0;
  color: white;
  text-align: center;
  line-height: 100px;
  background-color: black;
}
<div class="parentClass">
  <div class="childClass">
    <div class="insideChildDiv1">George</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">CEO</div>
  </div>
  <div class="childClass">
    <div class="insideChildDiv1">John</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">Vice President</div>
  </div>
  <div class="childClass">
    <div class="insideChildDiv1">John</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">Vice President</div>
  </div>
  <div class="childClass">
    <div class="insideChildDiv1">John</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">Vice President</div>
  </div>
  <div class="childClass">
    <div class="insideChildDiv1">John</div>
    <div class="insideChildDiv2"></div>
    <div class="insideChildDiv3">Vice President</div>
  </div>
</div>

I hope this will help you!

Bhuwan
  • 16,525
  • 5
  • 34
  • 57
  • Thanks, with your solution, if I put more than 3 childClass divs, it will squish the childClass divs to all fit on one line. I would like it to be forced to stay at 200px wide. How would I go about keeping them at a static width? – Calvyno Jan 14 '18 at 07:40
  • @Calvyno Updated! Use `flex-wrap:wrap` to wrap the flexible items – Bhuwan Jan 14 '18 at 07:44
  • Thanks Bhuwan! That worked! Now the issue is it doesn't work for anything older than IE 11 :( – Calvyno Jan 14 '18 at 07:56
  • @Calvyno You can use display `inline-block` for full support. Check solution2 above – Bhuwan Jan 14 '18 at 08:06
  • Yes, however because of the insideChildDiv, absolute positioning wont' work if the childClass divs are displayed as inline-block. – Calvyno Jan 14 '18 at 08:14
  • When I set all the insideChildDiv elements to absolute positioning and the childClass elements to display:inline-block, it ignores the width of the childClass and just expands outside of its area. – Calvyno Jan 15 '18 at 17:25
  • @Calvyno Remove `width:100%` from `insideChildDiv` and set `left` and `right` property to `**0**`. It will fix the expands issue. – Bhuwan Jan 15 '18 at 17:28
  • my god thank you! That works! What do the * mean in \*\*0\*\*? Never seen that before. Also I had to add a width: inherit. – Calvyno Jan 16 '18 at 18:28
  • @Calvyno oh! it was my mistake. I also never seen that before :) – Bhuwan Jan 16 '18 at 18:31
  • Oh it was? weird, because if I don't include the * then it won't work correctly. Here is the jsfiddle you sent me with your solution. I modified it with your suggested changes and it isn't working correctly, especially the positioning of the insideChildDivs https://jsfiddle.net/8o6w6s4v/2/ – Calvyno Jan 17 '18 at 18:44
  • @Calvyno because you didn't set `position:relative` to the `.childClass`. See this fiddle https://jsfiddle.net/bhuwanb9/xjgcpyyk/ – Bhuwan Jan 18 '18 at 15:17