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
This Isn't Supposed to Happen: Troubleshooting the ImpossibleThis Isn't Supposed to Happen: Troubleshooting the ImpossibleMay 15, 2025 am 10:32 AM

What it looks like to troubleshoot one of those impossible issues that turns out to be something totally else you never thought of.

@keyframes vs CSS Transitions: What is the difference?@keyframes vs CSS Transitions: What is the difference?May 14, 2025 am 12:01 AM

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

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 Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment