代码之家  ›  专栏  ›  技术社区  ›  Alpin Cleopatra

如何使用css设置输入焦点边框颜色?

  •  0
  • Alpin Cleopatra  · 技术社区  · 4 年前

    使用此CSS设置

    <!DOCTYPE html>
    <html>
      <head>
        <style>
          input:focus {
            border-color: red;
          }
        </style>
      </head>
      <body>
        <h1>Demo of the :focus selector</h1>
    
        <form>
          First name: <input type="text" name="firstname" /><br />
          Last name: <input type="text" name="lastname" />
        </form>
      </body>
    </html>
    

    我想在关注输入文本时为其设置一个新的颜色。但在Chrome浏览器中,边框变成了蓝色,在Firefox浏览器中,它变成了红色和蓝色。

    https://codesandbox.io/s/twilight-mountain-wq3ox5?file=/index.html

    焦点不适用于边框颜色吗?

    1 回复  |  直到 4 年前
        1
  •  3
  •   Obsidian Age    4 年前

    您希望修改 outline 属性:

    input:focus {
      border-color: red;
      outline: red;
    }
    <h1>Demo of the :focus selector</h1>
    
    <form>
      First name: <input type="text" name="firstname" /><br />
      Last name: <input type="text" name="lastname" />
    </form>

    有一篇关于两者区别的好文章 here 。