CSS is not a pretty language. While it is simple to get started with, it soon becomes problematic at any reasonable scale. There isn’t much we can do to change how CSS works, but we can make changes to the way we author and structure it. In working on large, long-running projects, with dozens of developers of differing specialities and abilities, it is important that we all work in a unified way in order to keep stylesheet maintainable, scalable, and to keep code transparent, sane and readable as well.
MySolidWorks site was initially built up with Boostrap 2 using the LESS CSS Preprocessor. CSS preprocessing is a method of extending the feature set of CSS by first writting the style sheets in a new extended language, then compiling the code to vanillar CSS so that it can be read by Web browsers. LESS is one of most notable of several CSS preprocessors like SASS.
LESS was designed to be as close to CSS as possible, so the syntax is indentical to the current CSS code, and can be used right away with the existing code in dev. SASS was designed to both simplify and extend CSS, so things like curly braces were removed from syntax. But SASS also introduced a CSS-like syntax called SCSS (Sassy CSS) to migrating easier. Bootstrap 3 still uses LESS for styling, but also provides an official SASS port to SASS/SCSS.
Now we are upgrading the framework from bootstrap 2 to 3, and also moving from LESS to SCSS in consideration of getting ready for furhter framework upgrade, since Bootstrap 4 will say goodbye to LESS.
We learn that all common CSS methodologies are trying to separate content from structure by placing styles in reusable modular blocks of code. Among them, the OOCSS Framework is an methodology similar to Bootstrap Framework CSS for its rules of classing, subclassing and naming convention. This can be an appraoch to derive a standard of writing our CSS.
Although separating frond-end from back-end is an ideal technique to develop web projects, MySolidWorks was not built in that way. Every developer has chance to touch CSS, creat new classes, edit and modify them at will without guidelines. Along with our website grows, our CSS gets messier. There is obviously a need for a better system and standard of writing CSS so as to keep code maintainable, easy to read and standard-based. Having good coding guidelines, when well followed, will:
One of the simplest forms of guidelines is a set of rules regarding syntax and formatting. Having a standard way of writing (literally writing) CSS means that code will always look and feel familiar to all members of the team.
Further, code that looks clean feels clean. It is a much nicer environment to work in, and prompts other team members to maintain the standard of cleanliness that they found. Ugly code sets a bad precedent.
Basic Syntax
:
in property declarations and before {
in rule declarations - this makes code easier to read. SASS syntax enforces you to place space after each semicolon.
rgb()
, hsl()
, or deg()
values.
white
or blue
to define color, use all hex values in lowercase. #fff
instead of #FFF
. And use shorthand hex values where possible #fff
instead of #ffffff
. Remember that you can shorten hexes like #aabbcc
to #abc
rgba(0,0,0,.4)
) and transition durations (e.g. margin .5s linear
)
margin
, padding
, font
, background
and border
where available (margin: 5px 10px;
instead of margin: 5px 10px 5px 10px;
). And avoid specifying units for zero values (margin: 0;
instead of margin: 0px;
input[type="text"]
). Even though they are optional in some cases, it is a good practice to use them for consistency.
Naming Covention for Class Selectors
.navigation-top-bar-stripped
instead of simple .navigation-top
. But do not shorten it to .nav
or even .n
because it might not have sense for other developers working with you and will not have sense for you after sometime.
.navigation-top-bar-stripped
instead of simple .navigation-top
. But do not shorten it to .nav
or even .n
, because it might not have sense for other developers working with you.
-
as a separator in a multi-word class names. It's clear, readable and distinguish CSS from underscore case and camel case naming.
nouns
for naming objects and modules.