css伪类选择器_如何使用CSS:root伪类选择器

news/2024/7/5 3:13:02

css伪类选择器

Learn about the CSS :root pseudo-class selector, and how you might want to use it in your projects!

了解CSS :root伪类选择器,以及如何在项目中使用它!

The CSS :root pseudo-class selector is used to select the highest-level parent of a given specification. In the HTML specification, the :root is essentially equivalent to the html selector.

CSS :root伪类选择器用于选择给定规范的最高级别的父级。 在HTML规范中, :root本质上等效于html选择器。

In the CSS snippet below the :root and html styles will do the same thing:

:roothtml样式下面CSS代码段中,将执行相同的操作:

:root {
  background-color: gray;
}

html {
  background-color: gray;
}

If you noticed I said :root is essentially equivalent to the html selector. In fact, the :root selector has more authority than html. This is because it’s actually considered a pseudo-class selector (like :first-child or :hover).

如果您注意到我说的话, :root本质上等效于html选择器 。 实际上, :root选择器比html具有更多权限。 这是因为它实际上被视为伪类选择器(例如:first-child:hover )。

As a pseudo-class selector, it has more authority/higher specificity than tag selectors:

作为伪类选择器,它比标记选择器具有更多的权限/ 更高的特异性 :

:root {
  background-color: blue;
  color: white;
}

html {
  background-color: red;
  color: white;
}

Despite the html selector coming after, the :root selector still wins, thanks to its higher specificity!

尽管html其后的是html选择器,但:root选择器仍然胜出,这归功于其更高的特异性!

交叉规格 (Cross-Specification)

In the HTML specification, the :root pseudo-class targets the highest-level parent: html.

在HTML规范中, :root伪类指定了最高级别的父类: html

Since CSS is also designed for SVG and XML you can actually use :root and it will just correspond to a different element.

由于CSS也是为SVG和XML设计的,因此您实际上可以使用:root ,它将仅对应于另一个元素。

For example, in SVG the highest-level parent is the svg tag.

例如,在SVG中,最高级别的父级是svg标签。

:root {
  fill: gold;
}

svg {
  fill: gold;
}

Similar to HTML, the :root and svg tags select the same element, however the :root selector will have higher specificity.

与HTML相似, :rootsvg标记选择相同的元素,但是:root选择器将具有更高的特异性。

实际用途 (Practical Uses)

What are the practical uses for :root? As we covered earlier, it’s a safe substitute for the html selector.

:root的实际用途是什么? 如前所述,它是html选择器的安全替代品。

This means you can do anything you’d normally do with the html selector:

这意味着您可以执行通常使用html选择器执行的任何操作:

:root {
  margin: 0;
  padding: 0;
  color: #0000FF;
  font-family: "Helvetica", "Arial", sans-serif;
  line-height: 1.5;
}

If you’d like, you can refactor this code to use CSS Custom Properties to create variables at the global level!

如果愿意,可以重构此代码以使用CSS自定义属性在全局级别创建变量!

:root {
  margin: 0;
  padding: 0;
  --primary-color: #0000FF;
  --body-fonts: "Helvetica", "Arial", sans-serif;
  --line-height: 1.5;
}

p {
  color: var(--primary-color);
  font-family: var(--body-fonts);
  line-height: var(--line-height);
}

The added benefit of using :root instead of html is that you can style your SVG graphics! 🤯

使用:root代替html的附加好处是您可以设置SVG图形的样式! 🤯

:root {
  margin: 0;
  padding: 0;
  --primary-color: #0000FF;
  --body-fonts: "Helvetica", "Arial", sans-serif;
  --line-height: 1.5;
}

svg {
  font-family: var(--body-fonts);
}

svg circle {
  fill: var(--primary-color);
}

For extensive documentation on the :root pseudo-class selector, visit the Mozilla Developer Network

有关:root伪类选择器的大量文档,请访问Mozilla开发人员网络。

翻译自: https://www.digitalocean.com/community/tutorials/css-root-pseudo-class

css伪类选择器


http://www.niftyadmin.cn/n/3648852.html

相关文章

Git使用教程详解之二 Git基础

Git 基础 读完本章你就能上手使用 Git 了。本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令。读完本章,你就能初始化一个新的代码仓库,做一些适当配置;开始或停止跟踪某些文件…

自定义view——实现评分控件RatingBar的实现

首先看下效果图 星星是两个不同的图片 首先老套路&#xff1a;自定义属性 <?xml version"1.0" encoding"utf-8"?> <resources><declare-styleable name"RatingBar"><attr name"starNormal" format"ref…

[C#][固定格式网页解析]使用正则表达式处理网页的初步体会

用IE WebControl解析网页得到特定网页中的特定数据&#xff1a;Set oDocument Form2.m_IE.Document Set oelement oDocument.Forms("searchdetail") Set oListTableElement oelement.children(0).children(0)这样的好处是简单&#xff0c;但坏处是&…

如何在Ubuntu 18.04上使用Apache设置密码身份验证[快速入门]

介绍 (Introduction) This tutorial will walk you through password-protecting assets on an Apache web server running on Ubuntu 18.04. Completing these steps will provide your server with additional security so that unauthorized users cannot access certain pa…

新闻组搜索技术讨论的利器

新闻组利器http://groups-beta.google.com/和http://www.google.com/advanced_group_search?hlzh-CN。尤其是前者&#xff0c;可以自定义自己喜欢的My Groups&#xff0c;My starred topics?&#xff0c;Recent groups&#xff0c;如果你有Google帐号或者Gmail帐号的话。

加载插件资源的Demo

首先了解一下Google加载资源源码 效果图 ImageView中加载src源码 final Drawable d a.getDrawable(R.styleable.ImageView_src); getDrawable源码 if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) {if (value.type TypedValue.TYPE_ATTRIBUTE) {throw n…

Git使用教程详解之三 Git分支

Git 分支 几乎每一种版本控制系统都以某种形式支持分支。使用分支意味着你可以从开发主线上分离开来&#xff0c;然后在不影响主线的同时继续工作。在很多版本控制系统中&#xff0c;这是个昂贵的过程&#xff0c;常常需要创建一个源代码目录的完整副本&#xff0c;对大型项目来…

DigitalOcean电子书:面向全栈开发人员的Kubernetes

Download the Complete eBook!下载完整的电子书&#xff01; Kubernetes for Full-Stack Developers eBook in EPUB format 适用于EPUB格式的 Full-Stack Developers电子书的Kubernetes Kubernetes for Full-Stack Developers eBook in PDF format Kubernetes适用于PDF格式的 …