【问题标题】:How to change Text color in Div without changing border color如何在不更改边框颜色的情况下更改 Div 中的文本颜色
【发布时间】:2015-10-18 05:12:25
【问题描述】:
<div class = "signupsubmit">Continue</div>
<style>
.signupsubmit {
        line-height: 32px;
        position: absolute;
        left: 36px;
        top: 527px;
        font-family: arial;
        font-size: 17px;
        font-weight: 600;
        width: 137px;
        height: 30px;
        border-color: #00297A;
        border-radius: 4px;
        border: 1px;
        border-style: solid;
        background-color: #FFB630;
        text-indent: 30px;
        }
</style>

我正在尝试将文本颜色更改为黑色,但保持边框颜色相同,所以这就是我所做的

 <div class = "signupsubmit">Continue</div>
<style>
.signupsubmit {
        line-height: 32px;
        position: absolute;
        left: 36px;
        top: 527px;
        font-family: arial;
        font-size: 17px;
        font-weight: 600;
        width: 137px;
        height: 30px;
        border-color: #00297A;
        border-radius: 4px;
        border: 1px;
        border-style: solid;
        background-color: #FFB630;
        text-indent: 30px;
        color: white; <!-- NEW LINE OF CODE -->
        }
</style>

我做了color:white,但它也将边框颜色更改为白色。我想保持边框颜色为黑色。

【问题讨论】:

  • @kannan:不需要。速记borderborder-color 设置覆盖为默认值。只需要将border-color 移动到border: 1px 下方或将border: 1px 更改为border-width: 1px

标签: html css colors


【解决方案1】:

对我有用的是使用 shorthand 语法将边框属性组合成 1 行...

.signupsubmit {
    line-height: 32px;
    position: absolute;
    left: 36px;
    top: 527px;
    font-family: arial;
    font-size: 17px;
    font-weight: 600;
    width: 137px;
    height: 30px;
    border: 1px solid #00297A;
    border-radius: 4px;
    background-color: #FFB630;
    text-indent: 30px;
    color: white; <!-- NEW LINE OF CODE -->
}
&lt;div class = "signupsubmit"&gt;Continue&lt;/div&gt;

【讨论】:

    【解决方案2】:

    当您使用速记属性(例如borderall)时,未指定的属性将设置为其默认(初始)值。在这种情况下,默认的border-colorcurrentColor,它会选择当前的颜色——在这种情况下是white。您可以通过在 border 速记属性规范中明确指定颜色来解决此问题,如其他答案中所建议的那样,或者仅将 border: 1px; 更改为 border-width: 1px;

    有关速记属性如何工作的信息,请参阅MDN page

    未指定的值被设置为其初始值。这听起来很有趣,但实际上意味着它覆盖之前设置的值。

    【讨论】:

      【解决方案3】:

      将您的border 合并为一个border:1px solid #00297A;

      .signupsubmit {
        line-height: 32px;
        position: absolute;
        left: 36px;
        top: 527px;
        font-family: arial;
        font-size: 17px;
        font-weight: 600;
        width: 137px;
        height: 30px;
        border-radius: 4px;
        border:1px solid #00297A;
        background-color: #FFB630;
        text-indent: 30px;
        color:#fff;
      }
      &lt;div class="signupsubmit"&gt;Continue&lt;/div&gt;

      【讨论】:

      • 不知道@torazaburo
      • 它看起来和我的代码完全一样,但改变了结果。
      猜你喜欢
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      相关资源
      最近更新 更多