css悬停_CSS悬停选择器说明(带有示例)

css悬停

The CSS :hover selector is one of many pseudo-classes that are used to style elements. :hover is used to select elements that users hover their cursor or mouse over. It can be used on all elements, not only on links.

CSS :hover选择器是用于样式元素的许多伪类之一。 :hover用于选择用户将光标悬停在其上的元素。 它不仅可以用于链接,还可以用于所有元素。

When used to style links, :hover is often paired with the :link, :visited, and :active selectors which style unvisited, visited, and active links, respectively.

当用于设置链接样式时, :hover通常与:link:visited:active选择器配对使用,它们分别设置未访问,访问和活动链接的样式。

If :link and :visited rules are in the CSS definition, :hover should fall after them. Otherwise, the styles in the :hover rule won't be applied to the selected element.

如果:link:visited规则在CSS定义中,则:hover应该放在它们之后。 否则, :hover规则中的样式将不会应用于所选元素。

Syntax:

句法:

a:hover {
  /* CSS declarations */
}

The hover selector only applies the styles provided in the rule when an element enters the hover state. This is typically when a user hovers over the element with their mouse.

悬停选择器仅在元素进入悬停状态时才应用规则中提供的样式。 通常,这是当用户将鼠标悬停在元素上时。

button {
  color: white;
  background-color: green;
}

button:hover {
  background-color: white;
  border: 2px solid green;
  color: green;
}

In the example above, the button’s normal styling is white text on a green button. When a user hovers over the button with their mouse, the rule with the :hover selector will become active and the button’s style will change.

在上面的示例中,按钮的常规样式是绿色按钮上的白色文本。 当用户将鼠标悬停在按钮上时,带有:hover选择器的规则将变为活动状态,并且按钮的样式将更改。

Note that :hover can be problematic on touchscreens – different hardware and mobile browser implementations can cause the pseudo-class to be triggered in some cases and not in others. Make sure to thoroughly test elements with :hover in as many different mobile browsers and devices as possible.

请注意:hover在触摸屏上可能会出现问题-不同的硬件和移动浏览器实现可能在某些情况下而不是在其他情况下触发伪类。 确保使用:hover在尽可能多的不同移动浏览器和设备中彻底​​测试元素。

翻译自: https://www.freecodecamp.org/news/hover-selector/

css悬停

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS选择器是用来选择HTML文档中的元素,并对这些元素应用样式。CSS选择器有多种类型,包括: 1. 元素选择器:根据元素的标签名称来选择元素,例如`p`选择器可以选择所有的`<p>`元素。 2. 类选择器:根据元素的class属性的值来选择元素,例如`.red`选择器可以选择所有class属性值为`red`的元素。 3. ID选择器:根据元素的id属性的值来选择元素,例如`#header`选择器可以选择id属性值为`header`的元素。 4. 属性选择器:根据元素的属性值来选择元素,例如`[type="text"]`选择器可以选择所有`type`属性值为`text`的元素。 5. 伪类选择器:根据元素在特定状态下的情况来选择元素,例如`:hover`选择器可以选择鼠标悬停在该元素上时的元素。 6. 伪元素选择器:用于向某些选择器添加一些特殊效果,例如`::before`选择器可以在元素的前面插入一个内容。 以下是一些CSS选择器示例: ``` /* 元素选择器 */ p { color: blue; } /* 类选择器 */ .red { color: red; } /* ID选择器 */ #header { font-size: 24px; } /* 属性选择器 */ [type="text"] { border: 1px solid gray; } /* 伪类选择器 */ a:hover { text-decoration: underline; } /* 伪元素选择器 */ p::before { content: ">> "; } ``` 以上代码中,`p`选择器选择所有的`<p>`元素,并将它们的文本颜色设置为蓝色。`.red`选择器选择所有class属性值为`red`的元素,并将它们的文本颜色设置为红色。`#header`选择器选择id属性值为`header`的元素,并将它们的字体大小设置为24px。`[type="text"]`选择器选择所有`type`属性值为`text`的元素,并将它们的边框样式设置为1像素的灰色实线。`a:hover`选择器选择鼠标悬停在链接上的元素,并将它们的文本下划线设置为实线。`p::before`选择器向所有`<p>`元素的前面插入一个包含`>> `文本内容的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值