代码之家  ›  专栏  ›  技术社区  ›  Octavian Niculescu

将文本设置为另一文本的居中背景

  •  2
  • Octavian Niculescu  · 技术社区  · 3 年前

    photo

    我一直想自己做,但我真的不知道怎么做。

    以下是我目前的结果:

    .quiz-question {
        position: relative;
        font-size: 4rem;
    }
    
    .quiz-question::before {
       content: "?";
       font-size: 7rem;
       position: absolute;
       top: -40%;
       left: 0;
       bottom: 0;
       right: 0;
       z-index: -1;
       overflow: hidden;
       text-align: center;
    }
    <div class="quiz-question">
                This is a test text.
    </div>

    如何解决这些问题?

    2 回复  |  直到 3 年前
        1
  •  3
  •   Lundstromski    3 年前

    .quiz-question {
      position: relative;
      font-size: 4rem;
      
      /* Added by me */
      display: inline-block;
    }
    
    .quiz-question::before {
      content: "?";
      font-size: 7rem;
      position: absolute;
      
      /* Added by me */
      top: 50%;
      transform: translateY(-50%);
      /* bottom: 0; */
      
      left: 0;
      right: 0;
      z-index: -1;
      overflow: hidden;
      text-align: center;
    }
    <div class="quiz-question">
      This is a test text.
    </div>
        2
  •  3
  •   Igor Zinchenko    3 年前

    您可以为之前设置:左上右下0、显示弹性、将项目对齐到中心、对齐资源中心