/*<div class="content">This works with any content</div>*/
.content {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
#inner {
margin: auto 0;
}
#outer {
width:100%;
display: flex;
justify-content: center;
}
<div id="outer">
<div id="inner">Foo foo</div>
</div>
/* margin auto does the magic, make sure to provide width less than 100% */
.center {
margin: auto;
width: 400px;
}
.center {
margin: auto;
width: 50%;
}
.content {
position: absolute;
text-align: center;
top: 50%;
}
.<your-outer-div> {
display: flex;
justify-content: center;
('align-items: center' will allow you to vertically center too ;))
}
.inner{
margin: 0 auto;
}
You can apply this CSS to the inner <div>:
#inner {
width: 50%;
margin: 0 auto;
}
Of course, you don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering.
If you are targeting Internet Explorer 8 (and later), it might be better to have this instead:
#inner {
display: table;
margin: 0 auto;
}
It will make the inner element center horizontally and it works without setting a specific width.
Working example here:
#inner {
display: table;
margin: 0 auto;
border: 1px solid black;
}
#outer {
border: 1px solid red;
width:100%
}
<div id="outer">
<div id="inner">Foo foo</div>
</div>
#inner {
width: 50%;
margin: 0 auto;
}
#inner {
display: table;
margin: 0 auto;
border: 1px solid black;
}
#outer {
border: 1px solid red;
width:100%
}
<-- Html -->
<div id="outer">
<div id="inner">Foo foo</div>
</div>
.container {
width: 980px;
margin: 0 auto;
}
Code Example |
---|
Css :: center with flex |
Css :: line spacing css |
Css :: curved lines css |
Css :: color checker css online |
Css :: css flex responsive |
Css :: get into a Docker container bash |
Css :: style image so it crops |
Css :: css center text |
Css :: bulma capitalized |
Css :: css glow on hover |
Css :: add line below text css |
Css :: hide element using css |
Css :: convert image in rounshape in css |
Css :: css scaling border radius |
Css :: set svg background color css |
Css :: progress bar color change css |
Css :: table font size |
Css :: css lighten function |
Css :: disable right click with css |
Css :: css transform |
Css :: css disable button click |
Css :: font-strech css |
Css :: align grid items with end |
Css :: color code for cyan |
Css :: css grid auto width |
Css :: double border color css |
Css :: jquery hover class css not working |
Css :: scroll padding top in css |
Css :: circle css |
Css :: media screen hp |