Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`margin:auto;` doesn't work on inline-block elements

Tags:

html

css

margin

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

Old code (works)

#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>

New code (doesn't work)

#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.

like image 923
Math chiller Avatar asked Sep 29 '13 10:09

Math chiller


People also ask

Does margin auto working on inline-block?

`margin:auto;` doesn't work on inline-block elements.

Why margin Auto is not working?

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.

Do inline elements respect margins?

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.

How do you give margins to inline elements?

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.


2 Answers

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>
like image 59
jsea Avatar answered Oct 04 '22 17:10

jsea


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...).

like image 20
JoostS Avatar answered Oct 04 '22 17:10

JoostS