Reputation: 4167
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>
Upvotes: 43
Views: 53572
Reputation: 931
I wanted to make 2 inline divs in same line together and one of them in most right, I used width to set the first div width to 70% and remaining for the second div, I also used white-space:nowrap; to prevent wrap divs in next line (to be responsive)
this is my example
<div style="border:1px solid black;white-space:nowrap;">
<div style="border:1px solid black;display:inline-block;width:70%;max-width:70%;word-break:break-word;">
<div style="border: 1px solid blue;word-break: break-word;white-space: normal;">
hello hi hi hi;hello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hi</div>
</div>
<div style="border:1px solid black;display:inline-block;width:28%;height:100%;vertical-align:top;word-break:break-word;white-space;">Right Side Inline-Div</div>
</div>
! note white-space:normal used to prevent the inherited white-space and allow for word-break in the child containers
Upvotes: 0
Reputation: 1667
margin-left:50%;
transform: translateX(-50%);
.container{
border:solid 1px red;
}
.container img{
display:inline-block;
margin-left:50%;
transform: translateX(-50%);
}
<div class="container">
<img src="https://placekitten.com/100/300" />
</div>
Upvotes: 4
Reputation: 2968
For elements with property display: inline-block; A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'. [reference: CSS2§10.3.9]
Upvotes: 11
Reputation: 4037
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>
Upvotes: 57
Reputation: 12592
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...).
Upvotes: 39