【问题标题】:How to remove the border highlight on an input text element如何删除输入文本元素上的边框突出显示
【发布时间】:2010-11-30 06:43:58
【问题描述】:

当一个 HTML 元素被“聚焦”(当前被选中/进入)时,许多浏览器(至少 Safari 和 Chrome)会在它周围放置一个蓝色边框。

对于我正在处理的布局,这会分散注意力并且看起来不正确。

<input type="text" name="user" class="middle" id="user" tabindex="1" />

Firefox 似乎没有这样做,或者至少,我可以通过以下方式控制它:

border: x;

如果有人能告诉我 IE 的表现如何,我会很好奇。

让 Safari 移除这一点点耀斑会很好。

【问题讨论】:

    标签: css input safari webkit border


    【解决方案1】:

    Edit 2021:你现在可以使用这个:https://github.com/WICG/focus-visible

    删除所有焦点样式通常对可访问性和键盘用户不利。但是轮廓很丑陋,并且为每个交互元素提供自定义的焦点样式可能会很痛苦。

    因此,我发现的最佳折衷方案是仅在我们检测到用户正在使用键盘进行导航时才显示大纲样式。基本上,如果用户按下 TAB,我们会显示轮廓,如果他使用鼠标,我们会隐藏它们。

    它不会阻止您为某些元素编写自定义焦点样式,但至少它提供了一个很好的默认值。

    这就是我的做法:

    // detect keyboard users
    
    const keyboardUserCssClass = "keyboardUser";
    
    function setIsKeyboardUser(isKeyboard) {
      const { body } = document;
      if (isKeyboard) {
       body.classList.contains(keyboardUserCssClass) || body.classList.add(keyboardUserCssClass);
      } else {
       body.classList.remove(keyboardUserCssClass);
      }
    }
    
    // This is a quick hack to activate focus styles only when the user is
    // navigating with TAB key. This is the best compromise we've found to
    // keep nice design without sacrifying accessibility.
    document.addEventListener("keydown", e => {
      if (e.key === "Tab") {
       setIsKeyboardUser(true);
      }
    });
    document.addEventListener("click", e => {
      // Pressing ENTER on buttons triggers a click event with coordinates to 0
      setIsKeyboardUser(!e.screenX && !e.screenY);
    });
    
    document.addEventListener("mousedown", e => {
      setIsKeyboardUser(false);
    });
    body:not(.keyboardUser) *:focus {
      outline: none;
    }
    <p>By default, you'll see no outline. But press TAB key and you'll see focussed element</p>
    <button>This is a button</button>
    <a href="#">This is anchor link</a>
    <input type="checkbox" />
    <textarea>textarea</textarea>
    <select/>

    【讨论】:

    • 这是一个彻底的方法。 click 监听器很不错。
    【解决方案2】:

    焦点上的文本区域可能有 box-shadow.. 可以这样删除:

    textarea:focus{
        outline: none!important;
        box-shadow: none!important;
    }
    

    【讨论】:

      【解决方案3】:

      唯一适合我的解决方案

      边框实际上是一个阴影。所以要隐藏它,我必须这样做:

      input[type="text"]:focus{
           box-shadow: 0 0 0 rgb(255, 255, 255);
      }
      
       input[type="checkbox"]:focus{
            box-shadow: 0 0 0 rgb(255, 255, 255);
       }
      

      【讨论】:

        【解决方案4】:

        2020 年更新 - :focus-visible

        可访问性的好消息 - Chrome 和 Firefox 刚刚添加了对 :focus-visible.

        隐藏焦点样式是不好的做法,因为可访问性要求(键盘导航)会降低您网站的可访问性。

        使用:focus-visible 伪类,让浏览器决定何时应用焦点。

        :focus-visible /* Chrome */
        

        请注意,Firefox 通过一个较旧的前缀伪类支持类似的功能:

        :-moz-focusring /* Firefox */
        

        button {
          color: #000;
          background-color: #fff;
          padding: 10px 16px;
          margin: 10px 0;
          border-radius: 4px;
        }
        
        button:focus {
          box-shadow: 0 0 0 2px #E59700;
          outline: 0;
        }
        
        button:hover {
          background-color: #eee;
        }
        
        button.with-focus-visible:focus:not(:focus-visible) {
          box-shadow: none;
          outline: 0;
        }
        
        button.with-focus-visible:focus-visible, 
        button.with-focus-visible:moz-focusring {
          box-shadow: 0 0 0 2px #E59700;
          outline: 0;
        }
        <p>Click on the button using a mouse. Then try tabbing to the button.</p>
        <button>without :focus-visible</button>
        <button class="with-focus-visible">with :focus-visible</button>

        文档:https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible

        w3 规格:https://www.w3.org/TR/selectors-4/#the-focus-visible-pseudo

        【讨论】:

          【解决方案5】:

          试试这个:

          *:focus {
              outline: none;
          }
          

          这会影响您的所有页面。

          【讨论】:

            【解决方案6】:

            您可以使用 CSS 来禁用它! 这是我用来禁用蓝色边框的代码:

            *:focus {
                outline: none;
            }
            

            This is a working example

            【讨论】:

              【解决方案7】:

              试试这个 CSS,它对我有用

              textarea:focus, input:focus{ border: none; }
              

              【讨论】:

              • 虽然此代码可以解决 OP 的问题,但最好包含关于您的代码如何解决 OP 问题的说明。通过这种方式,未来的访问者可以从您的帖子中学习,并将其应用到他们自己的代码中。 SO 不是编码服务,而是知识资源。此外,高质量、完整的答案更有可能得到支持。这些功能,以及所有帖子都是独立的要求,是 SO 作为一个平台的一些优势,使其与论坛区分开来。您可以编辑以添加其他信息和/或使用源文档补充您的解释。
              【解决方案8】:

              在 Bootstrap 4 中删除边框轮廓可以使用shadow-none,它会删除焦点轮廓。

                          <div class="form-group">
                              <label for="exampleInputEmail1">Name</label>
                              <input type="text" class="form-control form-control shadow-none" 
                              id="exampleInputEmail1"aria-describedby="emailHelp">
                          </div>
              

              【讨论】:

                【解决方案9】:

                在 Firefox 中没有一个解决方案适合我。

                以下解决方案更改了 Firefox 的焦点边框样式,并将其他浏览器的轮廓设置为无。

                我已经有效地将焦点边框从 3 像素蓝色发光变为与文本区域边框匹配的边框样式。以下是一些边框样式:

                虚线边框(边框 2px 红色虚线):

                没有边界! (边框0px):

                Textarea边框(边框1px纯灰色):

                代码如下:

                input:focus, textarea:focus {
                    outline: none; /** For Safari, etc **/
                    border:1px solid gray; /** For Firefox **/
                }
                
                #textarea  {
                  position:absolute;
                  top:10px;
                  left:10px;
                  right:10px;
                  width:calc(100% - 20px);
                  height:160px;
                  display:inline-block;
                  margin-top:-0.2em;
                }
                &lt;textarea id="textarea"&gt;yo&lt;/textarea&gt;

                【讨论】:

                  【解决方案10】:

                  这让我困惑了一段时间,直到我发现这条线既不是边框也不是轮廓,而是阴影。所以要删除它,我必须使用它:

                  input:focus, input.form-control:focus {
                  
                      outline:none !important;
                      outline-width: 0 !important;
                      box-shadow: none;
                      -moz-box-shadow: none;
                      -webkit-box-shadow: none;
                  }
                  

                  【讨论】:

                    【解决方案11】:

                    我尝试了所有答案,但仍然无法让我的答案在Mobile上工作,直到找到-webkit-tap-highlight-color

                    所以,对我有用的是...

                    * { -webkit-tap-highlight-color: transparent; }
                    

                    【讨论】:

                    • 这就是我正在寻找的解决方案。当您使用 li 等元素进行触摸屏体验时,这尤其有用
                    【解决方案12】:

                    您可以使用以下方法移除文本/输入框周围的橙色或蓝色边框(轮廓):outline:none

                    input {
                        background-color: transparent;
                        border: 0px solid;
                        height: 20px;
                        width: 160px;
                        color: #CCC;
                        outline:none !important;
                    }
                    

                    【讨论】:

                      【解决方案13】:

                      这是一个常见的问题。

                      浏览器渲染的默认outline很丑。

                      例如看这个:

                      form,
                      label {
                        margin: 1em auto;
                      }
                      
                      label {
                        display: block;
                      }
                      <form>
                        <label>Click to see the input below to see the outline</label>
                        <input type="text" placeholder="placeholder text" />
                      </form>

                      大多数人推荐的最常见的“修复”是outline:none - 如果使用不当 - 对可访问性来说是灾难。


                      那么……大纲到底有什么用呢?

                      我发现有一个very dry-cut website 很好地解释了一切。

                      它为具有“焦点”的链接提供视觉反馈 使用 TAB 键(或等效键)导航 Web 文档。这是 对于不能使用鼠标或无法使用视觉的人特别有用 减值。如果您删除大纲,您正在制作您的网站 这些人无法进入。

                      好的,让我们尝试与上面相同的示例,现在使用TAB 键进行导航。

                      form,
                      label {
                        margin: 1em auto;
                      }
                      
                      label {
                        display: block;
                      }
                      <form>
                        <label>Click on this text and then use the TAB key to naviagte inside the snippet.</label>
                        <input type="text" placeholder="placeholder text" />
                      </form>

                      请注意,即使不点击输入,您也可以知道焦点在哪里?

                      现在,让我们在可信赖的&lt;input&gt; 上尝试outline:none

                      所以,再次点击文本后使用TAB 键进行导航,看看会发生什么。

                      form,
                      label {
                        margin: 1em auto;
                      }
                      
                      label {
                        display: block;
                      }
                      
                      input {
                        outline: none;
                      }
                      <form>
                        <label>Click on this text and then use the TAB key to naviagte inside the snippet.</label>
                        <input type="text" placeholder="placeholder text" />
                      </form>

                      了解如何更难找出焦点在哪里?唯一的标志是光标闪烁。我上面的例子过于简单化了。在实际情况下,页面上不会只有一个元素。更多类似的东西。

                      .wrapper {
                        width: 500px;
                        max-width: 100%;
                        margin: 0 auto;
                      }
                      
                      form,
                      label {
                        margin: 1em auto;
                      }
                      
                      label {
                        display: block;
                      }
                      
                      input {
                        outline: none;
                      }
                      <div class="wrapper">
                      
                        <form>
                          <label>Click on this text and then use the TAB key to naviagte inside the snippet.</label>
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                        </form>
                      
                        <form>
                          First name:<br>
                          <input type="text" name="firstname"><br> Last name:<br>
                          <input type="text" name="lastname">
                        </form>
                      
                      
                        <form>
                          <input type="radio" name="gender" value="male" checked> Male<br>
                          <input type="radio" name="gender" value="female"> Female<br>
                          <input type="radio" name="gender" value="other"> Other
                        </form>
                      
                      
                      
                        <form>
                          <label for="GET-name">Name:</label>
                          <input id="GET-name" type="text" name="name">
                        </form>
                      
                      
                        <form>
                          <label for="POST-name">Name:</label>
                          <input id="POST-name" type="text" name="name">
                        </form>
                      
                      
                        <form>
                          <fieldset>
                            <legend>Title</legend>
                            <input type="radio" name="radio" id="radio">
                            <label for="radio">Click me</label>
                          </fieldset>
                        </form>
                      
                      </div>

                      如果我们保留大纲,现在将其与相同的模板进行比较:

                      .wrapper {
                        width: 500px;
                        max-width: 100%;
                        margin: 0 auto;
                      }
                      
                      form,
                      label {
                        margin: 1em auto;
                      }
                      
                      label {
                        display: block;
                      }
                      <div class="wrapper">
                      
                        <form>
                          <label>Click on this text and then use the TAB key to naviagte inside the snippet.</label>
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                        </form>
                      
                        <form>
                          First name:<br>
                          <input type="text" name="firstname"><br> Last name:<br>
                          <input type="text" name="lastname">
                        </form>
                      
                      
                        <form>
                          <input type="radio" name="gender" value="male" checked> Male<br>
                          <input type="radio" name="gender" value="female"> Female<br>
                          <input type="radio" name="gender" value="other"> Other
                        </form>
                      
                      
                      
                        <form>
                          <label for="GET-name">Name:</label>
                          <input id="GET-name" type="text" name="name">
                        </form>
                      
                      
                        <form>
                          <label for="POST-name">Name:</label>
                          <input id="POST-name" type="text" name="name">
                        </form>
                      
                      
                        <form>
                          <fieldset>
                            <legend>Title</legend>
                            <input type="radio" name="radio" id="radio">
                            <label for="radio">Click me</label>
                          </fieldset>
                        </form>
                      
                      </div>

                      所以我们建立了以下

                      1. 轮廓很丑
                      2. 移除它们会使生活更加困难。

                      那么答案是什么?

                      去除丑陋的轮廓并添加您自己的视觉提示来指示焦点。

                      这里有一个非常简单的例子来说明我的意思。

                      我删除了轮廓并在:focus:active 上添加了一个底部边框。我还通过在:focus:active 上将它们设置为透明来移除顶部、左侧和右侧的默认边框(个人喜好)

                      form,
                      label {
                        margin: 1em auto;
                      }
                      
                      label {
                        display: block;
                      }
                      
                      input {
                        outline: none
                      }
                      
                      input:focus,
                      input:active {
                        border-color: transparent;
                        border-bottom: 2px solid red
                      }
                      <form>
                        <label>Click to see the input below to see the outline</label>
                        <input type="text" placeholder="placeholder text" />
                      </form>

                      因此,我们用之前的“真实世界”示例尝试上述方法:

                      .wrapper {
                        width: 500px;
                        max-width: 100%;
                        margin: 0 auto;
                      }
                      
                      form,
                      label {
                        margin: 1em auto;
                      }
                      
                      label {
                        display: block;
                      }
                      
                      input {
                        outline: none
                      }
                      
                      input:focus,
                      input:active {
                        border-color: transparent;
                        border-bottom: 2px solid red
                      }
                      <div class="wrapper">
                      
                        <form>
                          <label>Click on this text and then use the TAB key to naviagte inside the snippet.</label>
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                          <input type="text" placeholder="placeholder text" />
                        </form>
                      
                        <form>
                          First name:<br>
                          <input type="text" name="firstname"><br> Last name:<br>
                          <input type="text" name="lastname">
                        </form>
                      
                      
                        <form>
                          <input type="radio" name="gender" value="male" checked> Male<br>
                          <input type="radio" name="gender" value="female"> Female<br>
                          <input type="radio" name="gender" value="other"> Other
                        </form>
                      
                      
                      
                        <form>
                          <label for="GET-name">Name:</label>
                          <input id="GET-name" type="text" name="name">
                        </form>
                      
                      
                        <form>
                          <label for="POST-name">Name:</label>
                          <input id="POST-name" type="text" name="name">
                        </form>
                      
                      
                        <form>
                          <fieldset>
                            <legend>Title</legend>
                            <input type="radio" name="radio" id="radio">
                            <label for="radio">Click me</label>
                          </fieldset>
                        </form>
                      
                      </div>

                      这可以通过使用基于修改“大纲”的想法的外部库来进一步扩展,而不是像 Materialize 那样完全删除它

                      你最终可以得到一些不难看的东西,而且用很少的努力就可以工作

                      body {
                        background: #444
                      }
                      
                      .wrapper {
                        padding: 2em;
                        width: 400px;
                        max-width: 100%;
                        text-align: center;
                        margin: 2em auto;
                        border: 1px solid #555
                      }
                      
                      button,
                      .wrapper {
                        border-radius: 3px;
                      }
                      
                      button {
                        padding: .25em 1em;
                      }
                      
                      input,
                      label {
                        color: white !important;
                      }
                      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/css/materialize.min.css" />
                      
                      <div class="wrapper">
                        <form>
                          <input type="text" placeholder="Enter Username" name="uname" required>
                          <input type="password" placeholder="Enter Password" name="psw" required>
                          <button type="submit">Login</button>
                        </form>
                      </div>

                      【讨论】:

                        【解决方案14】:

                        你也可以试试这个

                        input[type="text"] {
                        outline-style: none;
                        }
                        

                        .classname input{
                        outline-style: none;
                        }
                        

                        【讨论】:

                          【解决方案15】:

                          当焦点在元素上时移除轮廓,使用下面的 CSS 属性:

                          input:focus {
                              outline: 0;
                          }
                          

                          此 CSS 属性删除所有焦点输入字段的轮廓,或使用伪类使用以下 CSS 属性删除元素的轮廓。

                          .className input:focus {
                              outline: 0;
                          } 
                          

                          此属性删除选定元素的轮廓。

                          【讨论】:

                            【解决方案16】:

                            在你的情况下,尝试:

                            input.middle:focus {
                                outline-width: 0;
                            }
                            

                            或者一般来说,影响所有基本的表单元素:

                            input:focus,
                            select:focus,
                            textarea:focus,
                            button:focus {
                                outline: none;
                            }
                            

                            在 cmets 中,Noah Whitmore 建议进一步支持将contenteditable 属性设置为true 的元素(实际上使它们成为一种输入元素)。以下内容也应针对那些(在支持 CSS3 的浏览器中):

                            [contenteditable="true"]:focus {
                                outline: none;
                            }
                            

                            虽然我不建议这样做,但为了完整起见,您始终可以通过以下方式禁用所有内容上的焦点轮廓:

                            *:focus {
                                outline: none;
                            }
                            

                            请记住,焦点大纲是一种可访问性和可用性功能;它会提示用户当前关注的元素。

                            【讨论】:

                            • 谢谢科里,很棒的提示。您还需要将 CSS 分配给 textarea 以覆盖所有输入字段。 input:focus, textarea:focus {outline: none;}
                            • 别忘了选择select:focus {outline:none;}
                            • 还有 &lt;button&gt; 标签,它被 jQuery UI 和 Twitter Bootstrap 等使用,所以为了完整起见,我将 button: focus 添加到列表中。
                            • 鉴于 HTML 5 属性contenteditable,值得注意的是任何可编辑元素在获得焦点时都会有轮廓(在许多浏览器中),所以div:focus {outline:none}p:focus {outline:none} 或几乎任何元素也可以在这里申请。
                            • @Cᴏʀʏ 你介意把关于 a11y 和可用性的注释移到你问题的最前面吗? IMO 它将大大改善您的答案,因为删除 a11y 功能是一种不好的做法。
                            【解决方案17】:

                            使用此代码:

                            input:focus {
                                outline: 0;
                            }
                            

                            【讨论】:

                              【解决方案18】:

                              这是一个旧线程,但作为参考,重要的是要注意不建议禁用输入元素的轮廓,因为它会妨碍可访问性。

                              outline 属性的存在是有原因的——为用户提供键盘焦点的清晰指示。有关此主题的进一步阅读和其他资源,请参阅http://outlinenone.com/

                              【讨论】:

                              • Boaz,仅供参考 input.middle{outline: none} 仍然允许您遍历元素(包括 input.middle)。按 T​​ab 键也将关注输入标签。唯一的问题是您将看不到它的焦点(轮廓焦点)。所以使用它并没有那么有害.. :)
                              • @AnishNair Only thing is that you won't be able to see the focus(outline focus) on it - 这正是我的观点。移除轮廓会禁用焦点事件的视觉指示,而不是实际事件。删除视觉指示意味着您让依赖该指示的残障人士更难。
                              • 有时我们需要妥协,才能有所成就:)
                              • @AnishNair 是的。但是,阅读此线程的人通常更喜欢简单的出路(即outline:none;)而不考虑其含义。仅仅因为某事简单且节省时间,并不意味着它是最佳实践:)
                              • 我来晚了,但您仍然可以设置输入的焦点状态(例如更改边框颜色或宽度)。只要您在执行此操作时牢记可访问性(良好的对比度等),它就与默认轮廓一样易于访问。
                              【解决方案19】:

                              从所有输入中删除它

                              input {
                               outline:none;
                              }
                              

                              【讨论】:

                                猜你喜欢
                                • 2023-02-08
                                • 2011-08-31
                                • 2018-06-16
                                • 2011-02-25
                                • 2012-08-21
                                • 2014-12-26
                                • 1970-01-01
                                • 1970-01-01
                                相关资源
                                最近更新 更多