F {style code}"; 3. Adjacent sibling selectors , syntax "E + F {style code}"."/> F {style code}"; 3. Adjacent sibling selectors , syntax "E + F {style code}".">
search
HomeWeb Front-endFront-end Q&AThere are several derived selectors in css

css中的派生选择器有3种:1、后代选择器,语法“E F{样式代码}”;2、子元素选择器,语法“E > F {样式代码}”;3、相邻兄弟选择器,语法“E + F {样式代码}”。

There are several derived selectors in css

本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。

CSS之派生选择器(上下文选择器)

选择器(selector)是CSS里面非常重要地一部分知识。

根据我暂时浅薄地理解,CSS就是对一份HTML代码里面的元素进行运用各种样式。

所以,第一步就是要准确地定位你想要运用样式的元素。

CSS里面通过各种选择器来定位元素。 CSS里面选择器有很多种,今天就只想说说 “派生选择器”。

一听“派生选择器”这个名字,我真心做不到见名知义地感觉它是什么选择器。一看英文名叫“contextual selectors” ,如果直译就叫“上下文选择器”。虽然名字不文雅,但是我更偏向这个名字。

contexual selector是通过依据元素在其位置的上下文关系来定义样式的。

上下文选择器的情况有三种:后代选择器,子元素选择器,相邻兄弟选择器。 

后代选择器(descendant selector)

eg:

HTML代码:

    <h1 id="This-is-a-em-important-em-heading">This is a<em>important</em>heading</h1>

CSS代码: 

    h1 em {color:red;}

语法规则是 h1 和 em 之间有一个空格。那么这一条CSS代码就会运用于包含在

标签内的所有元素。
关于后代选择器,很重要的一点是第一个参数和第二个参数之间的代数是可以无限的。

eg:

HTML代码:


          
  •         
                  
    •                 This will be styled.              
    •         
        
  •     
  •         This will be styled too.     

CSS代码:

ul em{color:red;}

以上css的样式会运用于HTML代码中两处橘色的元素。

子选择器(child selector) 

 子选择器只会选择某个元素的子元素,而不会扩大到任意的后代元素。

eg:

HTML代码:

<h1 id="This-is-strong-This-will-be-styled-strong-important">This is <strong>This will be styled.</strong> important.</h1>
<h1 id="This-is-em-really-strong-This-will-not-be-styled-strong-em-important">This is <em>really <strong>This will not be styled.</strong></em> important.</h1>

CSS代码:

h1 > strong {color:red;}

语法规则是h1和strong之间会有一个“>”符号。而这个“>”和前面的h1或者是后面的strong之间的空格都是可有可无的。

在以上的例子中,第一行HTML代码里,元素是

的子元素,所有h1 > strong这个选择器会选择到它。

第二行HTML代码里, 不是

的子元素,而是

的孙子,所有h1 > strong这个选择器不会选择到它。

相邻兄弟选择器(Adjacent sibling selector)

相邻兄弟选择器会选择某一元素紧随其后的元素,但是前提是他们拥有相同的父级。 

eg:

HTML代码:

<h1>
    <h2>This is a heading<h2>
    <strong>This will be styled.</strong>
    <strong>This will not be styled.</strong>
<h1></h1>
</h2>
</h2>
</h1>

CSS代码:

h2 + strong {color:red;}

语法规则是h2和strong之间有一个“+”,“+”和前面的h1或者后面的strong之间的空格都是可有可无的。

在 以上例子中,第一个strong紧邻着h2并且他们拥有相同的父级(h1),所以h2 + strong会选择到第一个而不会选到第二个.

eg2:

HTML代码:

<p> 
    </p>
                  
  • List item 1
  •           
  • List item 2
  •           
  • List item 3
  •       
       
               
  1. List item 1
  2.            
  3. List item 2
  4.            
  5. List item 3
  6.       
 

CSS代码:

li + li {color:red;}

在以上的例子中li+li是选择紧挨着li后面的第一个

  • ,所以第一个
  • 不会被选择;而第二个
  • 是紧挨着第一个li的,所有会被选择;第三个
  • 是紧挨着第二个
  • 的,也会被选择。

    (学习视频分享:css视频教程

  • The above is the detailed content of There are several derived selectors in css. For more information, please follow other related articles on the PHP Chinese website!

    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
    CSS: Can I use multiple IDs in the same DOM?CSS: Can I use multiple IDs in the same DOM?May 14, 2025 am 12:20 AM

    No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

    The Aims of HTML5: Creating a More Powerful and Accessible WebThe Aims of HTML5: Creating a More Powerful and Accessible WebMay 14, 2025 am 12:18 AM

    HTML5aimstoenhancewebcapabilities,makingitmoredynamic,interactive,andaccessible.1)Itsupportsmultimediaelementslikeand,eliminatingtheneedforplugins.2)Semanticelementsimproveaccessibilityandcodereadability.3)Featureslikeenablepowerful,responsivewebappl

    Significant Goals of HTML5: Enhancing Web Development and User ExperienceSignificant Goals of HTML5: Enhancing Web Development and User ExperienceMay 14, 2025 am 12:18 AM

    HTML5aimstoenhancewebdevelopmentanduserexperiencethroughsemanticstructure,multimediaintegration,andperformanceimprovements.1)Semanticelementslike,,,andimprovereadabilityandaccessibility.2)andtagsallowseamlessmultimediaembeddingwithoutplugins.3)Featur

    HTML5: Is it secure?HTML5: Is it secure?May 14, 2025 am 12:15 AM

    HTML5isnotinherentlyinsecure,butitsfeaturescanleadtosecurityrisksifmisusedorimproperlyimplemented.1)Usethesandboxattributeiniframestocontrolembeddedcontentandpreventvulnerabilitieslikeclickjacking.2)AvoidstoringsensitivedatainWebStorageduetoitsaccess

    HTML5 goals in comparison with older HTML versionsHTML5 goals in comparison with older HTML versionsMay 14, 2025 am 12:14 AM

    HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

    CSS: Is it bad to use ID selector?CSS: Is it bad to use ID selector?May 13, 2025 am 12:14 AM

    Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

    HTML5: Goals in 2024HTML5: Goals in 2024May 13, 2025 am 12:13 AM

    HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

    What are the main areas where HTML5 tried to improve?What are the main areas where HTML5 tried to improve?May 13, 2025 am 12:12 AM

    HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

    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

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    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.

    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.

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use