【问题标题】:Can I hide text in container until user hover over it? If yes how?我可以在容器中隐藏文本直到用户将鼠标悬停在它上面吗?如果是如何?
【发布时间】:2022-11-16 21:49:24
【问题描述】:

我制作了一个容器,容器中有文本和背景图像。文本位于 h3 标签中。我希望文本保持隐藏状态,只显示图像。当用户将鼠标悬停在容器上时,我想显示文本和背景图像必须有点透明。 我怎样才能做到这一点?? 到目前为止,这是我的 CSS 代码...我还附上了我正在使用的图片Image I'm using for this code

.container{
    
    background-size: cover;
    background-repeat: no-repeat;
    margin-top: 100px;
    padding: 18px 40px;
    font-size: 22px;
    text-align: center;
    width: 250px;
    height: 250px;
    border-radius: 35px;
    
    color: transparent;
    line-height: 200px;
    float: left;
    margin-left: 20%;
    background-image: url(/Unstitched.jpeg.jpg);
}
.container:hover{
    background: rgba(255,0,0,0.3) ;
    color: black
}

【问题讨论】:

    标签: css containers background-image opacity transparent


    【解决方案1】:

    你或许可以这样做:

    .container {
      background-size: cover;
      background-repeat: no-repeat;
      margin-top: 100px;
      padding: 18px 40px;
      font-size: 22px;
      text-align: center;
      width: 250px;
      height: 250px;
      border-radius: 35px;
      color: transparent;
      line-height: 200px;
      float: left;
      margin-left: 20%;
      background-image: url(https://i.stack.imgur.com/MLu3i.jpg);
    }
    
    .container:hover {
      background: rgba(255, 0, 0, 0.3);
      color: black
    }
    
    .container h3 {
      display: none;
    }
    
    .container:hover h3 {
      display: block;
    }
    <div class="container">
      <h3>My invisible Text</h3>
    </div>

    相关的变化是:

    .container h3 {
        display: none;
    }
    
    .container:hover h3 {
        display: block
    }
    

    这使得 h3 标签不可见,直到有人将鼠标悬停在容器元素上。

    【讨论】:

      【解决方案2】:

      在这里你可以试试这个逻辑:


      <!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" />
          <title>Document</title>
        </head>
        <style>
          .container {
            width: 200px;
            height: 200px;
            background-color: rgb(67, 153, 228);
            top: 25%;
            left: 45%;
            position: absolute;
          }
      
          .container:hover * {
            display: none;
          }
        </style>
        <body>
          <div class="container">
            <h2>HELLO</h2>
            <h3>HELLO</h3>
          </div>
        </body>
      </html>

      【讨论】:

        【解决方案3】:

        稍微更改了您的 css 规则,但主要是我使用 :hover 上的不透明度 css 属性来更改悬停时的透明度。我也从网上挑了第一张cors friendly的图片来做背景处理的真实图片。

        行为如您所料:直到容器元素悬停时才显示文本,此时不透明度设置为较暗。缺点是使包括子元素在内的整体不透明度变暗,而不仅仅是背景。为了让它变得更好,它需要类似 ::before 规则的东西来在容器内添加一个样式元素,该容器包含背景,其不透明度将与其余内容分离。

        .container{    
            background-size: cover;
            background-repeat: no-repeat;    
            background-image: url(https://thumbs.dreamstime.com/z/cors-caron-boardwalk-across-bog-near-tregaron-wales-62354242.jpg);
            
            font-size: 22px;
            text-align: center;
            width: 250px;
            height: 250px;
            border-radius: 35px;    
            color: transparent;
        }
        .container:hover{    
            color: black;
            opacity: 0.6;
        }
        <div class="container">
           <h3>Caption text</h3>
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-12
          • 1970-01-01
          • 2014-06-10
          • 1970-01-01
          • 1970-01-01
          • 2016-04-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多