So often I’ll be working on a website that somebody else coded and have to strip out junk styling. There are a lot of different ways to accomplish the same thing with CSS, but so many people have no idea how much time they are wasting by coding something in a “hacky” manner.

Example.
I was working on a site the other day and noticed that every single paragraph in the main content area of every page of the site had a class of “first-paragraph” on it. So I looked in the external sheet and saw that this was simply to remove the top margin on the first paragraph on every page. Ok, so that method worked..great. Except that manually adding a class to every paragraph tag is such a waste of time. Check out the demo where I show an example of targeting the first paragraph in a content area without adding any classes or IDs paragraph tag in the XHTML. Then keep reading to see how to properly code the example above.

View Demo

Here is the code to put in your external CSS sheet to select the first paragraph in a section (in this example a div called “content”). In this example I remove the top margin. If the coder in my example above had done this he wouldn’t have need to add any class to the paragraph tags in the XHTML. Don’t ya just love CSS?

#content p:first-child {margin-top:0px}

Leave a Reply

Spam Protection by WP-SpamFree