css:
:
.element{
width: 200px;
left: 50%;
margin-left: -100px;
position: absolute;
display: block;
}
, css3:
.element{
width: 200px;
left: calc(50% - 100px);
position: absolute;
display: block;
}
You may also have a non-absolute approach , but the position of the parent should be relative :
.element-parent{
position: relative;
}
.element-parent .element{
margin: 0 auto;
}
If you use a text element (inline block), this works with IE 7 +:
.element-parent{
text-align: center;
}
.element-parent .element{
display: inline-block;
}
source
share