


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>
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';
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; }
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; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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...

JDBC...

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.

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? When using RedisTemplate for batch query operations, you may encounter the returned results...

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, 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.

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