fsck CSS

An experiment in cleaning up CSS and HTML with modern tools like flexbox and grids.


Cleaner CSS

This website is nothing more than a set of small demos for the idea of using flexbox and grids to clean up and simplify CSS. Most everything else about CSS stays. Filters, transforms, attributes, and everything that applies to the visual display of the components of a design. What gets removed is div heavy, class heavy, not-semantic-at-all layout systems cluttering the real information available through simple HTML tags.

In short, if you're writing this:

You could write this if you use flexbox, grids, and variables:


I demonstrate the idea with a series of copies of existing websites and other layout problems people frequently encounter. If you have a suggested layout challenge for me, then tell me on Twitter @lzsthw and I'll give it a shot.


What's wrong with classes?

There's nothing wrong with classes in CSS. How they're used in recent CSS subverts the normal specificity rules making CSS unecessarily difficult. The original specificity rules put the base design of a component at the tag level, then a class can be used to modify and extend the design, and an id can make this even more specific. In today's use of div.class heavy markup you lose this ability to alter the design as needed where its used, and instead you have to resort to !important and specificity hacking to change how something looks, or add more div with more class.

The other problem with div.class is it's more difficult to debug problems with the layout. Because you're relying on specificity that can come from anywhere in the cascade it's much more difficult to find exactly what bit of CSS is overriding your local decisions. If you've ever had to open the inspector in your browser to troll through CSS locations looking for the culprit then that's why. If the base design decisions are at the tag level, then your local class and id specifics will work as expected. You can also replace many uses of class as design modifiers with scoped CSS variables and reduce the use of div.class even further.

Finally, using div.class heavy layout systems polutes your markup with structure that's difficult to maintain and change over time. When you focus on using truly semantic named tags and follow specificity as intended then your HTML is lighter and easier to understand because it isn't full of div.grid.col-1.col-mx-auto class rules that really have nothing to do with the actual meaning of that particular visual element. With flexbox and CSS grids you don't need any layout divs, and can keep the HTML clean and semantic. This also ends up being more flexible with less or the same effort as using a pre-made grid system full of divs.

Should we ban div.class?

Hell no. The current problem of too much class based CSS is because of trends in recent years calling for everyone to irrationally ban some practice. Remember when everyone decided tables were evil? So then we banned tables and grids completely? Then nobody could do a layout without crazy tricks and JavaScript? Nobody wants a repeat of the anti-tables "semantic markup" war, so please don't ban div.class.

All this website does is demonstrate that you don't need so much div.class. You can scrap a lot of what you're using now to do layout and go with clean easy to read HTML that has a nicer flat structure and is easy to maintain, then judiciously add in any extra things you find you need like div.class. Treat div.class like salt. Right now you're pouring a whole box of it on your HTML when a little bit makes it taste better.

How did it get like this?

Before flexbox and CSS grids there really was no choice but to use div.class to make grids for layout. Since everyone in the CSS design world irrationally banned tables as a layout mechanism your only choice was to invent another table but call it div.grid.col-1.

Today we don't have to use divs for layout anymore. It's entirely possible to use flexbox and CSS grids to solve many layout problems, and to do it without too many irrelevant alterations to the meaningful HTML. You no longer have to wrap an img with 5 div tags just to overlay it on a background. You can write the img, maybe a figure and then use CSS to do anything you want to it.