Suggest a Snippet Login Register
  • LOG IN:

    Not a member? JOIN NOW!
     Username                   Password
    Remember me Recover password
DevSnippets Design Community & Snippets Gallery
  • Subscribe by RSS
>
  • Webdesign
    • Design
    • HTML5
    • lists
    • Menu
    • Navigation
    • Portfolio
    • Showcase
    • Template
    • Tooltip
    • Tutorial
    • webdesign
  • Web development
    • AJAX
    • CodeIgniter
    • CSS
    • CSS3
    • Database
    • Javascript
    • jQuery
    • Mootools
    • MySQL
    • PHP
    • PHP Framework
    • plugins
    • WordPress
    • WordPress Hacks
  • Inspiration
    • Fonts
    • Icons
    • Inspiration
    • Mac
    • Mac apps
    • Photography
    • Wallpapers
  • Snippets
In {Articles} -8th Mar- {34 Comments}

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.
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.

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:

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.
The 'nth-child' Pseudo-selector
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 <li> background color.

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

The 'nth-child' Pseudo-selector

Check out our demo.

Browser Support:
Internet Explorer has no support at all for the :nth-child pseudo-class. But if you are using jQuery, which supports all CSS selector including :nth-child, the selector will work, even in Internet Explorer.

Further Resources on how to use the nth-child

  • - Understanding :nth-child Pseudo-class Expressions
  • - How nth-child Works
  • - Cleaner Code with CSS3 Selectors

The :target pseudo-class

This pseudo-class affects an element that’s the target of a fragment identifier ( point to a specific element on a page, represented by a “#” and an anchor) in the document’s URL. When we click on a link that ends with a fragment identifier that element we are pointing to becomes the target (hence :target).

How to use the target

For example, if the URI was http://devsnippets.com/article/5-advanced-css-pseudo-class.html#nth, the following selector would match the element that had an id attribute of “nth”:

div > div:target {
background:#FFC1E4 none repeat scroll 0 0;
border:3px solid #EF4F7A;
padding:10px;
}

The 'target' Pseudo-selector

Check out our demo.

Browser Support:
Internet Explorer has no support at all for the :target pseudo-class. Opera doesn’t support this selector when using the back and forward buttons. Other than that, it has support from the other major browsers.

Further Resources on how to use the target

  • - The CSS3 :target Pseudo-class And CSS Animations

3. Use the :focus Pseudo Class

You can apply styling to your input areas and textareas that only takes affect when a user has clicked into that area using the :focus pseudo class. For example, you could change the background and border color on those elements like so:

textarea:focus, input:focus {
	background:#FFC1E4;
        border:3px solid #EF4F7A;
}

The 'focus' Pseudo-selector

Check out this demo.

Browser Support:
Most browsers support the :focus Pseudo Class.

4. The negation pseudo-class: :not()

The :not() pseudo-class is extremely useful when you want to apply a style to a class or group of elements, and want to exclude some element(s).
The code below selects all div elements that do not have .comment class.

div:not(.comment) {
border:1px solid #333
}

You can use a comma to choose more than one class. The code below selects all div elements that do not have .comment or .post classes.

div:not(.comment, .post) {
border:1px solid #333
}
Browser Support:
The :not() pseudo-class is only supported by modern browsers (Firefox, Safari and Opera), but not internet explorer.

5. The only-of-type pseudo-class

The only-of-type pseudo-class matches an element that’s the only child element of its type.
This selector will match an img element that’s the only child img element of its parent element:

.post > img {
	float: left;
	}

.post > img:only-of-type {
	float: none;
	margin: 10px;
	}

only-of-type

Check out our demo.

Browser Support:
The only-of-type pseudo-class is only supported by modern browsers (Firefox, Safari and Opera), but not internet explorer.

Author: Noura Yehia

Noura Yehia is a Web Designer, Blogger and Creative Thinker. Founder of Noupe.com a popular blog about web design, tutorials, resources and inspiration. If you’d like to connect with her, you can follow her on Twitter or at her blog Devsnippets.

Category: Article, CSS | Leave a Comment | 34 Comments

Related Posts

  • 12 Elegant, Free & High Quality HTML5+CSS3 Templates 12 Elegant, Free & High Quality HTML5+CSS3 Templates
  • Designing for the Future with HTML5+CSS3 : Tutorials and Best Practices Designing for the Future with HTML5+CSS3 : Tutorials and Best Practices

34 Comments, Add Comment or Ping

  1. Alex says:
    March 8, 2010 at 4:40 pm

    $20 bucks on CSS4 implementing a full-fledged scripting language, to facilitate interactive animations and whatnot. It’s just a matter of time.

    When will we see the first “CSS exploit”? :-)

  2. Steven says:
    March 8, 2010 at 5:06 pm

    @Alex: Don’t have to wait for CSS4. Webkit already has built in CSS animations and all sorts of advanced selectors, none of which are part of the CSS spec.

  3. Molly Holzschlag says:
    March 8, 2010 at 8:45 pm

    We have all that power now in SVG, and have had it for some years in most contemporary browsers.

    • DeepDown says:
      March 9, 2010 at 7:06 am

      “We have all that power now in SVG”

      Thanks Molly for pointing that out!

  4. temp says:
    March 9, 2010 at 12:13 am

    thanks, really cool stuff

    that last one was a bit hard to understand without an actual example tho.

  5. JP DeVries says:
    March 9, 2010 at 1:49 am

    Internet Explorer is so lame

    • ilz says:
      March 12, 2010 at 9:00 am

      Agreed. IE8 came out and big surprise, it doesn’t support anything either. Great.

      • Essex Landscaper says:
        March 18, 2010 at 3:32 am

        Checkout IE9 preview, has a lot more promise to it !

  6. Dimitri Fengshui says:
    March 9, 2010 at 4:14 am

    You can defines a CSS type selector to set the skin for all components of a given type.

  7. Keith Clark says:
    March 9, 2010 at 5:23 am

    If your happy to use a JavaScript solution, you can emulate most of these selectors in Internet Explorer 5.5 to 8 with ie-css3.js

    http://www.keithclark.co.uk/labs/ie-css3/

  8. Jordan Walker says:
    March 9, 2010 at 8:15 am

    That is really help about little know pseudo classes.

  9. Dzinepress says:
    March 9, 2010 at 11:02 am

    helping techniques for web designers and developers.

  10. Art2code says:
    March 9, 2010 at 11:44 am

    Nice and cool techniques !
    thx

  11. Timothy says:
    March 10, 2010 at 6:34 am

    Just check out http://www.w3.org/TR/css3-selectors/#selectors

  12. Liam O'Leary says:
    March 11, 2010 at 10:13 am

    Great article, I will be using these on some of our new websites, it’s gona make life a little easier, apart from MS Internet Explorer not supporting most of them, grrrr Microsoft you loosers, get it sorted in IE9!

  13. Tidy Design says:
    March 11, 2010 at 10:27 am

    Very tidy!! Love to read/learn more about css… It is just so cooool :D

  14. WallMountedHDD says:
    March 11, 2010 at 5:11 pm

    Figures this is CSS3. All this CSS3/HTML5 stuff is such a huge tease. I’m learning it, but I’m not using any of it yet. It’ll be a while…

  15. CSS Madness says:
    March 11, 2010 at 6:53 pm

    Love it! Thanks for the pseudo class article mate

  16. InfamousLA says:
    March 11, 2010 at 8:06 pm

    @Alex I got $20 on it too – as it is CSS3 is becoming more intricate with all these advanced selectors. What happened to it’s purpose of taking care of deprecated presentational tags in HTML? Isn’t that one of the reasons you have Javascript , and what not.

    Great article though.

  17. shevaa says:
    March 11, 2010 at 9:10 pm

    Cool… Nice Stuff…

  18. Ivan says:
    March 31, 2010 at 7:46 am

    when using negation I’m not sure you’re allowed to use commas to exclude more than one element.

    The correct way to do it:
    div:not(.comment):not(.post) {
    css
    }

Trackbacks/Pingbacks

  • You are now listed on FAQPAL
  • CSS Brigit | 5 Advanced CSS Pseudo Classes that will Save your Day
  • 5 Advanced CSS Pseudo Classes that will Save your Day | DevSnippets | CSS Guru How to CSS
  • 2010-03-12 유용한 링크 | We are connected
  • Linkhub – Woche 10-2010 « pehbehbeh
  • 5 Advanced CSS Pseudo Classes that will Save your Day | DevSnippets | [codepotato]
  • dropjack.com
  • 5 Advanced CSS Pseudo Classes | adkarta.com
  • Best Of Web And Design In March 2010 | Creative Nerds
  • Best Of Web And Design In March 2010 - Programming Blog
  • Adobe Photoshop How To | Best Of Web And Design In March 2010
  • [Linkdump #2] CSS3 na koniec tygodnia. « Tomasz Kowalczyk
  • 12 Elegant, Free & High Quality HTML5+CSS3 Templates | DevSnippets

Sponsored Ads

ADVERTISE HERE

Sponsored Ads

DevSnippets Latest Articles

  • Using Illustrations in WebDesign: 30 Creative Examples
    Tags: Illustration, Inspiration, webdesign Jul/20/2010
  • Dark and Mysterious High Quality Desktop Wallpapers
    Tags: Dark, Wallpapers Jun/23/2010
  • 10+ Most Beautiful Island Photography on Earth
    Jun/16/2010
  • 35 High Definition iPad Wallpapers For A Great Experience
    Tags: Inpiration, Wallpapers Jun/09/2010
  • 30 Unique Logo Designs That Actually Say Something
    Tags: Branding, Inspiration, Logo Jun/02/2010
  • Creating 10 Most-Used Javascript Techniques Using Pure CSS Styling
    Tags: CSS, Tutorial May/31/2010
  • 40 Creative Advertisements With Unexpected Ideas
    Tags: Advertising, Inspiration May/27/2010
  • Getting Slim, Spicy, & WordPress-Ready with Top 10 jQuery Plugins
    Tags: jQuery, WordPress May/25/2010
  • Spicing Up Your Tumblr Experience: Beautiful Themes, Icons and Apps
    Tags: Templates, Tumblr May/19/2010
  • 20 NEW & FREE High Quality (X)HTML/CSS Templates
    Tags: CSS, Design, Template, Tutorial May/17/2010
More Articles →

Design Community News

  • Is It Worth Shifting To Photoshop CS5?

    SloDive
    Jul/04/2010
  • Free Wordpress Theme Vector Nature Template

    templates4all
    Jul/04/2010
  • Best jQuery plugins – June 2010

    Spider8411
    Jun/22/2010
  • Freebies: Vintage Mega Pack 10 Samples from Designious.com

    Doink
    Jun/22/2010
  • CSS3 Animations, the Power Back to CSS Part 1: Transitions

    Teylor Feliz
    Jun/22/2010
  • Create a Photo-Realistic Candle with Basic Photoshop Tools

    Frank
    Jun/22/2010
  • Credit cards and payment icon set

    icon sets
    Jun/22/2010
  • 40 Refreshing Nature Inspired Web Designs

    Brukhar
    Jun/22/2010
  • How to Create a Tumblr Theme (Code Structure)

    Dainis Graveris
    Jun/21/2010
  • The Simple Photoshop CS5 Cheat Sheet

    Frank
    Jun/21/2010
More News →

Our Friends

  • Graphic Design School

  • Designm.ag

Top ↑

Add a Snippet / Design News

© 2008-2010 DEVSNIPPETS | Made by Noura Yehia | Submit Snippet | About| Subscribe | Advertise
In partnership with (mt) Media Temple