Table of Contents
示例 3
Home Web Front-end CSS Tutorial What characters are valid in CSS class names/selectors?

What characters are valid in CSS class names/selectors?

Sep 03, 2023 am 11:13 AM

CSS 类名/选择器中哪些字符有效?

在 CSS 中,类名或选择器用于选择特定的 HTML 元素。有一些 CSS 规则来定义类名称或 CSS 选择器。在本教程中,我们将学习所有 CSS 规则以及创建类名称的有效字符。

以下是创建 CSS 类名的规则。

  • 规则 1 - 类名称或 CSS 选择器应仅包含字母数字字符和一些特殊字符,例如连字符 (-) 和下划线 (_)。

  • 规则 2 - 类名称不能以数字开头。例如,“12sd”类名无效。

  • 规则 3 - 类名和 CSS 选择器可以包含特殊字符,例如 '@'、'~'、':' 等,但在使用时需要对其进行转义CSS 中的类名。

  • 规则 4 - 类名称和 CSS 选择器不区分大小写。因此,“.TEST”和“.test”是相同的,但“.TEST”会覆盖“.test”类。

  • 规则 5 - 类名不包含空格。

  • 规则 6 - 类名不应仅包含一个连字符 (-)。此外,类名称不能以连字符开头,后跟数字。例如,“-123”作为类名将不起作用。

上面,我们已经了解了 CSS 中定义类名的规则。现在,让我们尝试通过下面的示例来理解它。

示例 1

在下面的示例中,我们使用字母数字字符创建了 div 元素的类名称。除了“123test”类名之外,所有类名均有效,因为它以数字开头,用户可以在输出中观察到。

此外,“test”类的所有 CSS 都会被“TEST”类的 CSS 覆盖,这意味着如果类名相同,则按排序顺序具有最高优先级的类名会覆盖所有其他类的 CSS类。

<html>
<head>
   <style>
      .test { color: red; font-size: 30px;}
      .TEST { color: green; font-size: 30px;}
      .test123 {color: blue; font-size: 30px;}
      .123test { color: yellow; font-size: 30px;}
      .Test456 { color: orange; font-size: 30px; }
   </style>
</head>
<body>
   <h2>Creating the <i> CSS class names with valid characters. </i></h2>
   <div class = "test"> Class name is test. </div>
   <div class = "TEST"> Class name is TEST. </div>
   <div class = "test123"> Class name is test123. </div>
   <div class = "123test"> Class name is 123test. </div>
   <div class = "Test456"> Class name is Test456. </div>
</body>
</html>
Copy after login

示例 2

在下面的示例中,我们在类名称中使用了下划线和连字符等特殊字符。在此示例中,除包含单个连字符(以连字符开头并后跟数字)的类名称外,所有类名称均有效。

以单个下划线或多个下划线开头的类名是有效的。此外,以连字符开头并后跟字母字符的类名始终有效。

<html>
<head>
   <style>
      ._ { color: red; font-size: 25px;}
      .__ { color: green; font-size: 25px;}
      .- { color: blue; font-size: 25px;}
      .-- { color: yellow; font-size: 25px;}
      .-123 { color: orange; font-size: 25px;}
      .-abcd { color: purple; font-size: 25px;}
      ._123 { color: brown; font-size: 25px;}
      ._abcd { color: pink; font-size: 25px;}
      .demo-class { color: aqua; font-size: 25px;}
      .--demo {color: gray; font-size: 25px;}
   </style>
</head>
<body>
   <h2>Creating the <i> CSS class names with valid characters. </i></h2>
   <div class = "_"> Class name is '_' </div>
   <div class = "__"> Class name is '__' </div>
   <div class = "-"> Class name is '-' </div>
   <div class = "--"> Class name is '--' </div>
   <div class = "-123"> Class name is '-123' </div>
   <div class = "-abcd"> Class name is '-abcd' </div>
   <div class = "_123"> Class name is '_123' </div>
   <div class = "_abcd"> Class name is '_abcd' </div>
   <div class = "demo-class"> Class name is 'demo-class' </div>
   <div class = "--demo"> Class name is '--demo' </div>
</body>
</html>
Copy after login

示例 3

在下面的示例中,我们在类名称中使用了特殊字符,例如“@”、“#”、“$”等。我们可以在 HTML 中的类名中添加特殊字符,但在 CSS 中使用带有特殊字符的类名会出错。因此,我们需要使用‘\’字符来转义 CSS 中的特殊字符。

在这里,我们在类名中使用了各种字符,并在 CSS 中使用反斜杠进行转义。

<html>
<head>
   <style>
      /* escaping special characters in the class name */
      .test\@ { border: 2px solid green; margin: 5px; color: red;}
      .test\~ { border: 2px solid blue; margin: 5px; color: green; }
      .test\:123 { border: 2px solid red; margin: 5px; color: blue;}
      .test\[demo\] { border: 2px solid yellow; margin: 5px; color: black;}
      .test\(90\) { border: 2px solid orange; margin: 5px; color: purple;}
      .test\% { border: 2px solid pink; margin: 5px; color: brown;}
      .test\$ { border: 2px solid black; margin: 5px; color: pink;}
      .test\# { border: 2px solid pink; margin: 5px; color: black;}
   </style>
</head>
<body>
   <h2>Creating the <i> CSS class names with valid characters. </i></h2>
   <div class = "test@"> Class name is test@. </div>
   <div class = "test~"> Class name is test~. </div>
   <div class = "test:123"> Class name is test:123 </div>
   <div class = "test[demo]"> Class name is test[demo]. </div>
   <div class = "test(90)"> Class name is test(90) </div>
   <div class = "test%"> Class name is test%. </div>
   <div class = "test$"> Class name is test$. </div>
   <div class = "test#"> Class name is test#. </div>
</body>
</html>
Copy after login

用户学习了定义类名和 CSS 选择器的规则。此外,我们还学习了在定义类名时使用特殊字符,并使用 CSS 中的反斜杠字符对它们进行转义。

The above is the detailed content of What characters are valid in CSS class names/selectors?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week&#039;s roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

The Deal with the Section Element The Deal with the Section Element Apr 12, 2025 am 11:39 AM

Two articles published the exact same day:

How We Tagged Google Fonts and Created goofonts.com How We Tagged Google Fonts and Created goofonts.com Apr 12, 2025 pm 12:02 PM

GooFonts is a side project signed by a developer-wife and a designer-husband, both of them big fans of typography. We’ve been tagging Google

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I&#039;ve been aware of it for a while, but haven&#039;t taken it for a spin yet. It has some pretty cool and

Multi-Thumb Sliders: General Case Multi-Thumb Sliders: General Case Apr 12, 2025 am 10:52 AM

The first part of this two-part series detailed how we can get a two-thumb slider. Now we&#039;ll look at a general multi-thumb case, but with a different and

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

See all articles