Introduction complex selector
- Composite selector is actually very good understanding, saying that white blood just like life, like members of our family, through the label or
class
property orid
properties, look for the corresponding blood of a selector, specific everybody down Kane . - If it is written before beginners can look not very understanding of the author of the basic fundamental selector selector article, what basic CSS selectors are? How basic choice is working , but the use of more basic introduction in which the selectors.
Description compound selected table
Selector | description | For example |
---|---|---|
Selector Selector 1 2 {attribute: value;} | Multi-element selector. While matching the selector 1 and selector 2, among a plurality of selectors can be separated by commas. | h1,h2,h3{color: red;} |
EF {attribute: value;} | Descendant element selector, F match all elements belonging to the element E progeny, between the E and F can be separated by a space. | .box h1{color: red;} |
E> F {attribute: value;} | Child element selector to match all elements of the child elements E F | div >h1{color:red;} |
E + F {attribute: value;} | Adjacent element selector, matching all keeping up with the sibling elements after the element E F | div+div{color:red;} |
Multi-element selectors
- Multi-element selectors often used in work which is mainly used to set multiple elements using the same
CSS
style. - Let us enter the multi-element selector practice, practice content such as: the
HTML
pagediv
labels,h1
tags,p
labels, text color to red. -
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>多元素选择器</title>
</head>
<style>
div,h1,p{
color: red;
}
</style>
<body>
<div>成功不是打败别人,而是改变自己。</div>
<h1>成功不是打败别人,而是改变自己。</h1>
<p>成功不是打败别人,而是改变自己。</p>
</body>
</html>
-
Results Figure
-
Note: the role to the list of all the choice of setting the style,
class
class selector orid
selectors is the same, where they tookclass
the class selector property value.box
, for example, and the rest we can try it yourself. -
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>多元素选择器</title>
</head>
<style>
.box,h1,p{
color: red;
}
</style>
<body>
<div class="box" >成功不是打败别人,而是改变自己。</div>
<h1>成功不是打败别人,而是改变自己。</h1>
<p>成功不是打败别人,而是改变自己。</p>
</body>
</html>
-
Results Figure
Descendant element selector
- Let us enter descendant element selector practice, practice content such as: the
class
attribute value.box
descendant element text color set to red, to introduce the next structure:class
attribute value.box
in a total of three sub-elements, the firsth1
label, the secondh1
label, the thirddiv
label, but the third child element todiv
the label there is also a sub-element toh1
label or could be interpreted asclass
attribute value.box
of an element grandchildren. -
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>后代元素选择器</title>
</head>
<style>
.box h1{
color: red;
}
</style>
<body>
<div class="box" >
<h1>成功不是打败别人,而是改变自己。</h1>
<h1>微笑是最初的信仰</h1>
<div>
<h1>成功不是打败别人,而是改变自己。</h1>
</div>
</div>
</body>
</html>
-
Results Figure
-
Note: descendant selectors can only be included in the
class
property value.box
of all the properties inside.
Child element selector
Let us enter the sub-element selector practice, practice content such as: the
class
attribute value.box
sub-element of the text color to red, to introduce the next structure:class
attribute value.box
in a total of three sub-elements, the firsth1
label, the secondh1
tag,div
label, but the third child element todiv
the label there is also a sub-element toh1
label or could be interpreted asclass
attribute value.box
of an element grandchildren.-
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>子元素选择器</title>
</head>
<style>
.box > h1{
color: red;
}
</style>
<body>
<div class="box" >
<h1>成功不是打败别人,而是改变自己。</h1>
<h1>微笑是最初的信仰</h1>
<div>
<h1>成功不是打败别人,而是改变自己。</h1>
</div>
</div>
</body>
</html>
-
Results Figure
-
Note: sub-element selectors and descendant selectors is not to like elements, the sub-element selector is the
class
attribute value.box
sub-elements setCSS
the style, descendant element selector is theclass
attribute value.box
all the elements set inCSS
style, it is now should understand why thediv
label ofh1
the reasons the label text color is not rendered it, becausediv
tagh1
label is grandchildren.
Neighbors selector
- Neighbors selector must meet the following criteria will match.
E
Elements andF
element must be a sibling relationship, meaning that the same generation relations.E
Elements andF
element must be must be next, it is among the barrier element can not have any.- Required
F
element must be inE
the following elements. - Let us enter the adjacent element selector practice, practice content such as: The
HTML
pageclass
property value.box
neighboring elements of the text color to red. -
Block
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相邻元素选择器</title>
</head>
<style>
.box + h1{
color: red;
}
</style>
<body>
<h1>我在上面学习</h1>
<div class="box" >
<h1>成功不是打败别人,而是改变自己。</h1>
</div>
<h1>我在下面学习</h1>
</body>
</html>
-
Results Figure
-
Note: We must pay attention to more than
2
one must be1
a certain rules, otherwise theCSS
style will not be rendered.