【问题标题】::hover pseudo class not working on some elements:hover 伪类不适用于某些元素
【发布时间】:2021-12-26 12:47:42
【问题描述】:

我在 .content 中放置了一个 :hover 选择器,但它不起作用,而是当它被悬停在背景上时,悬停选择器就会起作用。当 .showcase:hover 都从 css 中删除时,它就会生效。相同的原因是什么?如果我同时需要 .content:hover 和 .showcase:hover 怎么办?

body {
  background: rgba(0, 0, 0, 0.9);
  margin: 0;
  color: #fff;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

.showcase::before {
  content: "";
  height: 100vh;
  width: 100%;
  background-image: url(https://image.ibb.co/gzOBup/showcase.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  display: block;
  filter: blur(10px);
  -webkit-filter: blur(10px);
  transition: all 1000ms;
}

.content:hover{
  filter: blur(10px); /* why is this effect not working */
}

.showcase:hover::before {
  filter: blur(0px);
  -webkit-filter: blur(0px);
}

.showcase:hover .content {
  filter: blur(2px);
  -webkit-filter: blur(2px);
}

.content {
  position: absolute;
  z-index: 1;
  top: 10%;
  left: 50%;
  margin-top: 105px;
  margin-left: -145px;
  width: 300px;
  height: 350px;
  text-align: center;
  transition: all 1000ms;
}

.content .logo {
  height: 180px;
  width: 180px;
}

.content .title {
  font-size: 2.2rem;
  margin-top: 1rem;
}

.content .text {
  line-height: 1.7;
  margin-top: 1rem;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="index.css" />
    <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.15.4/css/all.css"
      integrity="sha384-DyZ88mC6Up2uqS4h/KRgHuoeGwBcD4Ng9SiP4dIRy0EXTlnuz47vAwmeGwVChigm"
      crossorigin="anonymous"
    />
    <title>Blur Landing</title>
  </head>

  <body>
    <header class="showcase">
      <div class="content">
        <img
          src="https://image.ibb.co/ims4Ep/logo.png"
          class="logo"
          alt="Traversy Media"
        />
        <div class="title">Welcome To Traversy Media</div>
        <div class="text">
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Vitae, vel.
        </div>
      </div>
    </header>
      </body>
</html>

【问题讨论】:

    标签: html css hover


    【解决方案1】:

    你的选择器

    .content:hover{
      filter: blur(10px);
    }
    

    被另一个选择器覆盖

    .showcase:hover::before {
      filter: blur(0px);
      -webkit-filter: blur(0px);
    }
    

    无论您是否将鼠标悬停在它上面,showcase 选择器都会改为生效。

    解决方案是设置更具体的悬停选择器。

    .content:hover, .showcase:hover .content:hover {
      filter: blur(10px); // Covers the "double" hover.
    }
    

    body {
      background: rgba(0, 0, 0, 0.9);
      margin: 0;
      color: #fff;
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    }
    
    .showcase::before {
      content: "";
      height: 100vh;
      width: 100%;
      background-image: url(https://image.ibb.co/gzOBup/showcase.jpg);
      background-size: cover;
      background-repeat: no-repeat;
      background-position: center;
      display: block;
      filter: blur(10px);
      -webkit-filter: blur(10px);
      transition: all 1000ms;
    }
    
    .content:hover, .showcase:hover .content:hover {
      filter: blur(10px);
    }
    
    .showcase:hover::before {
      filter: blur(0px);
      -webkit-filter: blur(0px);
    }
    
    .showcase:hover .content {
      filter: blur(2px);
      -webkit-filter: blur(2px);
    }
    
    .content {
      position: absolute;
      z-index: 1;
      top: 10%;
      left: 50%;
      margin-top: 105px;
      margin-left: -145px;
      width: 300px;
      height: 350px;
      text-align: center;
      transition: all 1000ms;
    }
    
    .content .logo {
      height: 180px;
      width: 180px;
    }
    
    .content .title {
      font-size: 2.2rem;
      margin-top: 1rem;
    }
    
    .content .text {
      line-height: 1.7;
      margin-top: 1rem;
    }
    <header class="showcase">
          <div class="content">
            <img
              src="https://image.ibb.co/ims4Ep/logo.png"
              class="logo"
              alt="Traversy Media"
            />
            <div class="title">Welcome To Traversy Media</div>
            <div class="text">
              Lorem ipsum dolor sit amet consectetur adipisicing elit. Vitae, vel.
            </div>
          </div>
        </header>

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多