All CSS rules are global and almost all newbies misuse this feature. It is extremely easy to end up medium and large projects with overcomplicated styles. If you trapped - you couldn't change any style without modifying the whole bunch of other elements. To prevent these hazards learn and use various methods of organizing the styles:
Use Pixel Perfect Extension if your client has provided you with design mockups
Always set appropriate zoom for the web page to be able to compare all dimensions
CDN is not extremely useful. Possibility that user's browser has already cached the library with appropriate version is very low
Although nothing bad will happen if use imports that will be replace with such tools like Webpack
Each module should have less than 500 lines
Even if you have one rule for given selector - create a separate line for this. This approach is helpful at least for checking diffs in your CVS
Don't forget about hidden layers in files with design
Almost every interactive element (button, input or link) has separate styles for hover or pressed states
Use static layout only if your client demands
All styles of one block should be in one place. All styles should be sorted in order the block is located on the page.
Good:
.header {...}
.header-search-form {...}
.header-search-input {...}
.content {...}
.content-heading {...}
.content-sidebar {...}
Bad:
.header {...}
.content {...}
.header-search-form {...}
.content-heading {...}
.header-search-input {...}
.content-sidebar {...}
There are plenty methods over the web how to do it. For old browser use absolute position and simple padding without negative margins
Don't use mySuperAwesomeElement or my_super_awesome_element
Prefer "loading" to "big-yellow-spinny-thing"
In 99% cases you can just increase the priority of the selector
Flow of an elements is extremely important - try to break it as rare as possible
The relative position is quite confusing, so always try to move elements with margin or padding. Move elements in exceptional cases
However, it is valid property and W3C allows it, negative margins are more confusing than position: absolute
. There is the great guide about such margins, but all techniques can be replaced with more obvious solutions.
Fixed height can lead to confusion of your teammates and overlapped elements. You can use fixed height when there is no other way or it is a requirement of design:
- For example, we have a slider, all elements have
position: absolute
and they move with JS scripts. Here we have to set height to the slider container because all children haveposition: absolute
- In the design we have a list of blocks with fixed height, all overflowing content should be hidden.
Always try to align elements dynamically (with margin: 0 auto
, vertical-align: middle
or modern flexboxes)
Set background color, font-family, font-size and font color for <html>
. Remember that font-size of <html>
tag is a reference point for the rem
s font sizes.
Set cursor: pointer
for all clickable <div>
s, <span>
s etc
Don't set width and height simultaneously, if design requires set both dimension to use background-image: url(...) no-repeat center center
and background-size: cover
This will simplify your code and will make life of your teammates much easier
If you have some words with only upper cased symbols use text-transform: uppercase
. Exception is abbreviation
Outline style is useful for users who fill forms with keyboard
Try to keep all styles of one element in one place including media queries
Use Font Squirrel for generating these files. The .ttf
file will be enough for almost all modern browsers. Please check this blog post to see the version of supported browsers for each font format
Check that your font file contains needed symbols. If you use Font Squirrel for font's files generation and "expert" mode is enabled, check the appropriate checkboxes.
The italic and light/bold versions of fonts are usually separate files with different names. Please keep the font name in CSS the same, the example is:
@font-face {
font-family: "Lato";
src: url("../fonts/lato-regular.woff2") ...; /* Pay attention: font is regulat and "..." replaced for other formats */
font-weight: 400;
font-style: normal;
}
@font-face {
font-family: "Lato";
src: url("../fonts/lato-italic.woff2") ...; /* Pay attention: font is regular italic and "..." replaced for other formats */
font-weight: 400;
font-style: italic;
}
@font-face {
font-family: "Lato";
src: url("../fonts/lato-light.woff2") ...; /* Pay attention: font is light and "..." replaced for other formats */
font-weight: 300;
font-style: normal;
}
@font-face {
font-family: "Lato";
src: url("../fonts/lato-lightitalic.woff2") ...; /* Pay attention: font is light italic and "..." replaced for other formats */
font-weight: 300;
font-style: italic;
}
You should know differences between px
, em
, %
, rem
, vw
, vh
, vmin
, vmax
. Prefer rem
for elements without specific requirements (for example static size or relative to window width)
Example: font-family: Helvetica, Arial, sans-serif;
Helvetica
is a pluggable font with a separate file.
Arial
is a Web safe font.
sans-serif
is a subset, that will be used if Arial is not supported by a client.
Subset can be 'Serif', 'Sans-serif', 'Monospace' and very rarely 'Cursive'.
For 'Serif' we prefer Georgia, for 'Sans-serif' Arial and for 'Monospace' Courier New. We have never used 'Cursive' in practice yet, although you can use Comic Sans for default value :)
For example font-family: "Times New Roman", serif.
MDN says:
Top and bottom margins of blocks are sometimes combined (collapsed) into a single margin whose size is the largest of the margins combined into it, a behavior known as margin collapsing.
Be aware that margin of first child will move the parent in most cases too (learn more in the article on MDN)
Please, learn and remember this thread on StackOverflow.
Z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).
The problem with z-index is that very few people understand how it really works. It’s not complicated, but it if you’ve never taken the time to read its specification, there are almost certainly crucial aspects that you’re completely unaware of
Learn more here.