[英]How can I center align my image into div?
I have a div meant to contain all content on said site, an image I have nested into the div doesn't want to center and I'm not sure why? 我的div打算包含上述站点上的所有内容,我嵌套在div中的图像不想居中,而且我不确定为什么吗?
here is the CSS. 这是CSS。
#wrapper {
width: 1000px;
margin: 0 auto;
background-color: #f4f9f1;
}
#intro {
padding-bottom: 10px;
width:80%;
margin:10px auto;
border-bottom: 3px solid #b32230;
}
intro is the id given to said img, it just floats to the left of the div( id is wrapper) and wont center even though I have right and left margins set to auto? 简介是给img的ID,即使我将左右边距设置为auto,它也会浮动在div(id是wrapper)的左侧并且不会居中?
The problem with margin: auto;
margin: auto;
的问题margin: auto;
is that it will only center block level elements ... and img
is by default displayed as inline
and follows the text flow (to the left). 就是它只会使中心块级元素...而img
在默认情况下显示为inline
并遵循文本流(向左)。 If you add display: block
to it it should work: 如果您添加display: block
,它应该可以工作:
#intro {
display: block;
padding-bottom: 10px;
width:80%;
margin:10px auto;
border-bottom: 3px solid #b32230;
}
This way you don't need to centrally align the text of the wrapper element. 这样,您就不需要集中对齐包装元素的文本。
But if you want to center all inline
children of the the wrapper - just use text-align:center
on the #wrapper
... however then you may need to have line breaks before and after the images or some text could end up next to them and push them out of the center =) 但是,如果您想将包装器的所有inline
子项居中-只需在#wrapper
上使用text-align:center
...但是,您可能需要在图像前后插入换行符,否则某些文本可能会#wrapper
在旁边他们,并将其推出中心=)
Use text-align: center
on main container: 使用text-align: center
在主容器上text-align: center
:
#wrapper {
width: 1000px;
margin: 0 auto;
background-color: #f4f9f1;
text-align: center;/*Add text-align center*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.