html - 将包含大小相对于页面宽度的图像的 div 居中

标签 html css centering

我已经尝试过其他线程中的解决方案,但我不知所措。 我想要并排的两张图片,每张图片的宽度等于页面宽度的 40%,并且它们之间有 20px 的边距,整个 wazoo 应该居中。 大致图示:

|            [img1 width:40%] 20px space [img2 width:40%]            |

我已经非常接近了,但没有什么能奏效的。 这是我得到的最接近的:

<!-- Starts centering with the addition of "width" to the div's style. -->
<div style="margin: 0 auto;">
    <img width=40% src="http://bit.ly/1mlbOp6" style="display: inline-block;">
    <img width=40% src="http://bit.ly/1mlbOp6" style="display: inline-block; margin-left: 20px;">
</div>

JSFiddle 上的这段代码:http://jsfiddle.net/98Se3/

如果没有指定宽度的div,它坚决拒绝居中。但是,div 的宽度既不能相对于页面宽度设置,也不能精确地以像素为单位,因为它应该是 40% + 20px + 40%。 我错过了什么?

最佳答案

只需对父 div 使用 text-align:center:

<div style="text-align:center">
    <img width="40%" src="http://bit.ly/1mlbOp6">
    <img width="40%" src="http://bit.ly/1mlbOp6" style="margin-left: 20px;">
</div>

看看这个 working fiddle

关于html - 将包含大小相对于页面宽度的图像的 div 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25154368/

相关文章:

html - 我的下拉菜单有什么问题?

css - 水平居中页面的全部内容

css - 在图像顶部居中 div

html - 缩放时仅从右侧剪切图像

html - 下拉框中溢出文本的省略号

PHP 函数在尝试在 HTML 上显示时出现异常

javascript - 如何在javascript中添加一个按钮(带有类属性)点击回调

javascript - 打印带有已单击的 div 的 HTML 页面并隐藏其余部分

CSS3 : what is right syntax to get gradient like on image?

html - 在动态文章文本中 float 一个 Div