How We Could Write Better CSS
colmtuite.com
colmtuite.com
This also enables inconsistent UI in the front-end: some of my page-headers can have .mbl "Large bottom margins" and some could have .mbs "Small bottom margins".
would become: <li class="MyImportantItem">
with the SASS being: .MyImportantItem { @extend .u-paddingLarge; @extend .t-important; }
You can avoid writing inline styles, keep things DRY, and still have compose-able styles.
If you want to update all the large margins across the app, you can do that in one place. That wouldn't be possible using inline styles.
You can also enforce style guide standards. Only three margin sizes are allowed: small, medium, large. Inline styles obviously allow for much more variability.
Sure can, your pages are generated, generate the styles from config or constants. There, I fixed it.
Edit: your site's visual design is awesome, btw
Just because the output isn't 100% optimal doesn't mean the input isn't sane, maintainable, or clean.
I think the Facebook Way (tm) has its pros, but using page-level specificity is a great way to avoid specificity hell.
To be more specific, for a site with vastly different pages, it's best to have page or section level class that limits the scope of each css definition, so the person making change to one section of the site will not accidentally break other pages. Also, with little base style, there is less need to override styles with more specificity.
For such a site, CSS like this tend to be easier to maintain:
#home-page h2 { /* home page title styles / }
.news-item h3 { / news item title / }
#widget-page h2 { / widget page title / }
.widget-item h3 { / individual widget title */ }
When you code CSS the "facebook way" :(, you don't have to worry about bleeding effects.
Want your h1 to have a margin of 35px or whatever? You give it the class large-margin or something. You don't have to worry about affecting other h1's, because they have the classes specific to the way they should be styled.
When you style in the way the root comment is suggesting, you do have to worry about bleeding styles, because selecting h1 tags in one section may also style an additional h1 tag you didn't mean to style.
The whole point is: don't select specific elements to style, rather, create classes of types of styles, and, when creating an element, select the classes of styles you want the elements to have. This way you don't worry about bleeding styles, each element has the classes representing how they are supposed to be styled, and you will never accidently over-select elements.
There's a trade-off of consistency across your site. I personally think it's not a good design choice to encourage different pages to have different style sheets.
Anyways if you want more specific styles, don't create specific selectors, instead create more specifically named classes so you don't lose the positive properties of the facebook way (Can we please get a better name?).
All I'm trying to say, is that in some cases (i.e. when you only need a particular set of styles for a single page/section), limiting the scope of those style definition can help dealing with specificity hell / bleeding styles, and make the application more maintainable.
Obviously, this is a trade-off, a big one if you want your site to be consistent across pages. But there will be occasions where one or more page of a site/app is vastly different from other pages.
If performance (minimizing http requests) is not a big deal, then that'll be end of story. But if it unfortunately is a problem, current tools generally support combine/minimize them into a single file, but not dynamically choosing based on page requested.
What this is is basically "Object Oriented CSS" that became popular years ago and was later refuted by many in the community for it's numerous downsides, namely in maintenance cost, code-clutter and slow performance on old browsers and mobile.
With a preprocessor like SASS you can have OO and have semantic class names. Take that margin example. You could define class names that add varying types of margins. But you don't use them in HTML. You create a semantic class for specific use cases that @extend's the margin class you need.
Now you have the best of both worlds. Less, semantic class names, with the flexibility and consistency of the OO style.
We need this. I've been thinking the same thing as the author for quite some time, I'm glad someone has at least put the idea out there.
If your CSS rules are applied to classes, it will gather the classes first then filter it down (Resulting in a smaller set in the first place).
CSS processing will be much faster.
I know that there's recently been a backlash against the entire idea of the "separation of concerns" - but having your markup define your style margins is a bit insane. If suddenly you wanted 10px margins instead of 5 you have to grep your entire codebase for .m∗s (limiting the length of the string to 3 characters) and updating all those to .m∗m (or just defining both .m∗s and .m∗m to be 10px, which is just as dumb).
But, yes, most of the points in this article are somewhat accurate if not a bit outdated. Try not to use element selectors (and instead apply a class), avoid code duplication, write efficient selectors, etc.
(Note: asterisk changed to low asterisk and I hate markdown or whatever other text styling syntax HN is using).
If you are injecting your markup elements with classes like "mbm mts mrl" (or padding, or anything else that is essentially just a css property), this is not much different than just inlining your styles. If you want this level of reusability in your css, use sass and create these as placeholders. From there give your dom element a some kind of class(es) that can extend several of these placeholder values.
Ian Storm had a very well written article that hits on this more: http://ianstormtaylor.com/oocss-plus-sass-is-the-best-way-to...
Meaning, the Mixpanel abuse of styles is an old problem.
What CSS has over most all DTP and word processing apps is that CSS is declarative and explicit and discoverable. With CSS, styling is source code, and can be managed as such.
Or not, like Mixpanel apparently has done.
Edit: I actually wrote a detailed blog post on it: http://sriharisriraman.in/blog/2013/09/08/dont-nest-css/
.home-header-h1{
font-size: 20pt;
padding: 0px;
margin: 0px;
line-height: 1.3;
}
.home-header-h1-blue{
@extend .home-header-h1;
color: blue;
}
.home-header-h1-green{
@extend .home-header-h1;
color: green;
}
This helps keep classes out of your HTML and avoids large amounts of nested CSS. Here's the compiled source: .home-header-h1, .home-header-h1-blue, .home-header-h1-green {
font-size: 20pt;
padding: 0px;
margin: 0px;
line-height: 1.3; }
.home-header-h1-blue {
color: blue; }
.home-header-h1-green {
color: green; } .home-header-h1{
font-size: 20pt;
padding: 0px;
margin: 0px;
line-height: 1.3;
}
h1.home-blue{
@extend .home-header-h1;
color: blue;
}
h1.home-green{
@extend .home-header-h1;
color: green;
}
But I think if I was trying to truly minimize maintenance I would do something like... (in LESS not familiar with sass) h1.home {
font-size: 20pt;
padding: 0px;
margin: 0px;
line-height: 1.3;
&.blue {
color: blue;
}
&.green {
color: green;
}
}Thanks for the feedback. A lot of people seem to be getting hung up on the sass thing. I'm all for sass, I don't think using 15 CSS classes for margins is the best way to handle this. I just wanted to use a watered down illustration for those who don't yet understand sass. Ideally, this should al be abstracted into variables/mixins/extends.
My main point though, is that I'd like to see us working together to decide what the best approach is, then apply that across the board. I don't see any reason for us to continue doing things slightly differently.
Whether you use CSS, SASS, LESS or any other preprocessor, we should be writing reusable CSS. At the moment, that is not happening across the web.
I've two other articles: http://www.colmtuite.com/three-reasons-why-wireframing-tools... http://www.colmtuite.com/a-more-flexible-development-framewo...
The ideal CSS, in my opinion, is just the opposite. The developer writes Plain Old HTML and includes a stylesheet, and the new page looks instantly consistent with the rest of the website. That's how you enable anyone to work on the project. If a new UI is needed, the designers will change the css and instantly all pages will look good.
Now you add customization classes to these special cases instead of shoving them every-fucking-where.
A good place to start doing well structured CSS is to separate the layout from the style. Doing this will give you a much better grip of your CSS and makes for easier future changes and adjustments.
1. LESS - http://lesscss.org/
2. LESS Elements - http://lesselements.com/
3. Pure CSS - http://purecss.io/ [cool stuff, but not LESS.]