首页 > 其他分享 >css04 CSS Combinators

css04 CSS Combinators

时间:2024-05-28 20:57:34浏览次数:31  
标签:elements css04 Combinators element Paragraph div selector selects CSS

https://www.w3schools.com/css/css_combinators.asp

CSS Combinators

A combinator is something that explains the relationship between the selectors.

A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator.

There are four different combinators in CSS:

  • descendant selector (space)
  • child selector (>)
  • adjacent sibling selector (+)
  • general sibling selector (~)

Descendant Selector

The descendant selector matches all elements that are descendants of a specified element.

The following example selects all <p> elements inside <div> elements: 

Example

div p {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
div p {
  background-color: yellow;
}
</style>
</head>
<body>

<h2>Descendant Selector</h2>

<p>The descendant selector matches all elements that are descendants of a specified element.</p>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
  <section><p>Paragraph 3 in the div.</p></section>
</div>

<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>

</body>
</html>

 


Child Selector (>)

The child selector selects all elements that are the children of a specified element.

The following example selects all <p> elements that are children of a <div> element:

Example

div > p {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
div > p {
  background-color: yellow;
}
</style>
</head>
<body>

<h2>Child Selector</h2>

<p>The child selector (>) selects all elements that are the children of a specified element.</p>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
  <section>
    <!-- not Child but Descendant -->
    <p>Paragraph 3 in the div (inside a section element).</p>
  </section>
  <p>Paragraph 4 in the div.</p>
</div>

<p>Paragraph 5. Not in a div.</p>
<p>Paragraph 6. Not in a div.</p>

</body>
</html>

 


   

Adjacent Sibling Selector (+)

The adjacent sibling selector is used to select an element that is directly after another specific element.

Sibling elements must have the same parent element, and "adjacent" means "immediately following".

The following example selects the first <p> element that are placed immediately after <div> elements:

Example

div + p {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
div + p {
  background-color: yellow;
}
</style>
</head>
<body>

<h2>Adjacent Sibling Selector</h2>

<p>The + selector is used to select an element that is directly after another specific element.</p>
<p>The following example selects the first p element that are placed immediately after div elements:</p>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
</div>

<p>Paragraph 3. After a div.</p>
<p>Paragraph 4. After a div.</p>

<div>
  <p>Paragraph 5 in the div.</p>
  <p>Paragraph 6 in the div.</p>
</div>

<p>Paragraph 7. After a div.</p>
<p>Paragraph 8. After a div.</p>

</body>
</html>

 


General Sibling Selector (~)

The general sibling selector selects all elements that are next siblings of a specified element.

The following example selects all <p> elements that are next siblings of <div> elements: 

Example

div ~ p {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
div ~ p {
  background-color: yellow;
}
</style>
</head>
<body>

<h2>General Sibling Selector</h2>

<p>The general sibling selector (~) selects all elements that are next siblings of a specified element.</p>

<p>Paragraph 1.</p>

<div>
  <p>Paragraph 2.</p>
</div>

<p>Paragraph 3.</p>
<code>Some code.</code>
<p>Paragraph 4.</p>

</body>
</html>

 




All CSS Combinator Selectors

SelectorExampleExample description
element element div p Selects all <p> elements inside <div> elements
element>element div > p Selects all <p> elements where the parent is a <div> element
element+element div + p Selects the first <p> element that are placed immediately after <div> elements
element1~element2 p ~ ul Selects every <ul> element that are preceded by a <p> element

标签:elements,css04,Combinators,element,Paragraph,div,selector,selects,CSS
From: https://www.cnblogs.com/emanlee/p/18218808

相关文章

  • css05 CSS Pseudo-classes
    https://www.w3schools.com/css/css_pseudo_classes.asp WhatarePseudo-classes?Apseudo-classisusedtodefineaspecialstateofanelement.Forexample,itcanbeusedto:StyleanelementwhenausermousesoveritStylevisitedandunvisitedlinks......
  • css03 CSS Selectors
    https://www.w3schools.com/css/css_selectors.aspACSSselectorselectstheHTMLelement(s)youwanttostyle.CSSSelectorsCSSselectorsareusedto"find"(orselect)theHTMLelementsyouwanttostyle.WecandivideCSSselectorsintofive......
  • css01 CSS Introduction
    https://www.w3schools.com/css/css_intro.asp CSSisthelanguageweusetostyleaWebpage.WhatisCSS?CSSstandsforCascadingStyleSheetsCSSdescribeshowHTMLelementsaretobedisplayedonscreen,paper,orinothermediaCSSsavesalotofw......
  • css02 CSS Syntax
    https://www.w3schools.com/css/css_syntax.aspACSSruleconsistsofaselectorandadeclarationblock.CSSSyntaxTheselectorpointstotheHTMLelementyouwanttostyle.Thedeclarationblockcontainsoneormoredeclarationsseparatedbysemicolo......
  • html+CSS部分基础运用8
    1.P147实验1,完成页面制作效果。图7-1木兰花令效果图2.P147实验2,完成页面制作效果。项目1<!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <linktype="text/css">  <title>木兰花令</title>......
  • html+CSS部分基础运用7
    项目1 设计简易灯箱画廊1.实验所需素材在trees文件夹中提供一个MP3文件和18个JPG文件,设计页面时可以使用。2.编程实现简易灯箱画廊,鼠标单击任一个图像超链接,在底部浮动框架中显示大图像,效果如图4-1所示的页面。图4-1简易灯箱画廊项目2 设计支持音频、视频播放的......
  • CSS 之 自定义属性(变量)
    一、简介​CSS的自定义属性,又称为CSS变量或级联变量,用于定义一个带有值的、可重复使用的CSS属性(变量)。其包含的值可以在其作用域内的任意属性上重复使用,在使用时需要借助var()函数获取自定义属性的值。当自定义属性的值发生变化时,所有使用该自定义属性的CSS属性都会随之变......
  • dumi2.x如何支持css Module?
    在dumi2.x中支持CSSModules是比较直接的,因为dumi基于Umi,而Umi自身就对CSSModules提供了良好支持。以下是基本步骤和注意事项来在dumi项目中使用CSSModules:安装依赖:确保你的项目中已经安装了相应的加载器,如style-loader、css-loader以及可能的less-loader、s......
  • CSS设置水平垂直居中
    在CSS中,有多种方法可以实现元素的水平垂直居中。以下是一些常见的方法:1.使用FlexboxFlexbox是CSS3引入的一个强大的布局模型,可以轻松地实现元素的水平垂直居中。css.container{display:flex;justify-content:center;/*水平居中*/align-items:cente......
  • 自定义CSS属性(@property)解决自定义CSS变量无法实现过渡效果的问题
    且看下面的代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>demot</t......