html - 如何使用 div 使多个图像水平居中并响应?

标签 html css

如何使 div 中的一行图像具有响应性?我的目标是让一行图像(以网页为中心)根据屏幕大小进行缩放。我到目前为止的 (2) 个解决方案就像 this , 但还不够好。我尝试:

  • 让它在页面上居中
  • 当屏幕较小时(智能手机),图像应该变小
  • 保持在一行中,而不是通过调整大小进入垂直行
  • 多张图片之间留空
  • 原始图像的大小无关紧要。

我不是编程高手,所以....

提前致谢

丹尼斯

<div style="width: 100%; clear: center;">
<div style="text-align: center;"><a class="hoverborder" href="" rel="alternate"><img class="img-rounded" src="http://placehold.it/150x150" alt="" width="100" height="70" /></a>  

<div style="float: left; text-align: center;"><a class="hoverborder" href="index.php?Itemid=319" rel="alternate"><img class="img-rounded" src="http://placehold.it/150x150" alt="" width="100" height="70" /></a></div>
<div style="float: left; text-align: center;"><a class="hoverborder" href="index.php?Itemid=318" rel="alternate"><img class="img-rounded" src="http://placehold.it/150x150" alt="" /></a></div>
<div style="float: left; text-align: center;"><a class="hoverborder" href="index.php?Itemid=321" rel="alternate"><img class="img-rounded" src="http://placehold.it/150x150" alt="" /></a></div>
<div style="float: left; text-align: center;"><a class="hoverborder" href="index.php?Itemid=322" rel="alternate"><img class="img-rounded" src="http://placehold.it/150x150" alt="" /></a></div>

最佳答案

有很多方法可以做到这一点。它们在某种程度上取决于页面中图像周围的内容。我倾向于避免 float ,因为它们会带来其他挑战,而且根本没有必要。

.image-row {
    text-align: center;
}
.image-box {
    width: 20%;
    padding: 1%;
    margin: .5%;
    background: pink;
    display: inline-block;
    font-size: 0; /* fixes bottom padding */
}
.image-box img {
    max-width: 100%;
}
<div class="image-row">
    <div class="image-box">
        <img src="http://placehold.it/500x500" />
    </div>
    <div class="image-box">
        <img src="http://placehold.it/800x500" />
    </div>
    <div class="image-box">
        <img src="http://placehold.it/500x500" />
    </div>
    <div class="image-box">
        <img src="http://placehold.it/500x200" />
    </div>
</div>

Fiddle demo

关于html - 如何使用 div 使多个图像水平居中并响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29041359/

相关文章:

html - 子域的 DNS 预取

css - Nivo slider css 下拉菜单冲突

javascript - 如何通过 JavaScript/JQuery 将 HTML 文档添加到 <div></div> 中?

html - GWT 中的自定义滚动条

javascript - 如何使用js跳过div?

javascript - 在另一个页面上克隆整个 div 容器

javascript - 如何使用 JavaScript 截取 div 的屏幕截图?

html - 在 YSLOW 中使用 gtmetrix.com 进行测试显示远期过期错误

php - 更改 PHP 输出的特定字符的颜色

CSS 在窗口调整大小时调整 div 和 div 内的图像