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

Luke Lee
Luke Lee
7,577 Points

Is it DRY css really practical??

I understand and absolutely agree with the DRY principle. A DRY css file is like a clean, no-material wasted blue-print. It is so well-planed, that every style can be fully used and there is no unnecessary overrides. But in reality, is that really practical? I built a few websites with DRY css, I spent A LOT time to pre-plan and refine the css file, I had to go back to re-structure the classes and styles. But when a client want to make changes, even small changes, I need to plan the class structure again!

For example. h1 font style is grouped with h2, h3 in line 1, but it's color style is in a group with other classes in line 1000, and it's display style is in a group with other classes in line 2000.

2 Answers

Adam Moore
Adam Moore
21,956 Points

I believe the idea and practicality of it is to attempt to make the list possible coding for the best possible outcome. So in times when an h1 needs to be set to different things in different areas, if that is necessary for the best readability for the best outcome, then that would seem to me to be the best option. I also believe that the idea isn't to spend too much effort to try to condense code by only a few characters or lines, simply for the sake of DRY. I'm also assuming that it's an art and skill that is developed gradually over time and as one progresses in education and practice. Essentially, instead of having 3 separate declarations and sections for h1, h2, and h3 that have the exact same properties and values, putting them together into one declaration would be simple, as it would be cumbersome and superfluous to make them into 3 different declarations; spending lots of time deciding to put a property here or there in order to try to have the "perfect" DRY css would be impractical, not to mention nearly impossible.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I would think of DRY as a worthy goal of CSS coding but not an expected final outcome, especially with larger projects.

Even in SASS in which you can use DRY to write clean code, it produces CSS that repeats itself. DRY is a good thing to think about but so long as your code is clean and valid I wouldn't worry too much about it.