100 words 051

I’ve been thinking about what Harry said recently about logic in CSS. I think he makes an astute observation.



You can think of each part of a selector as a condition:



condition { }


That translates to code like:



if (condition) { }


So if you have a CSS selector like this:



condition1 condition2 condition3 { }


…it translates to code like this:



if (condition1) {
if (condition2) {
if (condition3) {
}
}
}


That doesn’t feel very elegant, even in its simpler form:



if (condition1 && condition2 && condition3) { }


I like Harry’s rule of thumb:




Think of your selectors as mini programs: Every time you nest or qualify, you are adding an if statement; read these ifs out loud to yourself to try and keep your selectors sane.


 •  0 comments  •  flag
Share on Twitter
Published on May 12, 2015 15:24
No comments have been added yet.


Jeremy Keith's Blog

Jeremy Keith
Jeremy Keith isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Jeremy Keith's blog with rss.