http://stackoverflow.com/questions/1182189/css-child-vs-descendant-selectors

163
28
|
I am a bit confused between these 2 selectors. Does the descendent selector:
select all
p within a div whether or not it's an immediate descedent? So if the p is inside another div it will still be selected?
Then the child selector:
Whats the difference? Does a child mean immediate child? Eg.
vs
will both be selected, or not?
| ||||
|
262
|
Just think of what the words "child" and "descendant" mean in English:
| ||||||||||||||||||||
|

29
|
Yes, you are correct.
div p will match the following example, but div > p will not.
The first one is called descendant selector and the second one is called child selector.
| ||
15
|
Bascailly, "a b" selects all b's inside a, while "a>b" selects b's what are only children to the a, it will not select b what is child of b what is child of a.
This example illustrates the difference:
Background color of abc and def will be green, but ghi will have red background color.
IMPORTANT: If you change order of the rules to:
All letters will have red background, because descendant selector selects child's too.
|
------------------------------------------------------------------------------------------------------------------
http://stackoverflow.com/questions/22632786/use-a-space-or-greater-than-sign-in-css-selector
5
|
This question already has an answer here:
I have some CSS code:
which does what I want it to do, but also writing it like
will do the very same.
Is there any reason to use one over the other or are they just two different ways of doing the same thing?
| |||
marked as duplicate by Antony, Josh Crozier, chopper, Merlevede, Jan Doggen Mar 25 '14 at 19:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
| ||||
SAME MEANING
-------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<style>
.welcome > div {
font-size: 20px;
color: #f00;
}
</style>
</head>
<body>
<div class="welcome">
<section>
<div>This will be selected</div>
</section>
<div>This will be selected as well</div>
</div>
</body>
</html>
---------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<style>
.welcome div {
font-size: 20px;
color: #f00;
}
</style>
</head>
<body>
<div class="welcome">
<section>
<div>This will be selected</div>
</section>
<div>This will be selected as well</div>
</div>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<style>
span { color: blue; }
div.one span { color: red; }
div.two > span { color: green; }
</style>
</head>
<body>
<div class="one">
<span>Span 1.
<span>Span 2.</span>
</span>
</div>
<div class="two">
<span>Span 1.
<span>Span 2.</span>
</span>
</div>
</body>
</html>


