【问题标题】:Text transparent issue with background color背景颜色的文本透明问题
【发布时间】:2015-01-14 14:59:50
【问题描述】:

我正在制作一个按钮,我想实现一个有趣的效果,你可以在这里看到。

问题是,当我悬停在文本rgba(0,0,0,0.0); 上时,所有按钮都会变成白色,甚至是文本。

到目前为止,这是我的代码:

.button{
    height: 40px;
    border-radius: 5px;
    border: 2px solid #fff;
    text-align: center;
    font-size: 16px;
    color: #fff;
    line-height: 2.4em;
    cursor: pointer;
}

.button:hover{
    background: #fff;
    color: rgba(0,0,0,0.0)
}

【问题讨论】:

标签: css


【解决方案1】:

原因是因为您的悬停color: 设置为透明,所以它当然是白色的。尝试以下更简单的方法:

.button {
  color: blue;
  background: white;
  padding: 5px 10px;
  border: 3px solid blue;
  border-radius: 5px;
  text-decoration: none;
  transition: all 0.3s;
}
.button:hover {
  color: white;
  background: blue;
}
<a href="alink.html" class="button">button</a>

【讨论】:

  • 如果我这样做,文本在悬停时将其颜色设置为蓝色,我希望它是背景的颜色..
  • 如果是背景颜色,它是不可见的,因为它会与背景颜色融为一体。
  • 我指的是背景图片的颜色,不是按钮背景的颜色
  • 这在 CSS 中并不容易做到。 stackoverflow.com/questions/9082909/…
【解决方案2】:

问题在于,您已将文本颜色不透明度设置为0,因此您的文本就像是完全透明的。您可以简单地将它与您的bodybackground 颜色匹配,就像我在下面所做的那样。 opacity就不用说了,默认是1

已修复且可工作的代码 sn-p:

body{
  background: #0E80C6;
}

.button{
    height: 40px;
    border-radius: 5px;
    border: 2px solid #fff;
    text-align: center;
    font-size: 16px;
    color: #fff;
    line-height: 2.4em;
    cursor: pointer;
    background: transparent; /* background changed to transparent so it shows the body's background color */
}

.button:hover{
    background: #fff;
    color: #0E80C6; /* color matched to that of the background */
}
<button class="button">Button</button>

【讨论】:

  • 看我上面的评论,如果我想让文本像背景一样(如果背景不是纯色?)
【解决方案3】:

您的问题是您将文本颜色的不透明度设置为 0%。 rgba 中的最后一个字母表示“alpha”或不透明度。

body {
  background-color: rgba(42, 148, 245, 1.00);
}
.button {
  font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
  background-color: transparent;
  height: 40px;
  border-radius: 5px;
  border: 2px solid #fff;
  text-align: center;
  font-size: 16px;
  color: #fff;
  line-height: 2.4em;
  padding-left: 40px;
  padding-right: 40px;
  cursor: pointer;
}
.button:hover {
  background: #fff;
  color: rgba(42, 148, 245, 1.00);/*Set this to whatever you have for the background color.  the "1.00" is the opacity"*/
}
<input type="button" value="button" class="button">

【讨论】:

    【解决方案4】:

    RGBA(红、绿、蓝、阿尔法)

    问题是文本的不透明度(alpha 值)为 0

    body{
      background: blue
    }
    .button{
        background: transparent;
        height: 40px;
        border-radius: 5px;
        border: 2px solid #fff;
        text-align: center;
        font-size: 16px;
        color: #fff;
        line-height: 2.4em;
        cursor: pointer;
    }
    
    .button:hover{
        background: #fff;
        color: blue
    }
    <button class="button">Button</button>

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2012-11-05
      • 2012-06-26
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多