-1

如何水平对齐 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
}
4

3 回答 3

1

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

HTML

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

CSS

/*
 * 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;
}​
于 2013-01-01T12:50:22.420 回答
0

.container{
  background:#d5d5d5;
  }
.container img{
  width:200px;
  margin:0 auto;
  display:block;
  }
<div class="container">
  <img src="https://dummyimage.com/200X200/000/fff"/>  
</div>

于 2016-12-20T13:06:50.360 回答
-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;
}
于 2013-01-01T12:16:26.883 回答