【问题标题】:Image still has margins when trying to fill a div尝试填充 div 时,图像仍然有边距
【发布时间】:2016-01-19 07:17:28
【问题描述】:

我想要一个图像在我的引导网格布局中完全填充一个 div。但是,当我尝试以下操作时,它仍然会在图像的侧面和底部留下可见的边距。这是我的代码:

<div class="container"
<div class="row equalheight">
        <div class="col-md-8 index-featured-image div-fill">
           <!--featured image -->
            <img src="http://placehold.it/400x200">
        </div>
        <div class="col-md-4 index-featured-about">
            <h2>this is about the featured post.</h2>
            <p>This is the content</p>
        </div>
    </div>
</div>

还有 CSS:

.container {
width: 100%;
height: auto;
}

.col-md-4 {
height: 100%;
background-color: green;
border-top:solid white 20px;
border-bottom:solid white 20px;
overflow: hidden;
}

.col-md-8 {
height: 100%;
background-color: black;
border-right: solid white 20px;
border-top:solid white 20px;
border-bottom:solid white 20px;
}

.col-md-8 img {
width: 100%; 
height: 100%;
}

Here's what it looks like.

感谢您的帮助!几个小时以来,我一直在努力解决这个问题!

【问题讨论】:

  • 首先关闭这个 div &lt;div class="container"&gt;
  • 您是否在任何浏览器的开发人员工具(Firefox 或 Chrome 中的 Ctrl-Shift-I)中检查过这个?这将立即告诉您出了什么问题,并且始终是解决 CSS 布局问题的起点。

标签: html css image twitter-bootstrap


【解决方案1】:

只需将 padding: 0; 添加到包含 img 的 div 中,就像这样 .div-fill{padding: 0;}

见小提琴:https://jsfiddle.net/16q2xr8k/18/

【讨论】:

  • 感谢您的回复!我试过这个,但它只能部分起作用——底部仍然有轻微的填充。另一个 div 或容器是否会干扰填充?
  • 可能有很多不同的原因导致这种情况,如果您提供更多代码,我可以为您查看。
  • 我想通了!我用更大的图像试了一下,效果很好。非常感谢您的意见!