如何水平对齐div中包含的图像?这是固定宽度。
示例代码:
HTML
<div id="random">
<a href=""><img src=".jpg" /><span></span></a>
<a href=".html"><h2>Title</h2></a><p>Info</p><p>Dd/Mm/Yyyy</p>
</div>
CSS
#random{
max-width: 650px
}
答案 0 :(得分:1)
通常浮点数可以很好地水平对齐元素。在包含(父)元素上使用与clearfix类的连接,这样浮动不会破坏您的其他页面布局。
<div class="horiz clearfix">
<div><a href="www.google.com"><img src="https://www.pathe.nl/themes/main_theme/gfx/icons/placeholder-large.png"/>
</a></div>
<div><a href="www.stackoverflow.com"><h2>Title</h2></a><p>Info</p><p>Dd/Mm/Yyyy</p></div>
</div>
<div class="horiz clearfix">
<div><a href="www.google.com"><img src="https://www.pathe.nl/themes/main_theme/gfx/icons/placeholder-large.png"/>
</a></div>
<div><a href="www.stackoverflow.com"><h2>Title</h2></a><p>Info</p><p>Dd/Mm/Yyyy</p></div>
</div>
/*
* Clearfix: contain floats
*
* For modern browsers
* 1. The space content is one way to avoid an Opera bug when the
* `contenteditable` attribute is included anywhere else in the document.
* Otherwise it causes space to appear at the top and bottom of elements
* that receive the `clearfix` class.
* 2. The use of `table` rather than `block` is only necessary if using
* `:before` to contain the top-margins of child elements.
*/
.clearfix:before,
.clearfix:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.clearfix:after {
clear: both;
}
/*
* For IE 6/7 only
* Include this rule to trigger hasLayout and contain floats.
*/
.clearfix {
*zoom: 1;
}
.horiz > * {
float: left;
margin-right: 5px;
}
答案 1 :(得分:0)
.container{
background:#d5d5d5;
}
.container img{
width:200px;
margin:0 auto;
display:block;
}
&#13;
<div class="container">
<img src="https://dummyimage.com/200X200/000/fff"/>
</div>
&#13;
答案 2 :(得分:-1)
使用类似的东西:
<强> HTML 强>
<div id="random">
<img src="1.jpg" />
<img src="2.jpg" />
<img src="3.jpg" />
</div>
<强> CSS 强>
#random{
max-width: 650px
}
#random img{
display:inline-block;
width:80px;
}