Table of Contents

This heading will be center-aligned
Home Web Front-end CSS Tutorial Detailed explanation of how to use css class selector_CSS/HTML

Detailed explanation of how to use css class selector_CSS/HTML

May 16, 2016 pm 12:03 PM
css

Class selector
In CSS, the class selector is displayed as a period:

Copy code The code is as follows:

.center {text-align: center}

In the above example, all HTML elements with the center class are centered.
In the HTML code below, both the h1 and p elements have the center class. This means that both will obey the rules in the ".center" selector.
Copy code The code is as follows:


This heading will be center-aligned




This paragraph will also be center-aligned.



Note: Numbers cannot be used as the first character of the class name! It won't work in Mozilla or Firefox.
Like id, class can also be used as a derived selector:
Copy code The code is as follows:

  .fancy td {
       color: #f60;
      background: #666; Table cells inside larger elements will have orange text on a gray background. (A larger element named fancy might be a table or a div.)
Elements can also be selected based on their class:
Copy code The code is as follows:

td.fancy {
color: #f60;
background: #666;
}

In the above example, the table cell with class name fancy will be orange with a gray background.
Copy code The code is as follows:


Multi-category selector

1. In HTML, a class value may contain a list of words, with each word separated by spaces. For example, if you want to mark a specific element as both important and warning, you can write (the order of the two words does not matter, you can also write warning important):

Copy code The code is as follows:


This paragraph is a very important warning.



We assume that all elements with class important are bold, and all elements with class warning are italic, and all elements with class containing both important and warning are also There is a silver background. You can write:
Copy code The code is as follows:

.important {font-weight: bold;}
.warning {font-weight:italic;}
.important.warning {background:silver;}

2. By linking two class selectors Together, only elements containing both of these class names can be selected (the order of the class names is not limited).
If a multi-class selector contains a class name that is not in the class name list, the match will fail. Please see the following rules:
Copy code The code is as follows:

.important.urgent { background:silver;}

As expected, this selector will only match p elements whose class attribute contains the words important and urgent. Therefore, if a p element has only the words important and warning in its class attribute, it will not match. However, it does match the following elements:
Copy code The code is as follows:


This paragraph is a very important and urgent warning.


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 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)

How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? Apr 05, 2025 pm 10:33 PM

Using locally installed font files in web pages Recently, I downloaded a free font from the internet and successfully installed it into my system. Now...

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

How to compatible with multi-line overflow omission on mobile terminal? How to compatible with multi-line overflow omission on mobile terminal? Apr 05, 2025 pm 10:36 PM

Compatibility issues of multi-row overflow on mobile terminal omitted on different devices When developing mobile applications using Vue 2.0, you often encounter the need to overflow text...

How to efficiently modify the style of nested DIV elements? How to efficiently modify the style of nested DIV elements? Apr 05, 2025 pm 10:45 PM

In-depth discussion on nested DIV style modification methods This article will explain in detail how to effectively modify the DIV element style of nested structures. The challenge we face is how...

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

What are the advantages of H5 page production What are the advantages of H5 page production Apr 05, 2025 pm 11:48 PM

The advantages of H5 page production include: lightweight experience, fast loading speed, and improving user retention. Cross-platform compatibility, no need to adapt to different platforms, improving development efficiency. Flexibility and dynamic updates, no audit required, making it easier to modify and update content. Cost-effective, lower development costs than native apps.

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

See all articles