I am new to css3
. Just wanted to know how to center align a div, it should work on web browser, web-kit. I don't have the exact size of the page. I tried
div.inputs {
margin-left:auto;
margin-right:auto;
}
But this never works for me.
Depending on your HTML you could use display: flex
to achieve an easy completely centred element with a small amount of CSS and no additional HTML elements
.container{
display: flex;
align-items: center;
justify-content: center;
}
The best way is to define the width, and then set margin to auto & 0:
div.inputs{ margin: 0 auto; width 100%}
If you don't know the size of your element, you can use the display: inline-block;
with a parent having text-align: center;
or you can use a display: table; margin-left: auto; margin-right: auto;
Stable Solution (supports flexible height easily):
A more stable code could be look like this which works for vertically & horizontally center a fixed-width, flexible height content:
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 100px
/*whatever width you want*/
;
}
<div class="outer">
<div class="middle">
<div class="inner">
<h1>The Content</h1>
<p>The inner content :)</p>
</div>
</div>
</div>
Quick and dirty:
According to your comment on another answer you want it to be centered horizontally and vertically or only horizontally. You can do this by using position absolute:
.centerDiv {
width:270px;
height:150px;
position:absolute;
left:50%;
top:50%;
margin:-75px 0 0 -135px;
background-color: green;
}
<div class="centerDiv">Center Me :)</div>
If you want it to be centered only horizontally simply use:
.centerDiv {
width: 270px;
margin: 0 auto;
}
See my answer on this Link here I used just div and span tag to center a div