Reputation: 37
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?
Upvotes: 0
Views: 617
Reputation: 16855
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.
.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!
Upvotes: 1