:focus 和 :active 有什么区别?

发布于 2024-08-09 23:17:31 字数 58 浏览 7 评论 0原文

:focus:active 伪类有什么区别?

What is the difference between the :focus and :active pseudo-classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

七度光 2024-08-16 23:17:31

:focus:active 是两种不同的状态。

  • :focus 表示当前选择元素接收输入时的状态,
  • :active 表示当前元素被用户激活时的状态。

例如,假设我们有一个

需要注意的是,当您单击某个元素时,您会为其提供焦点,这也会产生 :focus:active 相同的错觉。 它们不一样。单击时按钮处于 :focus:active 状态。

一个例子:

<style type="text/css">
  button { font-weight: normal; color: black; }
  button:focus { color: red; }
  button:active { font-weight: bold; }
</style>
  
<button>
  When clicked, my text turns red AND bold!<br />
  But not when focused only,<br />
 where my text just turns red
</button>

编辑: jsfiddle

:focus and :active are two different states.

  • :focus represents the state when the element is currently selected to receive input and
  • :active represents the state when the element is currently being activated by the user.

For example let's say we have a <button>. The <button> will not have any state to begin with. It just exists. If we use Tab to give "focus" to the <button>, it now enters its :focus state. If you then click (or press space), you then make the button enter its (:active) state.

On that note, when you click on an element, you give it focus, which also cultivates the illusion that :focus and :active are the same. They are not the same. When clicked the button is in :focus:active state.

An example:

<style type="text/css">
  button { font-weight: normal; color: black; }
  button:focus { color: red; }
  button:active { font-weight: bold; }
</style>
  
<button>
  When clicked, my text turns red AND bold!<br />
  But not when focused only,<br />
 where my text just turns red
</button>

edit: jsfiddle

天冷不及心凉 2024-08-16 23:17:31
:active       Adds a style to an element that is activated
:focus        Adds a style to an element that has keyboard input focus
:hover        Adds a style to an element when you mouse over it
:lang         Adds a style to an element with a specific lang attribute
:link         Adds a style to an unvisited link
:visited      Adds a style to a visited link

来源:CSS 伪类

:active       Adds a style to an element that is activated
:focus        Adds a style to an element that has keyboard input focus
:hover        Adds a style to an element when you mouse over it
:lang         Adds a style to an element with a specific lang attribute
:link         Adds a style to an unvisited link
:visited      Adds a style to a visited link

Source: CSS Pseudo-classes

清音悠歌 2024-08-16 23:17:31

有四种情况。

  1. 默认情况下,活动和焦点均处于关闭状态。
  2. 当您按 Tab 循环浏览可聚焦元素时,它们将输入 :focus(不处于活动状态)。
  3. 当您点击一个不可聚焦的元素时,它会进入:active(没有焦点)。
  4. 当您单击可聚焦元素时,它会输入:active:focus(同时活动和聚焦)。

示例:

<div>
  I cannot be focused.
</div>

<div tabindex="0">
  I am focusable.
</div>

div:focus {
  background: green;
}

div:active {
  color: orange;
}

div:focus:active {
  font-weight: bold;
}

当页面加载时,两者都属于情况 1。当您按 Tab 键时,您将聚焦第二个 div 并看到它展示情况 2。当您单击第一个 div 时,您会看到情况 3。当您单击第二个 div,您会看到情况 4。


元素是否可聚焦是另一个问题。大多数都不是默认的。但是,可以安全地假设

There are four cases.

  1. By default, active and focus are both off.
  2. When you tab to cycle through focusable elements, they will enter :focus (without active).
  3. When you click on a non-focusable element, it enters :active (without focus).
  4. When you click on a focusable element it enters :active:focus (active and focus simultaneously).

Example:

<div>
  I cannot be focused.
</div>

<div tabindex="0">
  I am focusable.
</div>

div:focus {
  background: green;
}

div:active {
  color: orange;
}

div:focus:active {
  font-weight: bold;
}

When the page loads both are in case 1. When you press tab you will focus the second div and see it exhibit case 2. When you click on the first div you see case 3. When you click the second div, you see case 4.


Whether an element is focusable or not is another question. Most are not by default. But, it's safe to assume <a>, <input>, <textarea> are focusable by default.

小鸟爱天空丶 2024-08-16 23:17:31
  • :focus 是指元素能够接受输入 - 输入框中的光标或已使用 Tab 键访问的链接。

  • :active 是指用户激活元素的时间 - 用户按下鼠标按钮然后释放鼠标按钮之间的时间。

  • :focus is when an element is able to accept input - the cursor in a input box or a link that has been tabbed to.

  • :active is when an element is being activated by a user - the time between when a user presses a mouse button and then releases it.

烟雨凡馨 2024-08-16 23:17:31

活动是当用户激活该点时(例如鼠标单击,如果我们在字段到字段之间使用选项卡,则没有活动样式的迹象。也许单击需要更多时间,只需尝试按住单击该点),焦点发生在之后该点被激活。试试这个:

<style type="text/css">
  input { font-weight: normal; color: black; }
  input:focus { color: green; outline: 1px solid green; }
  input:active { color: red; outline: 1px solid red; }
</style>

<input type="text"/>
<input type="text"/>

Active is when the user activating that point (Like mouse clicking, if we use tab from field-to-field there is no sign from active style. Maybe clicking need more time, just try hold click on that point), focus is happened after the point is activated. Try this :

<style type="text/css">
  input { font-weight: normal; color: black; }
  input:focus { color: green; outline: 1px solid green; }
  input:active { color: red; outline: 1px solid red; }
</style>

<input type="text"/>
<input type="text"/>
蔚蓝源自深海 2024-08-16 23:17:31

焦点只能通过键盘输入给出,但元素可以通过鼠标或键盘激活。

如果在链接上使用 :focus,则样式规则仅适用于按下键盘上的按钮。

Focus can only be given by keyboard input, but an Element can be activated by both, a mouse or a keyboard.

If one would use :focus on a link, the style rule would only apply with pressing a botton on the keyboard.

昨迟人 2024-08-16 23:17:31

使用“焦点”将为键盘用户提供与鼠标用户悬停鼠标时相同的效果。在 Internet Explorer 中需要“Active”才能获得相同的效果。

现实情况是,这些状态并不适用于所有用户。不使用所有三个选择器会给许多无法使用鼠标的仅使用键盘的用户带来可访问性问题。我邀请您参加 #nomouse 挑战 (nomouse dot org)。

Using "focus" will give keyboard users the same effect that mouse users get when they hover with a mouse. "Active" is needed to get the same effect in Internet Explorer.

The reality is, these states do not work as they should for all users. Not using all three selectors creates accessibility issues for many keyboard-only users who are physically unable to use a mouse. I invite you to take the #nomouse challenge (nomouse dot org).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文