【发布时间】:2017-08-17 05:33:57
【问题描述】:
到目前为止,我已经得到了这段代码(没有可见的标题图像):
.wrap {
margin: 50px auto;
}
html,
body {
height: 100%;
}
body {
background-image: url('https://placebear.com/1000/800');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-attachment: fixed;
}
.header {
background-image: url('https://placebear.com/940/248');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
<div class="wrap">
<div class="row">
<div class="medium-12 columns header"></div>
</div>
</div>
如果你给 header-image 一个 248px 的固定大小,你可以看到它出现。带有可见标题图像的示例:
.wrap {
margin: 50px auto;
}
html,
body {
height: 100%;
}
body {
background-image: url('https://placebear.com/1000/800');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-attachment: fixed;
}
.header {
background-image: url('https://placebear.com/940/248');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
height: 248px;
border: 3px solid white;
}
<div class="wrap">
<div class="row">
<div class="medium-12 columns header"></div>
</div>
</div>
有没有办法让它在不使用固定高度的情况下出现?
就像使用带有 src- 和 alt-attribute 的经典 img-tag 时一样。然后从图像文件中读取高度。
我想避免那里的固定高度,因为如果图像发生变化,那就完全错了。
【问题讨论】:
-
在how to get div height to auto-adjust to background size 上查看接受的答案。如果您可以将
<img>添加到 HTML 中,它将满足您的需求。否则,您将需要 javascript 到 get the size of a CSS background image 并动态更改 div 高度。 -
@Marvin 非常感谢。 :)