How to write CSS [ Tips and Tricks ]

Editor's note (2026): We've corrected and updated this 2014 post. The original claimed linked CSS files are hidden from visitors (they aren't; anyone can view them) and that whitespace noticeably slows pages (minifying for production solves that). Code examples have been cleaned up and modernized.

Every website, from a gaming site to a government portal, relies on CSS. Well-written CSS is easier to maintain and can help your pages load faster. Here are some tips for beginners.

Format CSS for readability

Write one declaration per line so your stylesheet is easy to read and edit:

.card {
  border: 1px solid #ccc;
  background: #fff;
  color: #222;
}

Readable source code is for you and your team. For production, run your CSS through a minifier (most build tools do this automatically) to strip whitespace and comments so files download faster.

Comment by section

Comments help you find your way around a stylesheet. Use them to label sections, not every line:

/* Layout */
.wrapper {
  max-width: 1024px;
  margin: 0 auto;
}

/* Navigation */
.nav {
  display: flex;
  gap: 1rem;
}

.nav a {
  text-decoration: none;
}

/* Main content */
.main-content {
  background: #fff;
  color: #000;
}

Also give classes clear, descriptive names.

Group selectors that share styles

Instead of repeating the same rule:

#nav a { text-decoration: none; }
#sponsors a { text-decoration: none; }
#footer a { text-decoration: none; }

Combine them with commas:

#nav a,
#sponsors a,
#footer a {
  text-decoration: none;
}

Plan for mobile with flexible widths

Rather than fixed pixel widths, use flexible layouts that adapt to the screen. A common pattern:

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

Combine this with media queries, Flexbox, and CSS Grid so your site looks good on phones, tablets, and desktops, and when visitors zoom in.

Use external stylesheets

Put your CSS in a separate file and link it in your page's <head>:

<link rel="stylesheet" href="styles.css">

This keeps HTML clean, lets browsers cache the CSS across pages, and makes site-wide design changes easy. (Note: CSS files are still public; this is about organization and performance, not hiding your design.)

Style common elements globally

Set base styles for elements such as headings, paragraphs, links, lists, and horizontal rules (<hr>) so your whole site looks consistent.

Normalize browser defaults

Browsers apply their own default styles. Rather than a heavy reset, many developers now use a modern normalize or minimal reset stylesheet, plus:

*, *::before, *::after {
  box-sizing: border-box;
}

The classic approach is Eric Meyer's reset, which is still a useful reference.


Need help with your website's design or front-end code? I.T. For Less can help — contact us.

Development