How to Lose CSS and Alienate Styles

A Guide to CSS Best Practices

This article aims to address some best practices when styling web pages and other practices to avoid. Let’s start with some Best Practices when it comes to styling your websites with Cascading Style Sheets (CSS) — the first being that you should avoid inline styling at all costs. Let’s take a look at an example of the right and wrong way to align an HTML element.

Bad

//HTML With Inline Styling
<p style="text-align: center">This is a center aligned paragraph.</p>

This is bad because it will lead to a mess of inline styles embedded all over your HTML markup. If you need to apply the styles of one page to another, you’ll be spending a few hours inspecting your code to gather all the right CSS. Further, inline styling makes responsive design a nightmare. Just don’t.

Good

HTML Page

<p class="centered-text">This is a center aligned paragraph.</p>

CSS Stylesheet

.centered-text {
text-align: center;
}

This is miles better because it achieves the goal of separating your style and your HTML. Aside from a reference to your stylesheet in your HTML page, there should be no CSS. In the above example, we added a class of “centered-text” to the paragraph which allowed us to align the paragraph in our stylesheet. This allows us to take advantage of one of the inherent benefits of CSS: reusable selectors.

Organizing your Stylesheet

Ok so let’s assume you’ve replaced all your inline CSS with classes and ID’s and moved your styles over to a stylesheet that targets your HTML classes and IDs. Great. Let’s take that one step further.

Bad

.news-title {
font-size: 28px;
font-size: 2.8rem;
line-height: 1.2;
color: #1e1e1f;
font-family: 'nimbus-sans-condensed', sans-serif;
font-weight: bold;
}
.home-title {
font-size: 6.7rem;
line-height: 1;
color: #fff;
font-family: 'Open Sans Condensed', sans-serif;
font-weight: 700;
padding-top: 50px;
}
.news-subtitle {
font-size: 14px;
}
.blog img {
display: block;
max-width: 100%;
height: auto;
}
.contact-header {
font-family: 'Open Sans Condensed', sans-serif;
font-weight: bold;
text-transform: inherit;
font-size: 81px;
font-size: 8.1rem;
line-height: 65.85px;
letter-spacing: 0px;
text-transform: uppercase;
}
.home-subhead {
font-size: 2.3rem;
line-height: 1.1;
margin: 30px 0 40px;
color: #fff;
}
.news-content {
line-height: 1.5 !important;
padding: 20px 0 10px;
margin-top: 20px;
}
.blog-content {
font-size: 17px;
color: #fff;
margin-bottom: 40px;
}

Generally speaking, we’ve all been in the situation where we add elements to our stylesheet haphazardly. So why is this bad? Several reasons. First off, you should always organize your stylesheets by section; if your website has a homepage, a news page, and a blog like our example, your stylesheet should reflect that organization. In the CSS above, all of the styles are jumbled together in no particular order. Within the code itself, there are redundant and inconsistent property values.

Good

/* Homepage Styles */
.home-title {
font-size: 2rem;
line-height: 1.5;
color: #fff;
font-family: 'Open Sans Condensed', Helvetica, Arial, sans-serif;
font-weight: 700;
padding-top: 50px;
}
.home-subhead {
font-size: 1.5rem;
margin: 30px 0 40px;
color: #fff;
}
/* End of Homepage Styles */
/* Newsroom Styles */
.news-title {
font-size: 2rem;
line-height: 1.5;
color: #1e1e1f;
font-family: 'nimbus-sans-condensed', Helvetica, Arial, sans-serif;
font-weight: 700;
}
.news-subtitle {
font-size: 1.5rem;
}
.news-content {
font-size: 1rem;
line-height: 1.2;
padding: 20px 0 10px;
margin-top: 20px;
}
/* End of Newsroom Styles */
/* Blog Styles */
.blog img {
display: block;
max-width: 100%;
height: auto;
}
.blog-content {
font-size: 1rem;
color: #fff;
margin-bottom: 40px;
}
/* End of Blog Styles */

Notice how we took the same CSS from the first example and organized it so it is broken into sections and has opening and closing comments? The selectors within a section even follow the linear order as they appear on the page. Aside from better organization, we also standardized the ordering of the properties. This will help you find specific properties within a given selector.

We also removed redundant properties, added fallback values for the font-family properties, and added some indentation to make the code readable. While many of these changes are small in scale, they make a significant difference when projected over the course of thousands of lines.

Organizing your Stylesheets

Although it is a Best Practice to maintain one stylesheet for your website there are occasions where there are so many lines of CSS that it may be ideal to break them up. This section will discuss best practices for organizing your CSS stylesheets when your website has more than one.

Bad

Why is this organization bad? Because there are so many different stylesheets with no indication of what the difference is between them. So if you’re looking for a particular style, you’ll need to go through all of these stylesheets or spend a lot of time inspecting in your browser. Save yourself and your fellow developers a lot of time and never do this.

Good

Unlike the “Bad” example, these stylesheets are named by what styles they contain. Looking for the styles for the blog page, or maybe the news section, or maybe even the team page? No problem. This designer was smart enough to name them all accordingly, making your life 1,000,000 times easier.

In Part I of our Series on CSS Best Practices, we covered the horror that is inline styles and how to use selectors like classes and ID’s in your HTML markup to target your web pages with CSS. We also covered organizing your stylesheets to mirror the organization of your website and also went over best practices when managing multiple stylesheets.

Over the course of a project, CSS tends to grow. Start with these core best practices and life as a web designer will be shockingly manageable. See you next time!

Get daily web design tutorials in your inbox by subscribing to the Solodev Blog at solodev.com/blog/!
Brought to you by the Solodev Team. Solodev is a cloud-based web content management system that empowers users with the freedom to bring amazing web designs to life.