草庐IT

css - 启用 :focus only on keyboard use (or tab press)

coder 2023-07-05 原文

我想禁用 :focus当不需要它时,因为我不喜欢我的导航在焦点上的样子。它使用与 .active 相同的样式这很令人困惑。但是,我不想为使用键盘的人摆脱它。

我正在考虑添加一个类 enabled-focus在标签上的 body 上按下然后有 body.enabled-focus a:focus{...}但这会为每个具有焦点的元素添加大量额外的 CSS。然后在第一次按下鼠标时从主体中删除该类。

我该怎么办?有更好的解决方案吗?

最佳答案

更新:此问题可能不再相关
Some other posters提到了:focus-visible伪类 - 现在有不错的 browser support ...
我想补充一点,基于 spec which covers the :focus-visible pseudo class ,浏览器现在应该只有 指示焦点当它对用户有帮助时 - 例如在用户通过键盘或其他非指针设备与页面交互的情况下
这基本上意味着原始问题不再相关,因为现在,当用户单击/点击按钮(或其他可聚焦元素)时,用户代理将不再显示聚焦环 - 即使按钮已聚焦 - 因为在这种情况下,对焦环对用户没有帮助。
来自 the spec :

While the :focus pseudo-class always matches the currently-focused element, UAs only sometimes visibly indicate focus (such as by drawing a “focus ring”), instead using a variety of heuristics to visibly indicate the focus only when it would be most helpful to the user. The :focus-visible pseudo-class matches a focused element in these situations only...


事实上,从版本 90 开始,Chromium 的用户代理样式表从 :focus 切换而来。至 :focus-visible ,并且由于此更改,按钮单击和点击不再调用聚焦环
此外,截至 version 87 , Firefox 还在其用户代理样式中使用 :focus-visible 。
话虽如此,如果需要自定义焦点样式,因为焦点样式现在已经从 :focus 转移了。至 :focus-visible , 当使用自定义焦点样式覆盖默认样式时 - :focus-visible应该使用伪类。
像这样的东西:
button:focus-visible {
  /* remove default focus style */
  outline: none;
  /* custom focus styles */
  box-shadow: 0 0 2px 2px #51a7e8;
  color: lime;
}
向后兼容性:
像这样使用 :focus-visible 的可能问题是不支持 :focus-visible 的浏览器, 将显示默认对焦环,它可能不清晰或不可见 - 取决于设计。
Šime Vidas,在 this article , 描述了当前使用 :focus-visible 伪类的可行策略 - 即使在尚不支持 :focus-visible 的浏览器中也可以使用 -

A good way to start using :focus-visible today is to define the focus styles in a :focus rule and then immediately undo these same styles in a :focus:not(:focus-visible) rule. This is admittedly not the most elegant and intuitive pattern, but it works well in all browsers:

Browsers that don’t support :focus-visible use the focus styles defined in the :focus rule and ignore the second style rule completely (because :focus-visible is unknown to them).

In browsers that do support :focus-visible, the second style rule reverts the focus styles defined in the :focus rule if the :focus-visible state isn’t active as well. In other words, the focus styles defined in the :focus rule are only in effect when :focus-visible is also active.

button:focus {
  outline: none;
  background: #ffdd00; /* gold */
}

button:focus:not(:focus-visible) {
  background: white; /* undo gold */
}
原答案:
This excellent article来自 Roman Komarov提出了实现 的可行解决方案仅限键盘的焦点样式 用于按钮、链接和其他容器元素,例如跨度或 div(使用 tabindex 属性人为地使其具有焦点)
解决方案:

button {
  -moz-appearance: none;
  -webkit-appearance: none;
  background: none;
  border: none;
  outline: none;
  font-size: inherit;
}

.btn {
  all: initial;
  margin: 1em;
  display: inline-block; 
}

.btn__content {
  background: orange;
  padding: 1em;
  cursor: pointer;
  display: inline-block;
}


/* Fixing the Safari bug for `<button>`s overflow */
.btn__content {
    position: relative;
}

/* All the states on the inner element */
.btn:hover > .btn__content  {
    background: salmon;
}

.btn:active > .btn__content  {
    background: darkorange;
}

.btn:focus > .btn__content  {
    box-shadow: 0 0 2px 2px #51a7e8;
    color: lime;
}

/* Removing default outline only after we've added our custom one */
.btn:focus,
.btn__content:focus {
    outline: none;
}
<h2>Keyboard-only focus styles</h2>

<button id="btn" class="btn" type="button">
    <span class="btn__content" tabindex="-1">
        I'm a button!
    </span>
</button>

<a class="btn" href="#x">
    <span class="btn__content" tabindex="-1">
        I'm a link!
    </span>
</a>

<span class="btn" tabindex="0">
    <span class="btn__content" tabindex="-1">
        I'm a span!
    </span>
</span>

<p>Try clicking any of the the 3 focusable elements above - no focus styles will show</p>
<p>Now try tabbing - behold - focus styles</p>

Codepen
  • 使用 tabindex="-1" 将原始交互元素的内容包装在一个额外的内部元素中(见下文说明)

  • 所以,而不是说:
    <button id="btn" class="btn" type="button">I'm a button!</button>
    
    这样做:
    <button id="btn" class="btn" type="button">
        <span class="btn__content" tabindex="-1">
            I'm a button!
        </span>
    </button>
    
  • 将 css 样式移动到内部元素(布局 css 应保留在原始外部元素上)-因此外部元素的宽度/高度来自内部元素等。
  • 从外部和内部元素中删除默认焦点样式:
    .btn:焦点,
    .btn__content:focus {
    大纲:无;
    }
  • 将焦点样式添加回内部元素 仅当 外部元素具有焦点:
    .btn:focus > .btn__content {
    框阴影:0 0 2px 2px #51a7e8;/* 仅键盘焦点样式/
    颜色:石灰;/仅键盘焦点样式 */
    }

  • 为什么这样做?
    这里的技巧是使用 tabindex="-1" 设置内部元素- 见 MDN :

    A negative value (usually tabindex="-1" means that the element should be focusable, but should not be reachable via sequential keyboard navigation...


    所以元素是 可调焦通过鼠标点击或以编程方式,但另一方面 - 它无法通过键盘“标签”访问。
    因此,当点击交互元素时 - 内部元件 获得焦点。不会显示焦点样式,因为我们已删除它们。
    .btn:focus,
    .btn__content:focus {
        outline: none;
    }
    
    请注意 在给定时间只能聚焦 1 个 DOM 元素 (和 document.activeElement 返回这个元素) - 所以 只有内部元素将被聚焦。
    另一方面:当我们使用键盘 Tab 键时 - 只有外部元素会获得焦点 (请记住:内部元素具有 tabindex="-1"并且无法通过顺序键盘导航访问)[请注意,对于固有不可聚焦的外部元素,例如可点击的 <div> - 我们必须通过添加 tabindex="0" 来人为地使它们可聚焦]
    现在我们的 CSS 开始工作并将仅键盘焦点样式添加到 the inner element .
    .btn:focus > .btn__content  {
        box-shadow: 0 0 2px 2px #51a7e8; /* keyboard-only focus styles */
        color: lime; /* keyboard-only focus styles */
    } 
    
    当然,我们要确保当我们按 Tab 键并按 enter 时。 - 我们没有破坏我们的交互元素,javascript 将运行。
    这是一个演示,表明情况确实如此,但请注意,对于按钮和链接等固有交互元素...你需要手动编码:)

    //var elem = Array.prototype.slice.call(document.querySelectorAll('.btn'));
    var btns = document.querySelectorAll('.btn');
    var fakeBtns = document.querySelectorAll('.btn[tabindex="0"]');
    
    
    var animate = function() {
      console.log('clicked!');
    }
    
    var kbAnimate = function(e) {
      console.log('clicking fake btn with keyboard tab + enter...');
      var code = e.which;
      // 13 = Return, 32 = Space
      if (code === 13) {
        this.click();
      }  
    }
    
    Array.from(btns).forEach(function(element) {
      element.addEventListener('click', animate);
    });
    
    Array.from(fakeBtns).forEach(function(element) {
      element.addEventListener('keydown', kbAnimate);
    });
    button {
      -moz-appearance: none;
      -webkit-appearance: none;
      background: none;
      border: none;
      outline: none;
      font-size: inherit;
    }
    
    .btn {
      all: initial;
      margin: 1em;
      display: inline-block; 
    }
    
    .btn__content {
      background: orange;
      padding: 1em;
      cursor: pointer;
      display: inline-block;
    }
    
    
    /* Fixing the Safari bug for `<button>`s overflow */
    .btn__content {
        position: relative;
    }
    
    /* All the states on the inner element */
    .btn:hover > .btn__content  {
        background: salmon;
    }
    
    .btn:active > .btn__content  {
        background: darkorange;
    }
    
    .btn:focus > .btn__content  {
        box-shadow: 0 0 2px 2px #51a7e8;
        color: lime;
    }
    
    /* Removing default outline only after we've added our custom one */
    .btn:focus,
    .btn__content:focus {
        outline: none;
    }
    <h2>Keyboard-only focus styles</h2>
    
    <button id="btn" class="btn" type="button">
        <span class="btn__content" tabindex="-1">
            I'm a button!
        </span>
    </button>
    
    <a class="btn" href="#x">
        <span class="btn__content" tabindex="-1">
            I'm a link!
        </span>
    </a>
    
    <span class="btn" tabindex="0">
        <span class="btn__content" tabindex="-1">
            I'm a span!
        </span>
    </span>
    
    <p>Try clicking any of the the 3 focusable elements above - no focus styles will show</p>
    <p>Now try tabbing + enter - behold - our interactive elements work</p>

    Codepen

    注意:
  • 虽然这似乎是一个过于复杂的解决方案,但对于非 javascript 解决方案来说,它实际上非常令人印象深刻。更简单的 css-only '解决方案' 涉及 :hover:active伪类样式根本不起作用。 (当然,除非您假设交互式元素在单击时立即消失,就像模态中的按钮一样)

  • button {
      -moz-appearance: none;
      -webkit-appearance: none;
      background: none;
      border: none;
      font-size: inherit;
    }
    
    .btn {
      margin: 1em;
      display: inline-block; 
      background: orange;
      padding: 1em;
      cursor: pointer;
    }
    
    .btn:hover, .btn:active {
      outline: none;
    }
    <h2>Remove css :focus outline only on :hover and :active states</h2>
    
    <button class="btn" type="button">I'm a button!</button>
    
    <a class="btn" href="#x">I'm a link!</a>
    
    <span class="btn" tabindex="0">I'm a span!</span>
    
    <h3>Problem: Click on an interactive element.As soon as you hover out - you get the focus styling back - because it is still focused (at least regarding the button and focusable span) </h3>

    Codepen
  • 这个解决方案并不完美:windows 上的 firefox 仍然会在点击时获得按钮的焦点样式 - 但这似乎是一个 firefox 错误(参见 the article)
  • 当浏览器实现 :fo­cus-ring伪类 - 这个问题可能有一个更简单的解决方案 - (见 the article )
    对于它的值(value),有 a polyfill:focus-ring - 见 this article by Chris DeMars

  • 仅键盘焦点样式的实用替代方案
    因此,实现仅键盘焦点样式非常困难。一种替代/解决方法是 简单得多并且既可以满足设计师的期望,也可以访问 - 就像你为悬停设计风格一样。
    Codepen
    因此,尽管从技术上讲这并没有实现纯键盘样式,但它基本上消除了对纯键盘样式的需求。

    关于css - 启用 :focus only on keyboard use (or tab press),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31402576/

    有关css - 启用 :focus only on keyboard use (or tab press)的更多相关文章

    1. ruby - capybara field.has_css?匹配器 - 2

      我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No

    2. ruby-on-rails - 启用 Rack::Deflater 时 ETag 发生变化 - 2

      在启用Rack::Deflater来gzip我的响应主体时偶然发现了一些奇怪的东西。也许我遗漏了一些东西,但启用此功能后,响应被压缩,但是资源的ETag在每个请求上都会发生变化。这会强制应用程序每次都响应,而不是发送304。这在没有启用Rack::Deflater的情况下有效,我已经验证页面源没有改变。我正在运行一个使用thin作为Web服务器的Rails应用程序。Gemfile.lockhttps://gist.github.com/2510816有没有什么方法可以让我从Rack中间件获得更多的输出,这样我就可以看到发生了什么?提前致谢。 最佳答案

    3. css - 用 watir 检查标签类? - 2

      我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes

    4. ruby-on-rails - Assets 管道损坏 : Not compiling on the fly css and js files - 2

      我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1

    5. ruby-on-rails - Rails - 理解 application.js 和 application.css - 2

      rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:

    6. css - Rails 4.1 和 Bootstrap 3 字形图标不工作 - 2

      我正在尝试消除使用Bootstrap3的Rails4元素中的glyphicon错误。我没有使用任何Bootstrapgem将其添加到Assets管道中。我手动将bootstrap.css和bootstrap.js添加到各自的app/assets目录下,分别添加到application.css和application.js什么的我现在在网络浏览器的控制台中看到以下内容:GEThttp://localhost:3000/fonts/glyphicons-halflings-regular.woff404(NotFound)localhost/:1GEThttp://localhost:30

    7. css - rails 萨斯 : variables are not passed with @import - 2

      我有一个使用twitterbootstrap和sass的Rails元素。scss文件结构化到文件夹中,所以我有更好的概述。现在我想为包含我的颜色等的全局变量定义一个文件,并将这些值传递给其他文件,这样我就有更少的冗余代码。虽然所有代码都已正确导入和应用,变量不起作用。这是当前的设置:样式表/application.css.scss/**=require_self*=require_tree*//*stylesheets/||–base/||–_reset.scss#Reset/normalize||–_typography.scss#Typographyrules||–componen

    8. css - Jekyll 和自定义 CSS - 2

      有没有一种方法可以在jekyll站点中包含自定义css标签,同时将markdown用于入口文件?例如,当我想突出显示某个段落时? 最佳答案 Markdown和YAMLFrontMatter都内置了这个。但你可以自己制作。比如说,您有foo.css想要包含在某些帖子中。在_posts/2013-02-03-higligting-foo.markdown中:---css:footitle:"DrupalImagecachesecurityvulnarabilitywithDDOSattackexplained"tags:[drupal,

    9. css - 检测到 Sass 更改但 style.css 仅在我保存时每 5 到 7 次被覆盖 - 2

      我在一台Windows764位机器上使用Sass和Ruby(最新版本),我正在我的家庭服务器上处理一个共享文件夹。(但是,我不得不承认问题本身也出现在服务器上,因为我试图安装Ruby并直接-watch服务器上的文件)。问题如下:如果我第一次保存,检测到变化,我的style.css被直接覆盖。之后,我总是需要保存多达7次才能覆盖style.css。每次都会检测到更改,但不会编译任何内容。这是一个屏幕:>>>Sassiswatchingforchanges.PressCtrl-Ctostop.overwritestyle.css>>>Changedetectedto:E:/Websites

    10. ruby - 用于 CSS3 跨浏览器兼容性的 SASS 插件? - 2

      是否有一个SASS扩展可以采用SASS样式表,找到中性属性(例如border-radius)并为其输出所有特定于供应商的属性(例如-webkit-border-radius等)自动?我真的不想手动创建所有混入,也不想手动编写代码。我确定一定有这样的扩展名,但我找不到它。帮忙? 最佳答案 有一个非常好的gem可以满足您的需求。它叫做Bourbon它不会用特定于供应商的css替换您的css,因为它可以像SASS一样工作。它基本上是一个正确生成跨浏览器css的mixin集合。 关于ruby-用

    随机推荐