代码之家  ›  专栏  ›  技术社区  ›  Benji

-webkit背景剪辑:文本和垂直居中文本

  •  1
  • Benji  · 技术社区  · 7 年前

    -webkit-background-clip: text 相应的IE回退功能对剪辑效果很好,但每次我都尝试使用 display: inline-block position: absolute 文本立即消失。

    body {
      margin: 1rem;
    }
    
    .box {
      height: calc(100vh - 2rem);
      border: 1px solid;
      background-image: url(http://lorempixel.com/1280/720/);
      -webkit-background-clip: text;
    }
    
    .align {
      /* to right-align the text box */
      text-align: right;
      
      /* uncomment below to see the issue */
      /* display: inline-block; */
    }
    
    .title {
      /* Duplicated here for Safari */
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      color: yellow;
      
      margin: 0;
      font: bold 6rem/1 sans-serif;
      text-transform: uppercase;
      text-align: center;
    }
    <article class="box">
      <div class="align">
        <h1 class="title">The Title</h1>
    </article>

    我创造了 a Codepen with an example . 如果您取消注释 .align 类或尝试将其垂直居中 位置:绝对位置 transform 例如,文本消失。

    我肯定我错过了一些简单的东西,任何建议都非常感谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Benji    7 年前

    看起来这确实是一个已知问题,在Chrome 63中得到了解决!