Playing with CSS
##CSS Selectors
###Meet The Family
There are parents, children, siblings, descendants and ancestors.
Siblings
- also known as adjacent siblings. Just like in your family, siblings share a parent.
<div id="parentOfParagaphs"><!--Also an ancestor of the anchor-->
<p>Child of the div above, sibling to the paragraph below.</p>
<p>Child of the div above, sibling to the paragraph above and parent to the anchor. <a href="#">Child of the paragraph, sibling of none and descendant of the div.</a>.</p>
</div><!--Everything in the div is a descendant to the div-->
###The Child Selector
The child selector is represented by the greater than symbol: >
.
#someDiv > p {
color: blue;
}
###Order
<div id="example3">
<p>First paragraph</p>
<p>Second paragraph</p>
<div id="nestedDiv">
<p>Third paragraph</p>
</div>
</div>
#example3 p {
color: red
}
#example3 > p {
color: blue
}
#example3 > p {
color: blue
}
#example3 p {
color: red
}
###Chaining If you really want to go nuts, you can chain child selectors together. In the following example, I first targeted any children paragraphs of the example3 div and changed them to blue, then I targeted any paragraphs that were the children of divs that were also children of another div and changed those to red.
<div id="example3">
<p>First paragraph</p>
<p>Second paragraph</p>
<div id="nestedDiv">
<p>Third paragraph</p>
</div>
</div>
#example3 > p {
color: blue;
}
div > div > p {
color: red;
}
###The Adjacent Sibling Selector
The adjacent sibling selector is represented by the plus symbol: +
.
<div id="example4">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
<div id="nestedDiv">
<p>Fourth paragraph</p>
</div>
</div>
p + p {
color: red;
}
In our CSS, we used the adjacent sibling selector, which changed the second and third paragraph to red. This is very tricky isn’t it? instinctively, we would expect the first paragraph to be red as well. After all, the first paragraph is on the same level of the tree as the next two and has siblings. However, this selector only applies to elements that are preceded by something else. In this instance, only those paragraphs preceded directly by a sibling paragraph will be targeted. The first paragraph in the list is preceded by the div, so it isn’t changed.
###Further Chaining
<div id="example5">
<p>First paragraph</p>
<p>Second paragraph</p>
<div id="nestedDiv">
<p>Third paragraph</p>
<p>Fourth paragraph</p>
</div>
</div>
div > p + p {
color: red;
}
In this example, we targeted paragraphs that were children of divs that were also preceded directly by another paragraph.
###Asterisk### ####Universal Selector####
* {margin: 0; padding: 0;}
#someDiv * {
color: red;
}
####Arbitrary Substring Attribute Value Selector####
div[id*='section'] {color: red;}
<div id="section-1">
</div>
<div id="section-2">
</div>
<div id="section-3">
</div>
<div id="section-4">
</div>
###Float Values###
###Clearing the Float### Float's sister property is clear. An element that has the clear property set on it will not move up adjacent to the float like the float desires, but will move itself down past the float.
###ClearValues###
Generally, a floated element should have an explicitly set width (unless it is a replaced element, like an image). This ensures that the float behaves as expected and helps to avoid issues in certain browsers.
####Live Demos#### Floating
####Useful Links#### the-mystery-of-css-float-property
#CSS
position: absolute
really does is position the element relative to its first non-statically-positioned ancestor (inherit doesn’t count either)####Live Demos#### Positions
####Useful Links#### the-lowdown-on-absolute-vs-relative-positioning