


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!

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
