Table of Contents
Precisely manipulate CSS and JavaScript: Style the first specific class element
I'm h1, just set my style to red
I'm h1
Home Web Front-end HTML Tutorial How to select and style elements of the first specific class using CSS and JavaScript?

How to select and style elements of the first specific class using CSS and JavaScript?

Apr 04, 2025 pm 11:33 PM
css Browser Pseudo class selector red

How to select and style elements of the first specific class using CSS and JavaScript?

Precisely manipulate CSS and JavaScript: Style the first specific class element

In web development, it is often necessary to style the first element of a specific category. For example, the page has multiple elements with class="red" but only the first element is expected to appear in red. This article will demonstrate how to achieve this using CSS and JavaScript.

HTML structure example:

The following HTML code contains multiple class="red" elements:

<div id="test">
  <span>I'm span</span>
  <h1 id="I-m-h-just-set-my-style-to-red">I'm h1, just set my style to red</h1>
  <h1 id="I-m-h">I'm h1</h1>
  <h1 id="I-m-h">I'm h1</h1>
  <h1 id="I-m-h">I'm h1</h1>
</div>
Copy after login

Method 1: JavaScript implementation

JavaScript's document.querySelector() method can easily select the first matching element. The following code sets the color of the first class="red" element to red:

 document.querySelector('.red').style.color = 'red';
Copy after login

Method 2: CSS implementation

The :nth-child() pseudo-class selector of CSS can be selected according to the order of elements. Combined with the of keyword, the first element in a specific class can be selected accurately.

 .red:nth-child(1 of .red) {
  color: red;
}
Copy after login

To style the last class="red" element, you can use the :nth-last-child() pseudo-class:

 .red:nth-last-child(1 of .red) {
  color: blue;
}
Copy after login

Compatibility Description: It should be noted that :nth-child(n of selector) and :nth-last-child(n of selector) may have some problems with browser compatibility. In some older browsers, this method may not work properly. Therefore, which method to choose depends on the project's browser compatibility requirements. If compatibility is critical, JavaScript methods are generally more reliable.

Summary: Both JavaScript and CSS can implement the style setting of the first element of a specific class. Developers should choose the most appropriate method based on actual project requirements and browser compatibility considerations.

The above is the detailed content of How to select and style elements of the first specific class using CSS and JavaScript?. 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 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 use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Which 2025 currency exchanges are more secure? Which 2025 currency exchanges are more secure? Apr 20, 2025 pm 06:09 PM

The top ten safe and reliable exchanges in the 2025 cryptocurrency circle include: 1. Binance, 2. OKX, 3. Gate.io (Sesame Open), 4. Coinbase, 5. Kraken, 6. Huobi Global, 7. Gemini, 8. Crypto.com, 9. Bitfinex, 10. KuCoin. These exchanges are rated as safe and reliable based on compliance, technical strength and user feedback.

In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? Apr 19, 2025 pm 10:57 PM

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

Why is the return value empty when using RedisTemplate for batch query? Why is the return value empty when using RedisTemplate for batch query? Apr 19, 2025 pm 10:15 PM

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

Why can't JavaScript directly obtain hardware information on the user's computer? Why can't JavaScript directly obtain hardware information on the user's computer? Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Apr 19, 2025 pm 10:27 PM

Regarding the reason why RedisTemplate.opsForList().leftPop() does not support passing numbers. When using Redis, many developers will encounter a problem: Why redisTempl...

See all articles