CSS Best Practices
Good CSS is more than a collection of styles that make a webpage look right. Well-written CSS should also be organized, readable, reusable, and easy to maintain as a website grows.
Developing consistent coding habits from the beginning can make stylesheets easier to understand, troubleshoot, and update. The following practices provide a useful foundation for working with CSS.
Organize Your CSS
Organize related CSS rules so they are easy to locate. A stylesheet might group rules for typography, navigation, forms, layout, and other parts of the website into logical sections.
For larger projects, styles can also be divided among multiple stylesheets when doing so makes the project easier to manage.
/* Typography */
h1 {
font-size: 2rem;
}
p {
line-height: 1.5;
}
/* Navigation */
.main-nav {
display: flex;
gap: 1rem;
}
A predictable organization makes it easier to find and update styles later.
Use Consistent Formatting
Choose a formatting style and use it consistently throughout your stylesheets. Consistent indentation, spacing, and declaration formatting make CSS easier to scan and understand.
.card {
margin: 20px;
padding: 20px;
border: 1px solid #cccccc;
}
Consistency is more important than choosing one particular formatting convention. A project should follow the same convention throughout its CSS.
Keep Selectors Simple
Use selectors that clearly identify the elements you want to style without making them unnecessarily specific. Simple selectors are generally easier to reuse and override.
.notice {
padding: 15px;
}
Avoid adding unnecessary elements or levels to a selector when a simpler selector identifies the same target.
main article section div.notice {
padding: 15px;
}
The first selector is easier to reuse and is less dependent on the exact structure of the HTML document.
Avoid Unnecessary Repetition
When multiple selectors need the same declarations, they can often be grouped instead of repeating the same CSS.
h1, h2, h3 {
font-family: Arial, Helvetica, sans-serif;
}
Reusable classes can also reduce repetition when the same visual treatment is needed in several places.
.highlight {
background-color: #ffffcc;
padding: 10px;
}
Reducing unnecessary duplication makes future changes easier because a shared style can often be updated in one place.
Use Comments When Helpful
Comments can identify sections of a stylesheet or explain CSS that may not be immediately clear. They should provide useful information rather than simply repeat what the code already says.
/* Main navigation */
.main-nav {
display: flex;
gap: 1rem;
}
Too many unnecessary comments can make a stylesheet harder to scan, so use them where they improve understanding or organization.
Separate Content and Presentation
Whenever practical, keep reusable presentation rules in stylesheets instead of placing them directly in HTML with the style attribute.
<p class="intro">Welcome to the CSS tutorial.</p>
.intro {
color: #333333;
font-size: 18px;
}
This approach keeps the HTML focused on document structure and content while the stylesheet controls presentation. It also allows the same CSS rule to be reused by other elements.
Write CSS for Maintainability
CSS should be written with future changes in mind. Use meaningful class names, keep selectors understandable, remove styles that are no longer needed, and avoid adding unnecessary rules simply to fix earlier CSS.
Test changes at different viewport sizes and in the browsers your project supports. As a stylesheet grows, periodically review it for duplicate rules, outdated declarations, and styles that can be simplified.
There is no single organization method that works for every project. The goal is to use a consistent approach that makes the CSS understandable and manageable for the website you are building.
