Reputation: 891
So I just googled a lot and was not able to find a solution for this. I want 2 items to be centered horizontally and vertically. The trick here is that I want to stack them above each other, no problem with position:absolute, but I can't center the elements with an absolute position. I think that this should work anyhow with flexbox. With stacking I mean so that one element is hiding the other partially.
Upvotes: 22
Views: 42008
Reputation: 32275
The container can be set align-items: center
and justify-content: center
for horizontal and vertical centering.
.outer {
align-items: center;
background: #800000 none repeat scroll 0 0;
border: 5px solid #353535;
border-radius: 50%;
display: flex;
height: 300px;
justify-content: center;
position: relative;
width: 300px;
}
.inner {
background: #C83737;
border: 5px solid #353535;
width: 150px;
height: 150px;
border-radius: 50%;
}
<div class="outer">
<div class="inner"></div>
</div>
Upvotes: 14
Reputation: 2280
<div class='item1'>
<div class='item2'></div>
</div>
html, body{
height: 100%;
}
body{
display: flex;
flex:1 1 auto;
justify-content:center;
align-items:center
}
.item1 {
display: flex;
width: 200px;
height: 200px;
background: red;
justify-content: space-around;
align-items:center
}
.item2 {
width: 100px;
height: 100px;
background: green;
}
Upvotes: 1
Reputation: 272
Maybe something like this? fiddle
<div class="container">
<div class="item"></div>
<div class="item"></div>
</div>
.container {
display: flex;
flex-flow: wrap;
align-items: center;
justify-content: center;
border: 1px solid black;
height: 500px;
width: 100%;
align-content: center;
}
.item {
flex: 1 100%;
border: 1px solid black;
height: 100px;
}
Upvotes: 3