- HTML
- CSS
- Always Use Semicolons
- Box Model
- Don't Change Element Flow
- Use Modern Layout Techniques
- Simple and Specific Selectors
- Avoid !important
- Don't Override Styles Unnecessarily
- Inherit Styles When Possible
- Keep Code Short
- Use Readable Units
- Use Hexadecimal Colors
- Create Simple Shapes with CSS
- Avoid Hacks
- CSS Variables
- CSS Grid Layout
- Flexbox Tips
- Browser Compatibility
- Debugging Tools
- Accessibility Enhancements
- SEO Best Practices
Semantic elements help structure your content better.
<!-- bad -->
<div id="main">
<div class="article">
<div class="header">
<h1>Blog post</h1>
<p>Published: <span>21st Feb, 2015</span></p>
</div>
<p>…</p>
</div>
</div>
<!-- good -->
<main>
<article>
<header>
<h1>Blog post</h1>
<p>Published: <time datetime="2015-02-21">21st Feb, 2015</time></p>
</header>
<p>…</p>
</article>
</main>
Use proper HTML5 structure to make your code cleaner and easier to read.
<!-- bad -->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
<!-- good -->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
HTML5 allows for shorter and cleaner code.
<!-- bad -->
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Contact</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<h1>Contact me</h1>
<label>
Email address:
<input type="email" placeholder="[email protected]" required="required">
</label>
<script src="main.js" type="text/javascript"></script>
</body>
</html>
<!-- good -->
<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title>Contact</title>
<link rel="stylesheet" href="style.css">
<body>
<h1>Contact me</h1>
<label>
Email address:
<input type="email" placeholder="[email protected]" required>
</label>
<script src="main.js"></script>
</body>
</html>
Improve accessibility with simple steps like proper alt
attributes and clear labels.
<!-- bad -->
<h1><img src="logo.png" alt=""></h1>
<!-- good -->
<h1><img src="logo.png" alt="Company"></h1>
Always define the language and use UTF-8 encoding.
<!-- bad -->
<!doctype html>
<title>Hello, world.</title>
<!-- good -->
<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title>Hello, world.</title>
</html>
Load scripts after the content to improve page load times.
<!-- bad -->
<!doctype html>
<meta charset="UTF-8">
<script src="analytics.js"></script>
<title>Hello, world.</title>
<p>...</p>
<!-- good -->
<!doctype html>
<meta charset="UTF-8">
<title>Hello, world.</title>
<p>...</p>
<script src="analytics.js"></script>
Even though semicolons are separators, always use them.
/* bad */
div {
color: red
}
/* good */
div {
color: red;
}
Use the same box model for the entire document.
/* bad */
div {
width: 100%;
padding: 10px;
box-sizing: border-box;
}
/* good */
* {
box-sizing: border-box;
}
Avoid changing the default flow of elements.
/* bad */
img {
display: block;
}
/* good */
img {
vertical-align: middle;
}
Prefer Flexbox and Grid over older layout methods.
/* bad */
div {
width: 100px;
position: absolute;
right: 0;
}
/* good */
.container {
display: flex;
justify-content: flex-end;
}
Keep selectors simple and specific.
/* bad */
div:first-of-type :last-child > p ~ *
/* good */
div:first-of-type .info
Avoid using !important
to make styles easier to override.
/* bad */
.bar {
color: green !important;
}
.foo {
color: red;
}
/* good */
.foo.bar {
color: green;
}
.foo {
color: red;
}
Minimize style overrides to keep code clean.
/* bad */
li {
visibility: hidden;
}
li:first-child {
visibility: visible;
}
/* good */
li + li {
visibility: hidden;
}
Use inheritance to avoid repetitive styles.
/* bad */
div h1, div p {
text-shadow: 0 1px 0 #fff;
}
/* good */
div {
text-shadow: 0 1px 0 #fff;
}
Use shorthand properties and concise code.
/* bad */
div {
transition: all 1s;
top: 50%;
margin-top: -10px;
padding-top: 5px;
padding-right: 10px;
padding-bottom: 20px;
padding-left: 10px;
}
/* good */
div {
transition: 1s;
top: calc(50% - 10px);
padding: 5px 10px 20px;
}
Prefer rem
and unitless values when possible.
/* bad */
div {
margin: 0px;
font-size: .9em;
line-height: 22px;
transition: 500ms;
}
/* good */
div {
margin: 0;
font-size: .9rem;
line-height: 1.5;
transition: .5s;
}
Use hex colors for simplicity unless you need transparency.
/*
bad */
div {
color: hsl(103, 54%, 43%);
}
/* good */
div {
color: #5a3;
}
Avoid additional HTTP requests by creating shapes with CSS.
/* bad */
div::before {
content: url(white-circle.svg);
}
/* good */
div::before {
content: "";
display: block;
width: 20px;
height: 20px;
border-radius: 50%;
background: #fff;
}
Use clean and standard solutions instead of hacks.
/* bad */
div {
// position: relative;
transform: translateZ(0);
}
/* good */
div {
/* position: relative; */
will-change: transform;
}
CSS variables make it easier to maintain and update your styles.
:root {
--main-bg-color: #fff;
--main-text-color: #333;
}
body {
background-color: var(--main-bg-color);
color: var(--main-text-color);
}
Use CSS Grid for complex layouts.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.item {
background-color: #ccc;
padding: 20px;
}
Flexbox is great for 1D layouts.
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
.item {
padding: 10px;
background-color: #ccc;
}
Use tools like Autoprefixer to ensure compatibility across different browsers.
/* bad */
.box {
display: flex;
}
/* good */
.box {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
Use browser developer tools to debug and test your HTML and CSS.
- Chrome DevTools
- Firefox Developer Tools
- Safari Web Inspector
Enhance accessibility by using ARIA roles and ensuring color contrast.
<!-- bad -->
<button>Click</button>
<!-- good -->
<button aria-label="Close">X</button>
Improve SEO with proper HTML structure and meta tags.
<!-- bad -->
<title>Page</title>
<!-- good -->
<title>My Awesome Page</title>
<meta name="description" content="This is a description of my awesome page.">