| name | css |
| description | Apply Google CSS style guide conventions to CSS code |
CSS Style Guide
This skill applies Google's CSS style guide conventions to ensure clean, maintainable, and efficient CSS code.
Core Principles
Valid CSS
Use valid CSS code tested with W3C CSS Validator:
- Catches errors early
- Ensures browser compatibility
- Improves maintainability
- Exception: vendor-specific prefixes and required proprietary syntax
Meaningful Class Names
Use functional or generic class names, not presentational:
.button-green {}
.clear {}
.yee-1901 {}
.gallery {}
.login {}
.video {}
.aux {}
.alt {}
Avoid ID Selectors
IDs have high specificity and reduce reusability:
#example {}
#navigation {}
.example {}
.navigation {}
CSS Style Rules
Class Naming Conventions
Use short but meaningful names:
.navigation {}
.atr {}
.nav {}
.author {}
Use hyphens as delimiters:
.demoimage {}
.error_status {}
.video-id {}
.ads-sample {}
Use prefixes for large projects (optional):
.adw-help {}
.maia-note {}
Avoid Type Selectors
Don't qualify class names with type selectors:
ul.example {}
div.error {}
.example {}
.error {}
Reason: Performance and flexibility
Shorthand Properties
Use shorthand when possible:
border-top-style: none;
font-family: palatino, georgia, serif;
font-size: 100%;
line-height: 1.6;
padding-bottom: 2em;
padding-left: 1em;
padding-right: 1em;
padding-top: 0;
border-top: 0;
font: 100%/1.6 palatino, georgia, serif;
padding: 0 1em 2em;
Units
Omit units after 0 values:
margin: 0px;
padding: 0em;
margin: 0;
padding: 0;
flex: 0px;
flex: 1 1 0px;
Include leading 0s:
font-size: .8em;
font-size: 0.8em;
Color Values
Use 3-character hex when possible:
color: #eebbcc;
color: #ebc;
Use lowercase:
color: #E5E5E5;
color: #e5e5e5;
Important Declarations
Avoid !important - use specificity instead:
.example {
font-weight: bold !important;
}
.example {
font-weight: bold;
}
.container .example {
font-weight: bold;
}
Browser Hacks
Avoid CSS hacks and user agent detection - use progressive enhancement:
.example {
width: 100px\9;
}
@supports (display: grid) {
.example {
display: grid;
}
}
Formatting Rules
Indentation
Indent by 2 spaces (no tabs):
.example {
color: blue;
}
@media screen, projection {
html {
background: #fff;
color: #444;
}
}
Declaration Order
Alphabetize declarations for consistency (optional):
background: fuchsia;
border: 1px solid;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
color: black;
text-align: center;
text-indent: 2em;
Note: Ignore vendor prefixes for sorting, but keep them grouped
Semicolons
Always end declarations with semicolons:
.test {
display: block;
height: 100px
}
.test {
display: block;
height: 100px;
}
Property-Value Spacing
Single space after colon, no space before:
h3 {
font-weight:bold;
}
h3 {
font-weight: bold;
}
Declaration Block Spacing
Single space before opening brace, same line:
.video{
margin-top: 1em;
}
.video
{
margin-top: 1em;
}
.video {
margin-top: 1em;
}
Selector and Declaration Separation
New line for each selector and declaration:
a:focus, a:active {
position: relative; top: 1px;
}
h1,
h2,
h3 {
font-weight: normal;
line-height: 1.2;
}
Rule Separation
Blank line between rules:
html {
background: #fff;
}
body {
margin: auto;
width: 50%;
}
Quotation Marks
Use single quotes for attribute selectors and property values:
@import url("https://www.google.com/css/maia.css");
html {
font-family: "open sans", arial, sans-serif;
}
@import url(https://www.google.com/css/maia.css);
html {
font-family: 'open sans', arial, sans-serif;
}
@charset "utf-8";
Do not quote URLs:
background: url(images/bg.png);
Organizing CSS
Section Comments
Group related rules with comments (optional):
.adw-header {}
.adw-header-logo {}
.adw-footer {}
.adw-gallery {}
.adw-gallery-item {}
File Organization
Organize CSS files logically:
html,
body {
margin: 0;
padding: 0;
}
h1, h2, h3 {
font-family: 'Arial', sans-serif;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.button {
padding: 10px 20px;
}
.hidden {
display: none;
}
Best Practices
Specificity Management
Keep specificity low for easier overrides:
html body div.container ul.nav li.item a.link {}
.nav-link {}
Avoid Over-nesting
.header .nav .menu .item .link {
color: blue;
}
.nav-link {
color: blue;
}
Mobile-First Media Queries
.container {
width: 100%;
}
@media (min-width: 768px) {
.container {
width: 750px;
}
}
@media (min-width: 1024px) {
.container {
width: 960px;
}
}
Reusable Classes
Create utility classes for common patterns:
.flex {
display: flex;
}
.flex-center {
display: flex;
justify-content: center;
align-items: center;
}
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
Quick Reference
| Rule | Convention |
|---|
| Indentation | 2 spaces |
| Case | Lowercase |
| Quotes | Single (') except @charset |
| Semicolons | Required after every declaration |
| Units | Omit after 0 |
| Leading zeros | Always include (0.8em) |
| Hex colors | 3-char when possible, lowercase |
| ID selectors | Avoid |
| Type selectors | Don't qualify classes |
!important | Avoid |
| Property order | Alphabetical (optional) |
| Line breaks | New line per selector/declaration |
| Rule separation | Blank line between rules |
| Comments | Section comments for organization |
Common Patterns
Button Component
.button {
background-color: #007bff;
border: 0;
border-radius: 4px;
color: #fff;
cursor: pointer;
display: inline-block;
font-size: 1rem;
padding: 0.5rem 1rem;
text-align: center;
text-decoration: none;
}
.button:hover {
background-color: #0056b3;
}
.button-secondary {
background-color: #6c757d;
}
.button-large {
font-size: 1.25rem;
padding: 0.75rem 1.5rem;
}
Card Component
.card {
background: #fff;
border: 1px solid #ddd;
border-radius: 8px;
padding: 1.5rem;
}
.card-header {
border-bottom: 1px solid #ddd;
margin-bottom: 1rem;
padding-bottom: 0.5rem;
}
.card-title {
font-size: 1.5rem;
margin: 0;
}
.card-body {
line-height: 1.6;
}
Grid System
.grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(12, 1fr);
}
.col-4 {
grid-column: span 4;
}
.col-6 {
grid-column: span 6;
}
.col-12 {
grid-column: span 12;
}
Additional Resources
Summary
Write CSS that is:
- Valid: Passes W3C validation
- Semantic: Meaningful class names
- Maintainable: Low specificity, avoid IDs
- Consistent: Follow formatting rules
- Efficient: Use shorthand, avoid repetition
- Readable: Proper spacing and organization
- Lowercase: All selectors and properties
- Quoted: Single quotes (except
@charset)