If you really want the parent div collapse around its children (for whatever reason, based on what you are trying to execute), and then you want to center this div , then the @Vector answer will be spot, use display: table with margin: 0 auto .
If it is normal for a div to remain expanded to the full width of the container in which you are trying to center your children, then you have at least a few more options, again depending on your specific situation.
You can use text-align: center .
.content { text-align: center; border-style: solid; border-width: thin; } .content span { display: inline; border-style: solid; border-width: thin; }
<div class="content"> <div>Test</div> <div>Test</div> </div>
You can also use the new display: flex with justify-content: center , depending on the compatibility level of the browser you support, of course.
.content { display: flex; justify-content: center; border-style: solid; border-width: thin; } .content div { border-style: solid; border-width: thin; }
<div class="content"> <div>Test</div> <div>Test</div> </div>
Ryan H.
source share