Html 水平对齐图像

Html 水平对齐图像,html,css,alignment,Html,Css,Alignment,如何将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中包含的图像水平对齐?这是一个固定的宽度

示例代码:

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>
<div id="random">
    <img src="1.jpg" />
    <img src="2.jpg" />
    <img src="3.jpg" />
</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>
<div id="random">
    <img src="1.jpg" />
    <img src="2.jpg" />
    <img src="3.jpg" />
</div>

通常,浮动可以很好地将元素水平对齐。在包含(父)元素上与clearfix类结合使用,这样浮动就不会破坏其他页面布局

HTML
.container{
背景#d5d5d5;
}
.集装箱img{
宽度:200px;
保证金:0自动;
显示:块;
}


请注意,clearfix是从Nicolas Gallagher复制的,而HTML5样板文件是从Nicolas Gallagher复制的(请参见:)。这里只有一个图像和一些无效的HTML(
a
不能包含块级元素,如
h2
p
)。您是想将图像与
h2
对齐还是与另一个以相同方式格式化的图像对齐?在HTML5中,链接环绕块级元素是完全有效的。请解释为什么这是一个解决方案,或者至少对您发布的内容添加一些解释
<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;
}​