Here is another way to center horizontally using Flexbox and without specifying any width to the inner container. The idea is to use pseudo elements that will push the inner content from the right and the left.
Using flex:1
on pseudo element will make them fill the remaining spaces and take equal size and the inner container will get centered.
.container {_x000D_
display: flex;_x000D_
border: 1px solid;_x000D_
}_x000D_
_x000D_
.container:before,_x000D_
.container:after {_x000D_
content: "";_x000D_
flex: 1;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
border: 1px solid red;_x000D_
padding: 5px;_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="inner">_x000D_
Foo content_x000D_
</div>_x000D_
</div>
_x000D_
We can also consider the same situation for vertical alignment by simply changing the direction of flex to column:
.container {_x000D_
display: flex;_x000D_
flex-direction: column;_x000D_
border: 1px solid;_x000D_
min-height: 200px;_x000D_
}_x000D_
_x000D_
.container:before,_x000D_
.container:after {_x000D_
content: "";_x000D_
flex: 1;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
border: 1px solid red;_x000D_
padding: 5px;_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="inner">_x000D_
Foo content_x000D_
</div>_x000D_
</div>
_x000D_