I have a "container" div
to which I gave margin:auto;
.
It worked fine as long as I gave it a specific width
, but now I changed it to inline-block
and margin:auto;
stopped working
#container { border: 1px solid black; height: 200px; width: 200px; } .MtopBig { margin-top: 75px; } .center { margin-left: auto; margin-right: auto; text-align: center; }
<div class="center MtopBig" id="container"></div>
#container { border: 1px solid black; display: inline-block; padding: 50px; } .MtopBig { margin: 75px auto; position: relative; } .center { text-align: center; }
<div class="center MtopBig" id="container"></div>
DEMO fiddle.
`margin:auto;` doesn't work on inline-block elements.
margin: auto; , it will not work. This is because by default the block-level elements such as a div, p, list, etc take up the full width of its parent element, and no space is left to adjust the element horizontally. So, the very first thing you need to check is whether you have set the width of the element or not.
The display:inline property treats an element as if it were a normal inline element such as <span> . For inline elements, horizontal padding and margins are respected, but the vertical margin and padding is discarded.
Margin properties specify the width of the margin area of a box. The 'margin' shorthand property sets the margin for all four sides while the other margin properties only set their respective side. These properties apply to all elements, but vertical margins will not have any effect on non-replaced inline elements.
It is no longer centered because it now flows on the page in the same way inline
elements do (very similarly to img
elements). You will have to text-align: center
the containing element to center the inline-block
div
.
#container { border: 1px solid black; display: inline-block; padding: 50px; } .MtopBig { margin: 75px auto; position: relative; } .center { text-align: center; }
<div class="center"> <div class="MtopBig" id="container"></div> </div>
What 'auto' means:
Using auto
for the horizontal margin will instruct the element to fill up the available space (source: http://www.hongkiat.com/blog/css-margin-auto/).
Why 'display: inline-block' does not center:
There is no available horizontal space in an inline setting. Before and after it are other inline elements (characters) that take up their own space. Therefore, the element will act as if the horizontal margin is set to zero.
Why 'display: block' centers:
When used as an element with display: block
set to it, the available horizontal space will be the full width of the parent element minus the width of the element itself. This makes sense because display: block
is reserving this horizontal space (thus making it 'available'). Note that elements with display: block
cannot be placed next to each other. The only exception occurs when you use float
, but in that case you also get the (expected) zero-margin-behaviour, as this disables the horizontal 'availability'.
Solution for 'inline-block' elements:
Elements with display: inline-block
should be approached as characters. Centering characters/text can be done by adding text-align: center
to their parent (but you probably knew that already...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With