-
Notifications
You must be signed in to change notification settings - Fork 25
CSS Styleguide
Ryan Johnson edited this page Oct 25, 2019
·
15 revisions
!! IN PROGRESS !!
- Spaces, not tabs
- Two (2) spaces per indent
- Unix line breaks (
\n
) - Max line length:
120
- Always double-quote strings
- Anything that can be quoted should be quoted
- Always write case-insensitive syntax in lowercase
- selectors, declarations, keywords, hex colors, units, etc.
- No inline blocks
- Open block on same line as the last selector
- Close block on line after last declaration
- One (1) declaration/rule per line
- Never on the same line as a selector
- One (1) selector per line
- Exclude unit for
0
values - Always prefix helper classes with
hx
usinghxLowerCamelCase
notation - One (1) space after colon (no space before)
- One (1) space after comma (no space before)
- No trailing space at end of lines
- Always include final newline at end of source
- Never begin source files with a blank line
- Alphabetize declarations within each block
- Avoid inlining multiple selectors in a selector list
- Arrange
@import
statements before all other content in generated CSS - Use shorthand properties with caution
- no space before colon
- one (1) space after colon
Example:
/* PASS */
@media (min-width: 900px) { }
:root {
--hxCustom-prop1: foobar;
font-size: 16px;
}
/* FAIL */
@media (min-width:900px) { }
@media (min-width :900px) { }
@media (min-width : 900px) { }
:root {
--hxCustom-prop1:foobar;
--hxCustom-prop2 :foobar;
--hxCustom-prop3 : foobar;
font-size:16px;
font-size :16px;
font-size : 16px;
}
- one (1) space before operator
- one (1) space after operator
- Attribute selector operators should never have space around them
Example:
/* PASS */
@media (width >= 600px) { }
input[type="number"] { }
/* FAIL */
@media (width>=600px) { }
@media (width >=600px) { }
@media (width>= 600px) { }
input[type= "number"] { }
input[type ="number"] { }
input[type = "number"] { }
- no space before comma
- one (1) space after comma
Example:
/* PASS */
.foo {
background-image: linear-gradient(90deg, transparent, #ffffff);
}
/* FAIL */
.foo {
background-image: linear-gradient(90deg,transparent,#ffffff);
background-image: linear-gradient(90deg ,transparent ,#ffffff);
background-image: linear-gradient(90deg , transparent , #ffffff);
}
- All quoted values should use double-quotes
- Anything that can be quoted should be quoted
- e.g., url() paths, attribute selector values, non-keyword font families, etc.
- Strings should never be multi-line
Example:
/* PASS */
@import url("https://example.com/styles.css");
input[type="number"] {
font-family: "Helvetica", sans-serif;
}
.foobar::before {
content: "Foobar: ";
}
/* FAIL - no quotes */
@import url(https://example.com/styles.css);
input[type=number] {
font-family: Helvetica, sans-serif;
}
.foobar {
grid-template-areas: foo bar;
}
.foobar::before {
content: Foobar: ;
}
/* FAIL - wrong quotes */
@import url('https://example.com/styles.css');
input[type='number'] {
font-family: 'Helvetica', sans-serif;
}
.foobar {
grid-template-areas: 'foo' 'bar';
}
.foobar::before {
content: 'Foobar: ';
}
/* FAIL - multi-line string */
.foobar::before {
content: "Foo\nBar:";
}
- do not use
!important
within keyframes- restricts reuse
- not supported in all browsers
- can result in unexpected behavior
- prefix keyframe names with
hx-
- avoids conflicts with 3rd-party styles
Example:
/* PASS */
@keyframes hx-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* FAIL */
@keyframes spin {
0% {
transform: rotate(0deg) !important;
}
100% {
transform: rotate(360deg) !important;
}
}
- do not use
max-width
in media queries- encourages mobile-first development
- do not use vendor-prefixed features
- prevents incompatibilities from browser updates
(a.k.a. CSS Variables) Not all browsers support custom properties, so you'll need to make sure you have a fallback property for legacy browsers.
IE | Edge | Chrome | Safari | Firefox | Opera |
---|---|---|---|---|---|
n/a | 15+ | 49+ | 9.1+ | 31+ | 36+ |
.hxFoobar {
/* Fallback Styles (legacy browsers) */
background-color: gray;
/* Modern Styles (modern browsers) */
/* overrides above, legacy browsers ignore it */
background-color: var(--background-color, gray);
}
Alternatively, you could start with the fallback and use a @supports
query to apply CSS Custom Properties.
(The custom property and value used in the query just needs to be valid CSS and doesn't need to match the exact custom property and value.)
/* Fallback Styles */
.hxFoobar {
background-color: gray;
}
/* Modern Styles */
@supports (--modern: true) {
.hxFoobar {
background-color: var(--background-color, gray);
}
}
- Exclude leading zeros
- Exclude trailing zeros
- Maximum precision in the thousandths
- Pass:
1.125em
- Fail:
1.1245em
- Pass:
The following units are blacklisted and shouldn't be used.
cm
ex
in
mm
pc
pt
The following guidelines are not required but are highly recommended.
Although CSS shorthand properties are useful to reduce the size of compiled assets, related properties may get defaulted to a value that is undesirable.
- Shorthands have long effects
- Double-check expanded properties being set by CSS shorthand
-
background: lime;
isn't the same asbackground-color: lime;
-