1/* this will center all children within the parent element. */
2.parent {
3 display: flex;
4 justify-content: center; /* horizontal */
5 align-items: center; /* vertical */
6}
1.row {
2 width: 100%;
3 display: flex;
4 flex-direction: row;
5 justify-content: center;
6}
7.block {
8 width: 100px;
9}
1.parent {
2 position: relative;
3}
4.child {
5 position: absolute;
6 top: 50%;
7 left: 50%;
8 transform: translate(-50%, -50%);
9}
1/* This works wonderfully when you know the size of
2the thing you are centering. If you don’t know, or are
3thinking it might change and want to be future proof,
4try this: */
5
6.centered {
7 position: fixed;
8 top: 50%;
9 left: 50%;
10 /* bring your own prefixes */
11 transform: translate(-50%, -50%);
12}
13
14/* The translate value for transform is based off the size
15of the element, so that will center nicely. */
1.center {
2 margin: auto;
3 width: 50%;
4 border: 3px solid green;
5 padding: 10px;
6}