Thứ Tư, 28 tháng 11, 2012

Understand ‘+’, ‘>’ and ‘~’ symbols in CSS Selector

It explains How to use different signs (+,> and ~) in CSS selector and their differences. Before starting, let us take a sample code to understand the signs.
?
1
2
3
4
5
6
7
8
<div id="container">
   <p>First</p>
    <div>
        <p>Child Paragraph</p>
    </div>
   <p>Second</p>
   <p>Third</p>
</div>

Space:

?
1
2
3
div#container p{
font-weight:bold;
}
It is the descendant selector. It will target all p tags within container div.

> Sign:

It will target elements which are DIRECT children of a particular element.
?
1
2
3
div#container > p {
  border: 1px solid black;
}
 Understand +, > and ~ symbols in CSS Selector
It will target all P element which are direct children of container div, not children of child div.

+ Sign:

It is Adjacent sibling combinator. It combines two sequences of simple selectors having the same parent and the second one must come IMMEDIATELY after the first.
?
1
2
3
div + p {
   color: green;
}
css%20selector%203 Understand +, > and ~ symbols in CSS Selector
It will only select the first element that is immediately preceded by the former selector. In our example, it will target to Second ONLY because the owner P element comes just after Div tag.

~ Sign:

It is general sibling combinator and similar to Adjacent sibling combinator. the difference is that the second selector does NOT have to immediately follow the first one means It will select all elements that is preceded by the former selector.
?
1
2
3
div ~ p{
background-color:blue;
}

css%20selector%204 Understand +, > and ~ symbols in CSS Selector
It will target both second and third.
Hope, you enjoyed this.

(http://techbrij.com/654/css-selector-adjacent-child-sibling)

Không có nhận xét nào:

Đăng nhận xét