Some posters have mentioned the css3CSS 3 way to center using display:box.
This syntax is outdated and shouldn't be used anymore.[See [See also this post] So.
So just for completeness here is the latest way to center in css3CSS 3 using the Flexible Box Layout Module.
So if you have simple markup like:
<div class="box">
<div class="item1">A</div>
<div class="item2">B</div>
<div class="item3">C</div>
</div>
...and you want to center your items within the box, here's what you need on the parent element (.box):
.box {
display: flex;
flex-wrap: wrap; /* optionalOptional. only if you want the items to wrap */
justify-content: center; /* forFor horizontal alignment */
align-items: center; /* forFor vertical alignment */
}
.box {
display: flex;
flex-wrap: wrap;
/* optionalOptional. only if you want the items to wrap */
justify-content: center;
/* forFor horizontal alignment */
align-items: center;
/* forFor vertical alignment */
}
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
.box {
height: 200px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
border: 2px solid tomato;
}
.box div {
margin: 0 10px;
width: 100px;
}
.item1 {
height: 50px;
background: pink;
}
.item2 {
background: brown;
height: 100px;
}
.item3 {
height: 150px;
background: orange;
}
<div class="box">
<div class="item1">A</div>
<div class="item2">B</div>
<div class="item3">C</div>
</div>
If you need to support older browsers which use older syntax for flexbox here's a good place to look.