search
HomeWeb Front-endCSS TutorialCSS Basics CSS Selectors_Basic Tutorial


Example:

*[lang=fr] { font-size:14px; width:120px; }

*.div { text-decoration:none; }

2. Type selector
Syntax:

E { sRules }

Description:
Type selector. Use the document language object (Element) type as the selector.
Example:

td { font-size:14px; width:120px; }

a { text-decoration:none; }

3. Attribute selector
Syntax:

E [ attr ] { sRules }

E [ attr = value ] { sRules }

E [ attr ~= value ] { sRules }
E [ attr |= value ] { sRules }

Description:
Attribute selector.
Select E with attr attribute
Select E with attr attribute whose value is equal to value
Select E with attr attribute whose value is a space-separated list of words, one of which is equal to value . The value here cannot contain spaces
Select the E that has the attr attribute and the attribute value is a list of words separated by hyphens, starting with value
Example:

h[title] { color: blue; }

/* All h objects with title attribute */



span[class=demo] { color: red; }



div[speed="fast"][dorun="no"] { color: red; }



a[rel~="copyright"] { color :black; }

4. Contains selectors
Syntax:

E1 E2 { sRules }


Description:
Contains selectors. Selects all E2 contained by E1 . That is, E1.contains(E2)==true.
Example:

table td { font-size:14px; }



div.sub a { font-size:14px; }

5. Sub-object selector
Syntax:

E1 > E2 { sRules }

Description:
Sub-object selector. Selects all E2 that are children of E1 .
Example:

body > p { font-size:14px; }

/* The font size of all p objects that are child objects of body is 14px */



div ul>li p { font-size:14px; }

6.ID selector
Syntax:

#ID { sRules }


Description:
ID selector. Use the ID as the unique identifier of the object in the document tree (DOM) as the selector.
Example:

#note { font-size:14px; width:120px;}

7. Class selector
Syntax:

E.className { sRules }

Description:
Class selector.This selector can be used in HTML. Its effect is equivalent to E [ class ~= className ]. See Attribute Selectors.
In IE5, you can specify more than one value ( className ) for the class attribute (property) of an object by specifying a set of style sheet class names separated by spaces. For example:
.
Example:

div.note { font-size:14px; }

/* The font size of all div objects whose class attribute value is equal to (including) "note" is 14px */



.dream { font-size:14px; }

/* The font size of all objects whose class attribute value is equal to (including) "note" is 14px */

8. Selector grouping
Syntax:

E1 , E2 , E3 { sRules }

Description:
Selector grouping. To apply the same definition to multiple selectors, you can combine the selectors into comma-separated groups.
Example:

.td1,div a,body { font-size:14px; }

td,div,a { font-size:14px; }


9. Pseudo-class and pseudo-object selectors

Syntax:

E: Pseudo-Classes { sRules }

E: Pseudo-Elements { sRules }
Description:
Pseudo-class and pseudo-object selectors.
Pseudo-class selector. See Pseudo-Classes[:link :hover :active :visited :focus :first-child :first :left :right :lang].
Pseudo-object selector. See Pseudo-Elements[:first-letter:first-line:before:after].
Example:

div:first-letter { font-size:14px; }

a.fly :hover { font-size:14px; color:red; }
Statement
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
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment