css - 无法在位置绝对 div 上添加背景图片

标签 css

你能看看this demo吗?让我知道为什么我无法将图像添加到 #img-box

html, body {
    height:100%;
    width:100%;
}
#wrapper {
    position: relative;
    height: 100%;
    width: 100%;
}
#img-box {
    position: absolute;
    background-image: url("http://www.bluearthrenewables.com/wp-content/uploads/2014/12/7_Intake-Construction_CullitonCreek.jpg");
    top:0;
    left:0;
    background-size: cover;
    background-repeat: no-repeat;
    background-attachment: fixed;
}

最佳答案

因为 #img-box 没有任何与之关联的宽度和高度。尝试定义

html, body {
    height:100%;
    width:100%;
}
#wrapper {
    position: relative;
    height: 100%;
    width: 100%;
}
#img-box {
    position: absolute;
    background-image: url("http://www.clker.com/cliparts/2/0/f/b/13873752061098664878happy_smiley-th.png");
    height: 100%;
    width: 100%;
    top:0;
    left:0;
    background-size: cover;
    background-repeat: no-repeat;
    background-attachment: fixed;
}
<div id="wrapper">
    <div id="img-box"></div>
    <div id="tets"></div>
    <div id="tes3"></div>
</div>

关于css - 无法在位置绝对 div 上添加背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393687/

相关文章:

javascript - 如何使用 Javascript 更改 HTML 标签

jquery - 我怎样才能让我的 div 在第二次点击时恢复到原来的背景颜色?

css - Vue - 使用大括号语法在 css 中绑定(bind)数据

html - CSS :first-child with class selector picking up wrong elements to style?

jquery - 使用 jQuery 实现内联文本元素的交叉淡入淡出

html - 如何将div彼此对齐

javascript - 如何在用户输入的输入类型=文本中附加 % 符号?

javascript - 选择文本并将其隐藏到 div 的末尾

javascript - HTML 中 <div> 元素的默认 z-Index 是什么,如何使用 JavaScript 获取它?

css - Chrome 开发工具/检查器 : Is there a keyboard shortcut to switch between css properties in the Styles tab?