[html] Flexbox: center horizontally and vertically

I think you want something like the following.

_x000D_
_x000D_
html, body {_x000D_
    height: 100%;_x000D_
}_x000D_
body {_x000D_
    margin: 0;_x000D_
}_x000D_
.flex-container {_x000D_
    height: 100%;_x000D_
    padding: 0;_x000D_
    margin: 0;_x000D_
    display: -webkit-box;_x000D_
    display: -moz-box;_x000D_
    display: -ms-flexbox;_x000D_
    display: -webkit-flex;_x000D_
    display: flex;_x000D_
    align-items: center;_x000D_
    justify-content: center;_x000D_
}_x000D_
.row {_x000D_
    width: auto;_x000D_
    border: 1px solid blue;_x000D_
}_x000D_
.flex-item {_x000D_
    background-color: tomato;_x000D_
    padding: 5px;_x000D_
    width: 20px;_x000D_
    height: 20px;_x000D_
    margin: 10px;_x000D_
    line-height: 20px;_x000D_
    color: white;_x000D_
    font-weight: bold;_x000D_
    font-size: 2em;_x000D_
    text-align: center;_x000D_
}
_x000D_
<div class="flex-container">_x000D_
    <div class="row"> _x000D_
        <div class="flex-item">1</div>_x000D_
        <div class="flex-item">2</div>_x000D_
        <div class="flex-item">3</div>_x000D_
        <div class="flex-item">4</div>_x000D_
    </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

See demo at: http://jsfiddle.net/audetwebdesign/tFscL/

Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.

Also, on .row, set the width to auto instead of 100%.

Your .flex-container properties are fine.

If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.

Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.

Footnote:
The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).

A useful resource is: http://demo.agektmr.com/flexbox/

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to flexbox

Force flex item to span full row width vuetify center items into v-flex Equal height rows in CSS Grid Layout display: flex not working on Internet Explorer Center the content inside a column in Bootstrap 4 Vertical Align Center in Bootstrap 4 How to use zIndex in react-native Bootstrap 4 align navbar items to the right Does 'position: absolute' conflict with Flexbox? Make flex items take content width, not width of parent container