Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Would like some advice on CSS formatting

Hi, I am looking for some opinions on a CSS formatting discussion I am having with another front end developer. I am going to try and keep it short as this has been a long discussion.

We have a developer who like to style most of his/her CSS in relation to the body.

Example:

body > .wrapper > article.main > [itemprop="articleBody"] section:first-child {  }

this is a really short example, some lines have loads of *, ~ and +

Now I find that this is so overly complicated and could be done with a simple class like

.post-content {  }

I have less experience so I am just ignored when I mention this. But I am the one that has to update the site and I am finding simple things like creating a new page and putting the blue box from page A onto the new page extremely overly complicated and I find that I a) have to make a even bigger more specific string of selectors b) I have to go through his code and change his long string to a simple class. But then as I do this everything that is after the blue box on page A has now lost some of its formatting so I have to fix that too.

I would really like a second opinion on this so any comments would be much appreciated

4 Answers

Your goal should be to write a selector that is as specific as needed but no more. Think of CSS specificity as a precious resource you want to conserve until you need it.

Rule of Thumb: If a selector is longer than 3 elements (or classes) you are probably in a specificity war.

Here's a great article about CSS specificity. You can use this specificity calculator if you think you might be in a specificity war.

Here's a great reference on CSS specifity:

source: http://www.standardista.com/css3/css-specificity/

Bigger PDF Version

Thanks for the reply and the article James!

I'm down with what James pointed out. There's no need to target your selectors nested that deep. Throw a class on your element, and just use that. There's absolutely no need to do anything more.

The problem being, if you guys go to change any of those elements later, you'll have to use all those nested selectors all over again. Big ordeal.

Point your fellow front-end dev towards the philosophies of Nicole Sullivan and Harry Roberts and let them think on it for themselves.