Vertical alignment in CSS can be difficult, one of the strategies for doing this is to use vertical-align which allows vertical alignment with an inline or table-cell box.

This can be used with media queries to create a responsive layout which switches between a horizontal and vertical layout depending on the size of the page.

A responsive layout like this is nice for mobile devices because it’ll rearrange between portrait and landscape mode, additionally it adjusts nicely when resizing windows on desktop.

Demo

Example animation

Horizontal Layout

A horizontal layout is pretty easy with tables, we just create a div styled as a table

1
2
3
4
5
6
.table {
    display: table;
    table-layout:fixed;
    width: 100%;
    height: 100%;
}

add a row

1
2
3
.row {
    display: table-row;
}

and add a few cells to the row using vertical-align to center our content in each cell

1
2
3
4
5
6
.cell {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    overflow: hidden;
}

Changing to a vertical layout

To change from a row with cells to a single column with rows we just need to do a basic pivot. In CSS we’ll change our row into a sub-table, each cell into a new row, and a new object for the new cells.

  • table > row > cells
  • table > table > rows > cells

Full Code Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<html>
<head>
    <style type="text/css">
        html, body {
            height: 100%;
            margin: 0;
        }
        .container {
            display: table;
            table-layout:fixed;
            width: 100%;
            height: 100%;
        }
        .pivot {
            display: table-row;
        }
        .column-child {
            display: table-cell;
            vertical-align: middle;
            text-align: center;
            overflow: hidden;
        }
        @media (max-width: 500px) {
            .pivot {
                display: table;
                table-layout: fixed;
                width: 100%;
                height: 100%;
            }
            .column-child {
                display: table-row;
            }
            .row-child {
                display: table-cell;
                vertical-align: middle;
                text-align: center;
                overflow: hidden;
            }
        }
        .circle {
            height: 100px;
            width: 100px;
            margin: 0 auto;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="pivot">
            <div class="column-child">
                <div class="row-child">
                    <div class="circle" style="background: red;"></div>
                </div>
            </div>
            <div class="column-child">
                <div class="row-child">
                    <div class="circle" style="background: blue;"></div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>