search
HomeWeb Front-endCSS TutorialDetailed introduction to css selectors

Detailed introduction to css selectors

Apr 05, 2020 am 09:10 AM
cssSelector

Detailed introduction to css selectors

What is a selector?

The function of the selector is to find the element through it and pass the css style to the element! CSS selectors are mainly divided into four categories: basic selectors, attribute selectors, combination selectors and pseudo-class selectors!

css basic selector

Basic selector is divided into: * wildcard, label selector, class selector, id selector, you need to pay attention to the programming here The idea is that elements in css cascading style sheets have one and only one id. Pay attention to the following points: 1. ID uniqueness 2. Element IDs are not the same, just like each person has only one ID card, ID represents the ID card 3. The class selector is not unique, it can be reused! In addition, this * wildcard represents the whole world.

  <!DOCTYPE html>
<html>
  <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>css基本选择器</title>
     <style type="text/css">
          *{
              color: skyblue;
         }/**通配符*/
         div{
             color: red;
         }/*标签选择器*/
         .box{
             color: steelblue;
         }/*class选择器*/.box{color: steelblue;}也可以写成*.box{color: steelblue;}代表的所有的box字体颜色为 steelblue
         #content{
             color: tomato;
         }/*id选择器*/
     </style>
 </head>
 <body>
     <div id="content">
         大灰牛博客专注web前端技术学习
     </div>
 </body>
 </html>

(Recommended tutorial: CSS introductory tutorial)

css combination selector

Put the basic selector through special symbols There are opinions that stringing them together is called css combination selectors. Common css combination selectors include: group selectors, nested selectors, sub-selectors, adjacent same-level selectors

 <!DOCTYPE html>
  <html>
  <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>css组合选择器</title>
      <style type="text/css">
      /* div{
          color: teal;
         font-size: 24px;
     }
     p{
         color: teal;
     } */
     div{
         font-size: 24px;
     }
     div,p{
         color: teal;
     }/*分组选择器*/
     div p{
         color: red;
     }/*嵌套选择器*/
     ul>li{
         font-size: 24px;
         list-style: square;
     }/*子选择器*/
     div+p{
         color: blue;
     }/*相邻同级别选择器*/
     </style>
 </head>
 <body>
     <div>
         成功的花,人们只惊羡她现时的明艳!然而当初她的芽儿,浸透了奋斗的泪泉,洒遍了牺牲的血雨
         <p>自以为懂得很多了经历很多了最后才知道都是那么的可笑</p>
     </div>
     <p>我们不要只看成功者荣誉的瞬间,更要看到成功者为之努力、为之奋斗的过程,从而激励自己也不断付出,奔着那个成功的目标前进.</p>
     <p>成功的花,人们只惊羡她现时的明艳!然而当初她的芽儿,浸透了奋斗的泪泉,洒遍了牺牲的血雨<span>我们不要只看成功者荣誉的瞬间,更要看到成功者为之努力、为之奋斗的过程,从而激励自己也不断付出,奔着那个成功的目标前进.</span></p>
     <ul>
         <li>1</li>
         <li>2</li>
         <li>3</li>
     </ul>
 </body>
 </html>

css attribute selector

Basic selector [attribute], basic selector [attribute=value], basic selector [attribute~=value] separated by multiple spaces, basic selector [attribute] What does ^=value] start with and what does the basic selector [attribute $=value] end with?

<!DOCTYPE html>
 <html>
 <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>css属性选择器</title>
      <style type="text/css">
     div[title]/*基本选择器[属性]*/
     div[title=english]{
         color: blue;
     }/*基本选择器[属性=值]*/
     div[title~=en]{
         color:#f90;
         font-weight: bold;
     }/*基本选择器[属性~=值] 任意包含有属性中的一个*/
     p,div[title^=zh]{
         font-size:24px;
         color: brown;
     }/*基本选择器[属性^=值]以什么开始*/
     div[title$=china]{
         letter-spacing: 10px;
         text-decoration: line-through;
         font-style: italic;
     }/*基本选择器[属性$=值]以什么结束*/
     </style>
 </head>
 <body>
     <div title="english">
         If you can NOT explain it simply, you do NOT understand it well enough
     </div>
     <div title="english en yingyu">
         你们没发现嘛?2013爱你一生,2014爱你一世,2015爱你一屋,2016爱你一路,2017爱你一切。

Recommended related video tutorials: css video tutorial

The above is the detailed content of Detailed introduction to css selectors. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.