/*<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;
}
.container {
width: 980px;
margin: 0 auto;
}
Code Example |
---|
Css :: detect if an element has a class jQurey |
Css :: css list line spacing |
Css :: font semi bold css |
Css :: vertically center div inside div |
Css :: how to block elements from scrolling css |
Css :: apply color tint to image |
Css :: css hide timeline |
Css :: css selector not checked |
Css :: li only showing first bullet |
Css :: comfirm before delete |
Css :: background color for whole page css |
Css :: padding html |
Css :: css dark mode |
Css :: bootstrap word-wrap: break-word; |
Css :: how to horizontally center in css |
Css :: how to evenly space icons in a div css |
Css :: css rotate 3d |
Css :: css text align |
Css :: add line below text css |
Css :: html table wrap text |
Css :: css scrollbar always visible |
Css :: input outline focus |
Css :: kerning css |
Css :: one image position relative and other absolute |
Css :: mat-progress-bar just dots |
Css :: css disabled cursor not allowed |
Css :: add inner border css |
Css :: smooth scroll |
Css :: inner border css |
Css :: inherit class in sass |