html - 如何让图片占据整个div的高度和宽度?

标签 html css

我定义了以下标题元素:

<div class="header">
    <div class="row">
        <div class="col-md-8 col-xs-12">
            <h3 class="heading">Heading info here</h3>
            <p>...</p>
        </div>
        <div class="col-md-4 hidden-xs header-image"></div>
    </div>
</div>

采用这种风格:

.header-image {
    background-image: url("../img/email.jpg");
}

但是图像并没有占据整个 div,事实上,当我在 Chrome Inspector 中查看它时,它的高度为 1px,宽度为 380px

如何让它占据 div 的整个宽度和高度,而没有内边距或边距?

最佳答案

一个选项是使用 background-size 属性(但是,请记住,低于 9 的 IE 版本不支持此功能):

background-size: 100% 100%;

这将填充 div 的宽度和高度,但仅当 div 也有自己的宽度和高度时才可见:

.header-image {
    width: /* your width */;
    height: /* your height */;
}

关于html - 如何让图片占据整个div的高度和宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135406/

相关文章:

javascript - 使用 css responsive 将 div 中的元素居中

html - css 悬停在链接上 - 不起作用

javascript - Glyphicon 图标未出现在首页和刷新后页面上

html - 如何使用 display inline-block 将元素右对齐?

javascript - CSS 中的图库指示器

javascript - 提交前检查表单输入

javascript - 使用 jQuery 代码创建 jQuery 插件

javascript - 防止侧边栏菜单在页脚下方滚动

html - 使用 Internet Explorer 显示 z-index 时出现问题

html - 非方形 PNG 图像作为 IE8 中的背景图像问题