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

如何在标头中正确定位此CSS伪元素?

css
  •  0
  • TinyTiger  · 技术社区  · 5 年前

    h1 标题。

    我使用CSS伪元素来实现这一点。

    但是,我如何使CSS伪元素出现在“小菜一碟”这个词后面,而不是其他任何东西?

    .yellow-highlight {
      white-space: nowrap;
    }
    
    .yellow-highlight:before {
      content: " ";
      position: absolute;
      width: 100%;
      height: 1em;
      left: -0.25em;
      top: 0.1em;
      padding: 0 0.25em;
      background-color: #FEFDBD;
      transform: rotate(-2deg);
      z-index: -1;
    }
    <h1>Blah Blah Blah Blah Blah Blah Blah <span class="yellow-highlight">Piece of Cake</span></h1>
    1 回复  |  直到 5 年前
        1
  •  3
  •   wizzfizz94    5 年前

    加上 position: relative; .yellow-highlight 选择器。

    .yellow-highlight {
      position: relative;
      white-space: nowrap;
    }
    
    .yellow-highlight:before {
      content: " ";
      position: absolute;
      width: 100%;
      height: 1em;
      left: -0.25em;
      top: 0.1em;
      padding: 0 0.25em;
      background-color: #FEFDBD;
      transform: rotate(-2deg);
      z-index: -1;
    }
    <h1>Blah Blah Blah Blah Blah Blah Blah <span class="yellow-highlight">Piece of Cake</span></h1>