【问题标题】:Transparent Text Background Overlap透明文本背景重叠
【发布时间】:2017-04-30 03:22:33
【问题描述】:

所以我一直在使用 Chris 的文章 https://css-tricks.com/multi-line-padded-text/ 来帮助我获得一些带有背景的居中文本。唯一的问题是我希望背景是半透明的,我想不出办法。我玩过线高和填充,但无法提出解决方案。

我想要做的是停止每行的背景重叠,但确保每行之间的空间相同。

  <div class="ui-section__component">
   <div class="comp--hero-text">
    <h2>You're all clear, kid. Let's blow this thing and go home! You're all clear, kid. Let's blow this thing and go home!</h2>
   </div>
  </div>


$white: #fff;
$max-width: 1024px;
$small-spacing: 0.75em;

.ui-section__component{
  background-color: green; 
  display: flex; 
  height: 500px;
  width: 700px;
}

.comp--hero-text {
  align-self: center;
  margin: 0 auto;
  max-width: $max-width - 200px;
  text-align: center;
  width: 80%;

  h2 {
    background-color: rgba($white, 0.85);
    box-decoration-break: clone;
    box-shadow: $small-spacing / 2 0 0 rgba($white, 0.85), -$small-spacing / 2 0 0 rgba($white, 0.85);
    display: inline;
    line-height: 1.5;
    padding: 0.5em 0;
  }
}

我的代码可以在 Code Pen 上找到:http://codepen.io/rmaspero/pen/VmVwNx

【问题讨论】:

    标签: html css sass


    【解决方案1】:

    line-height: 1.2 设为.comp--hero-text h2,例如:

    .comp--hero-text h2 {
      line-height: 2.2;
    }
    

    看看下面的sn-p:

    .ui-section__component {
      background-color: green;
      display: flex;
      height: 500px;
      width: 700px;
    }
    
    .comp--hero-text {
      align-self: center;
      margin: 0 auto;
      max-width: 824px;
      text-align: center;
      width: 80%;
    }
    .comp--hero-text h2 {
      background-color: rgba(255, 255, 255, 0.85);
      box-decoration-break: clone;
      box-shadow: 0.375em 0 0 rgba(255, 255, 255, 0.85), -0.375em 0 0 rgba(255, 255, 255, 0.85);
      display: inline;
      line-height: 1.5;
      padding: 0.15em 0;
    }
    <div class="ui-section__component">
      <div class="comp--hero-text">
        <h2>You're all clear, kid. Let's blow this thing and go home! You're all clear, kid. Let's blow this thing and go home!</h2>
       </div>
      </div>

    希望这会有所帮助!

    【讨论】:

    • 问题在于你现在每行之间的间隙是第一行顶部和最后一行底部的两倍
    • @rmaspero 看看我更新的答案,我稍微调整了填充和行高。让我知道这是否适合您。
    【解决方案2】:

    你可以减少'comp--hero-text h2'的行高

    .comp--英雄文本 h2{ 行高:2; }

    或者您可以调整填充

    .comp--英雄文本 h2{ 填充:.2em 0; }

    【讨论】:

    • 问题在于你现在每行之间的间隙是第一行顶部和最后一行底部的两倍。
    【解决方案3】:

    我认为您可以通过消除背景颜色的 alpha 通道并将其更改为 rgb 相似颜色来解决此问题。是我能想到的最简单的解决方案。

        h2 {
            background-color: #f6f9f4;
            box-decoration-break: clone;
            box-shadow: $small-spacing / 2 0 0 rgba($white, 0.85), -$small-spacing / 2 0 0 #f6f9f4;
            display: inline;
            line-height: 1.5;
            padding: 0.5em 0;
          }
    

    【讨论】:

    • 我希望它是半透明的,因为背景会有所不同,并且不会总是块颜色。
    猜你喜欢
    • 2013-06-21
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 2018-06-22
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多