Sunday, October 31, 2010

5 Advanced CSS Pseudo Classes that will Save your Day

CSS3 provides powerful pseudo-classes that allow the designer to select multiple elements according to their positions in a document tree. Using these pseudo-classes can be a little confusing at first, but it becomes a lot easier over time to set up your layout.

In today’s article I’m going to take a look at 5 pseudo-classes that will simplify our design process and reduces a lot of time to create a certain visual effects. You will also find demonstration below each point to demonstrate how we can use these selectors in different design scenario you face everyday in your designing process.

1. The ‘nth-child’ Pseudo-selector

One of my favorite pseudo-selectors is the nth-child. This can be very useful if you are dealing with a Content Management System where you have no ability to change the server-side code to add classes into the markup.

How to use the nth-child

If you put simply a number in the parentheses, it will only affect the element associated with that number. For example, here is how to select only the 3rd element:
view plaincopy to clipboardprint?

1. ul li:nth-child(3) {
2. background-color: #333;
3. }

ul li:nth-child(3) {
background-color: #333;

Now let’s look at the table below for Pseudo-class Expressions, the first column of the table represents values for n, and the other columns display the results (for N) of various example expressions.
This pseudo-class matches elements on the basis of their positions within a parent element’s list of child elements. The pseudo-class accepts an argument, N, which can be a keyword or a number. The argument N can also be given as an+b, where a and b are integers (for example, 3n+1). For example, if the argument is 3, the third element will be selected.

Thus the expression 2n+1 will match the first, third, fifth, seventh, ninth, and so on, elements if they exist. This can be used to create alternated
  • background color.
    view plaincopy to clipboardprint?

    1. ul li:nth-child(2n+1) {
    2. background:#dfe6ec;
    3. color: #333;
    4. }

    Read More at :