html - 在固定图像上使用 div 作为 mask

标签 html css overflow css-position

我想对我正在设计的网页进行特定设计。

主包装包含一系列 <div class='section'><div class='section-header'> . section-header应该在图像上显示该部分的标题。

例子:

<div id="tag" class="section-header">
    <h1>Title</h1>
    <img src="assets/img/some_image.jpg">
</div>

到目前为止,我的 CSS 是:

.section-header
{
    width:            100%;
    height:           192px;
    overflow:         hidden;
}
.section-header > *
{
    width:            100%;
    line-height:      192px;
    margin:           0;
}
.section-header > h1
{
    position:         absolute;
    z-index:          10000;
    text-align:       center;
}
.section-header > img
{
    filter:           opacity(50%);
}

但是我想在背景图像和 section-header 之间添加一些相对运动.我基本上想用 position: fixed; 将图像固定到屏幕上并让 overflow: none;做这份工作。

然而,一旦我添加 position: fixed; top: 0; 似乎至 .section-header > img ,溢出不再隐藏,图像可见,无论它嵌套在标题的位置如何。

我该如何解决?

编辑: 开发代码可见here .我基本上在每个部分的标题后面都有图像,而不是在页面上滚动,只是让该部分的标题在您滚动时显示它

最佳答案

如果我理解你想要的效果,你可能需要使用img作为背景;试试这个:

body{
  background: #f3f3f3;
  height:800px;
}
div {
  text-align:center;
  color:white;
  width:80%;
  margin:150px auto;
  line-height:200px;
  background:url('http://lorempixel.com/600/600') no-repeat center;
  background-attachment: fixed;
}
div h1 {
  font-size:3em;
}
<div>
  <h1>Mytitle</h1>
</div>


Jsfiddle Demo

关于html - 在固定图像上使用 div 作为 mask ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39514317/

相关文章:

html - IE <a> 元素 float left 由于某种原因首先移动

css - 视频链接打不开

c++ - 比较C或C++中两个浮点值的总和

html - 如何使条形图不被谷歌条形图中的大量数据压碎?

html - Helvetica 或 Arial 作为 CSS 中的基本字体?

html - Nginx 服务器不从骨架框架加载 css 文件

html - 在 Codeigniter 中单击按钮时从数据库添加卡片元素

jquery - 响应式网站 - 幻像宽度导致水平滚动条

css - 下拉菜单,其中溢出隐藏在父内部

html - CSS transform RotateY() 不适用于动画?