CSS Errors
Common CSS mistakes and how to fix them
Common CSS Errors
Even experienced developers make CSS mistakes. Here are the most common errors and how to fix them.
π‘ Debugging Tip:
Use your browser's Developer Tools (F12) to inspect and debug CSS issues!
πΉ 1. Syntax Errors
Missing Semicolons
β Wrong:
.button {
background-color: blue /* Missing semicolon */
color: white;
padding: 10px;
}
β Correct:
.button {
background-color: blue; /* Added semicolon */
color: white;
padding: 10px;
}
Missing Curly Braces
β Wrong:
.header
background-color: #333; /* Missing opening brace */
color: white;
}
β Correct:
.header { /* Added opening brace */
background-color: #333;
color: white;
}
Incorrect Property Names
β Wrong:
.text {
colour: red; /* Should be 'color' */
font-weight: bolder; /* Should be 'bold' */
text-allign: center; /* Should be 'text-align' */
}
β Correct:
.text {
color: red;
font-weight: bold;
text-align: center;
}
πΉ 2. Selector Errors
Wrong Class/ID Syntax
β Wrong:
button { /* Targets all buttons */
color: red;
}
.my-class { /* Class doesn't exist in HTML */
font-size: 20px;
}
β Correct:
.button { /* Targets elements with class="button" */
color: red;
}
#my-id { /* Targets element with id="my-id" */
font-size: 20px;
}
Case Sensitivity Issues
HTML:
<div class="MyButton">Click me</div>
β Wrong CSS:
.mybutton { /* Wrong case */
color: blue;
}
β Correct CSS:
.MyButton { /* Matches HTML exactly */
color: blue;
}
πΉ 3. Property Value Errors
Invalid Color Values
β Wrong:
.text {
color: #gggggg; /* Invalid hex */
background: rgb(300, 300, 300); /* Values too high */
}
β Correct:
.text {
color: #666666; /* Valid hex */
background: rgb(200, 200, 200); /* Valid RGB values */
}
Missing Units
β Wrong:
.box {
width: 300; /* Missing unit */
margin: 20; /* Missing unit */
font-size: 16; /* Missing unit */
}
β Correct:
.box {
width: 300px; /* Added px unit */
margin: 20px; /* Added px unit */
font-size: 16px; /* Added px unit */
}
πΉ 4. CSS Specificity Problems
CSS specificity conflicts occur when multiple rules target the same element, causing unexpected style overrides and broken designs. Specificity is calculated based on selector types: inline styles, IDs, classes, and elements. Overusing !important or deeply nested selectors creates maintenance headaches. Strategies like BEM methodology, reducing ID usage, and leveraging CSS cascade order help manage specificity. Tools like specificity calculators aid debugging. For SEO, clean specificity management leads to smaller, more efficient stylesheets, faster load times, and consistent rendering across devices. This improves page experience metricsβa direct ranking factorβand supports maintainable, scalable codebases.
HTML:
<div id="container" class="box">
<p class="text">Hello World</p>
</div>
β Problem:
/* This won't work because ID has higher specificity */
.text {
color: red;
}
#container .text {
color: blue; /* This wins! */
}
β Solutions:
/* Option 1: Use more specific selector */
#container .text {
color: red !important; /* Use !important sparingly */
}
/* Option 2: Match the specificity */
#container .text {
color: red;
}
πΉ 5. File Path Errors
Incorrect CSS Link
β Wrong:
<link rel="stylesheet" href="style.css">
<!-- File is actually in css/styles.css -->
β Correct:
<link rel="stylesheet" href="css/styles.css">
Background Image Paths
β Wrong:
.hero {
background-image: url(hero.jpg); /* File not found */
}
β Correct:
.hero {
background-image: url(../images/hero.jpg); /* Correct path */
}
πΉ Debugging Tips
Use Developer Tools
Press F12 to inspect elements and see which CSS rules apply
Validate Your CSS
Use online CSS validators to check for syntax errors
Test in Multiple Browsers
Different browsers may handle CSS differently
Comment Your Code
Add comments to remember why you wrote certain CSS
π‘ Quick Fixes Checklist
When CSS isn't working, check:
- β Are there any syntax errors (missing semicolons, braces)?
- β Is the CSS file properly linked in HTML?
- β Are class/ID names spelled correctly?
- β Do property values have the correct units?
- β Is another CSS rule overriding your styles?
- β Are file paths correct for images and fonts?
- β Is the browser cache cleared?