html - :before pseudo element is not staying in div

标签 html css

我使用伪元素 :before 来帮助固定背景图片,因为如果不这样做,由于背景附件重绘功能,它会导致我的网站滚动缓慢。我修复了背景图像以使其运行流畅,但现在背景图像溢出整个文档,而不是停留在 div 中。

我尝试过使用不同的溢出选项,并尝试将控制伪元素的英雄类放在不同的元素上,看看是否有帮助。

https://jsfiddle.net/4prhubwy/

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ukiyo Sushi ツ</title>
    <link href = "/style.css" type = "text/css" rel = "stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Fira+Sans&display=swap" rel="stylesheet">
</head>
<body>
    <div class = "hero">
        <header>
            <nav class = "navbar">
                <a href = "#" class = "logo">Ukiyo Sushi ツ</a>
                <ul>
                    <li><a href ="#" class = "about">About us</a></li>
                    <li><a href = "#" class = "menu">Menu</a></li>
                    <li><a href = "#" class = "services">Services</a></li>
                    <li><a href = "#" class = "contact">Contact</a></li>
                </ul>
            </nav> 
            <div class = "sushiPlatter">
                <h2>Chef's Special Sushi Platter</h2>
                <br>
                <a href = "#">View Menu</a>
            </div>
        </header>
    </div>
    <section class = "idkYet">
        <div>
            <span>hello</span>
        </div>    
    </section>
</body>
</html>

CSS:

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

a{
    text-decoration: none;
    color: white;
    /*overflow: hidden;*/
}

.hero{
    position: relative;
    overflow: hidden;
    min-height: 100%;
}

.hero::before{
    background-image: url("/img/header.jpg");
    background-repeat: no-repeat;
    background-position: center top;
    background-size: cover;
    content: '';
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    will-change: transform;
    z-index: -1;

}

body{
    font-family: "Fira Sans", sans-serif;
    font-size: 2rem;
    color: white;
}

.logo{
    font-size: 3rem;
    margin: .5em;
    border-bottom: 2px solid transparent;
}

.logo:hover{
    border-bottom: 2px solid white;
}

.navbar{
    /*position: fixed;*/
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
}

.navbar  ul{
    display: inline-flex;
    list-style: none;
    margin-left: auto;
}

.navbar li{
    margin: 1.15em;   
    border-bottom: 2px solid transparent;
}

.about:hover, .menu:hover, .services:hover, .contact:hover{
    border-bottom: 2px solid white;
}

.sushiPlatter{
    font-size: 1rem;
    text-align: left;   
    display: flex;
    height: 100vh;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    margin: 2.15em 2em;
}

.sushiPlatter h2{
    font-weight: bold;
}

.sushiPlatter a{
    margin: 0em 7em;
} 

.sushiPlatter a:hover{
    opacity: .25;
    transition: 1s;
}

section{
    height: 100vh;
    color: black;
}

.idkYet{
    color: white;
    display: block;
}

最佳答案

为元素的其余部分添加背景,以便在其顶部滚动时隐藏伪元素,例如此处的部分:

例子

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
/* selector moved here to be seen */
section {
  height: 100vh;
  color: black;
  background: white;/* rule added , use your own color or img */
}
/* end selector moved */
a {
  text-decoration: none;
  color: white;
  /*overflow: hidden;*/
}

.hero {
  position: relative;
  overflow: hidden;
  min-height: 100%; 
}

.hero::before {
  background-image: url(https://i.imgur.com/bs8H4p7.jpg);
  background-repeat: no-repeat;
  background-position: center top;
  background-size: cover;
  content: '';
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  will-change: transform;
  z-index: -1;
}

body {
  font-family: "Fira Sans", sans-serif;
  font-size: 2rem;
  color: white;
}

.logo {
  font-size: 3rem;
  margin: .5em;
  border-bottom: 2px solid transparent;
}

.logo:hover {
  border-bottom: 2px solid white;
}

.navbar {
  /*position: fixed;*/
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
}

.navbar ul {
  display: inline-flex;
  list-style: none;
  margin-left: auto;
}

.navbar li {
  margin: 1.15em;
  border-bottom: 2px solid transparent;
}

.about:hover,
.menu:hover,
.services:hover,
.contact:hover {
  border-bottom: 2px solid white;
}

.sushiPlatter {
  font-size: 1rem;
  text-align: left;
  display: flex;
  height: 100vh;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  margin: 2.15em 2em;
}

.sushiPlatter h2 {
  font-weight: bold;
}

.sushiPlatter a {
  margin: 0em 7em;
}

.sushiPlatter a:hover {
  opacity: .25;
  transition: 1s;
}



.idkYet {
  color: white;
  display: block;
}
<div class="hero">
  <header>
    <nav class="navbar">
      <a href="#" class="logo">Ukiyo Sushi ツ</a>
      <ul>
        <li><a href="#" class="about">About us</a></li>
        <li><a href="#" class="menu">Menu</a></li>
        <li><a href="#" class="services">Services</a></li>
        <li><a href="#" class="contact">Contact</a></li>
      </ul>
    </nav>
    <div class="sushiPlatter">
      <h2>Chef's Special Sushi Platter</h2>
      <br>
      <a href="#">View Menu</a>
    </div>
  </header>
</div>
<section class="idkYet">
  <div>
    <span>hello</span>
  </div>
</section>

关于html - :before pseudo element is not staying in div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58624164/

相关文章:

javascript - <span> 光标未按预期工作

css - 媒体查询的写法

css - 如何在一个div中包含一个div?

html - 如何设置 SVG 滤镜的 alpha?

html - 路径和 G 元素进入 Wordpress 中的 <p> 标签

css - 使用位置固定的问题

javascript - 带有 svg 和(双击)单击事件的 <object> 标签

html - 试图为消息实现这种样式

html - 水平滚动时固定宽度

html - 网页中的图片列表