Most Common CSS Mistakes that Web Developers Make
Last Updated : 30 May, 2023
Cascading Style Sheets (CSS) is a style sheet language used to apply styles on HTML tags. Most people think that CSS is easy to learn but the fact is that CSS is really straightforward to learn but it is difficult to master. It requires a lot of practice to master CSS. There are some common mistakes that every web developer makes while using CSS. It is not a big deal to make mistakes but small mistakes may become a headache for you when you are working on a large-scale project. So, you should know your mistakes and you should learn from them. In this article, we will know about the 5 most common CSS mistakes that web developers make.
1. Not using consistent naming: It is a good practice to use a single naming convention in your project because if you are working in a team then it is important to maintain consistency otherwise everything will get messed up. We should also use meaningful names for class and id so that it becomes easy for us to debug our code and it also makes our code more readable.
Example: In this example, we are using the above-explained method.
HTML
<!DOCTYPE html>
< html >
< head >
< style >
.spiderMan {
color: red;
}
.black-widow {
font-size: 3rem;
}
</ style >
</ head >
< body >
< h2 class = "spiderMan" >
Welcome To GFG
</ h2 >
< p class = "black-widow" >
Top 5 CSS mistakes that
web developer makes
</ p >
</ body >
</ html >
|
In the above example, class names that are being used are “spiderMan” and “black-widow”. This practice makes our code difficult to read and it also looks unprofessional if we are using inappropriate class and id names. Here, two naming conventions are being used (i.e. camel case and kebab case). It makes our code look inconsistent and ugly. So it is a good practice to use a single naming convention for your project and use the appropriate class and id name.
We can use a single naming convention (in this case we are using camelCase) and appropriate names in the following way:
HTML
<!DOCTYPE html>
< html >
< head >
< style >
.exampleHeading {
color: red;
}
.exampleContent {
font-size: 3rem;
}
</ style >
</ head >
< body >
< h2 class = "exampleHeading" >
Welcome To GFG
</ h2 >
< p class = "exampleContent" >
Top 5 CSS mistakes that
web developer makes
</ p >
</ body >
</ html >
|
Above code looks much better and consistent than the previous one. This practice also saves your time because now, you don’t have to think about fancy variable names like “spiderman” and “blackwidow”.
2. Using inline CSS: Inline CSS is a method to link CSS to HTML. We can use this method to link CSS, there is nothing wrong with it but we should know when we should link CSS using inline and when we should not. As a programmer, we should always follow DRY (Don’t Repeat Yourself) principle. Inline CSS prevents the possibility to reuse our code because in it CSS properties are specific to one element only, so it is a better practice to use CSS classes because they are meant for reuse.
Example: In this example, we are using inline CSS property.
HTML
<!DOCTYPE html>
< html >
< head >
< title >INLINE EXAMPLE</ title >
</ head >
< body >
< h2 style = "color:blue;font-size:3rem;" >RAY</ h2 >
< h2 style = "color:blue;font-size:3rem;" >SHYAM</ h2 >
< h2 style = "color:blue;font-size:3rem;" >RIDHIMA</ h2 >
< h2 style = "color:blue;font-size:3rem;" >MAYANK</ h2 >
< h2 style = "color:blue;font-size:3rem;" >ARJUN</ h2 >
</ body >
</ html >
|
In the above example, we have five h2 elements and all of them have the same style properties and we are using inline CSS here which violates the DRY principle. So instead of inline CSS, we can define a class that will make save you time and will save you from the mess that inline CSS may create.
We can rewrite the above code in a better way in the following manner:
HTML
<!DOCTYPE html>
< html >
< head >
< title >INLINE EXAMPLE</ title >
< style >
.example {
color: blue;
font-size: 3rem;
}
</ style >
</ head >
< body >
< h2 class = "example" >RAY</ h2 >
< h2 class = "example" >SHYAM</ h2 >
< h2 class = "example" >RIDHIMA</ h2 >
< h2 class = "example" >MAYANK</ h2 >
< h2 class = "example" >ARJUN</ h2 >
</ body >
</ html >
|
It is recommended to use internal or external CSS instead of inline CSS because it may cause a huge mess in your code and it is also difficult to update inline CSS.
3. Using absolute units instead of relative units: It is fine to use absolute units in certain cases but if you are only using absolute units then it’s a huge mistake. In today’s world, we want our websites to be responsive so that we can use them on different devices, so it is necessary for elements to get scale relative to window size. Absolute units don’t scale when the window size changes so they are less favorable for our responsive websites. So it is highly recommended to use relative units instead of absolute units. Some of the relative’s units are vh, %, em, rem, etc.
Example:
h1 { font-size: 16px; line-height: 20px; margin-bottom: 8px; }
In the above code, absolute units are being used which are less favorable for our responsive websites, so instead of absolute units we can use relative units in the following manner:-
h1 { font-size: 1rem; line-height: 1.25; margin-bottom: 0.5em; }
4. Not putting comments in code: As a developer, we have heard many times that it is a good practice to put comments in your code. Comments can save you from hours of debugging. If you are working in a team or if you are working on some large scale, it is necessary to put comments in your code so that it becomes easy for your teammates to understand your CSS quickly and it will also become easy for you to debug. Commenting on your CSS code is necessary because even a single HTML page can have lots of CSS linked with it so it is highly recommended to use comments. Note that you should not use long and inappropriate comments, comments should be short and appropriate.
5. Using only a single style sheet: It’s ok to use a single style sheet if you are working on some small project. But if you are working on some big-scale project, then it is highly recommended to split style sheets into different ones because it will become easy to maintain and will provide better modularity. We can have different CSS files for different fixes.
Example:

In the above, example there are two style sheets. One is for the dark theme and one is for the light theme.
Similar Reads
6 Most Common Bootstrap Mistakes to Avoid in Web Development
.bg-primary .bg-danger breadcrumb alert-success dropdown data-toggle="collapse" col-md-4 Aren't all the above words familiar to you?? If yes....then surely you're using bootstrap (or you might have used) in your application and this article is worth reading (let's go ahead...) If we ask...how to sta
9 min read
CSS Viewer Chrome Extension for Web Developers
CSS viewer inspect and displays the CSS properties of a web page. It is a very smart and useful extension that identifies CSS properties anywhere one points his mouse. It is time-saving and very fast, and also it makes identifying CSS easy. It allows one to click on any image, button, text, etc on a
4 min read
Websites and Software that help HTML, CSS And JavaScript Developers
Developing a website using HTML, CSS, and JS is a time consuming and lengthy process. But using proper resources for the work can accelerate the process. These tools not only make things easier, but you also step it up on the quality level. Here is a list of websites and software that will help you
3 min read
CSS Tricks That Every Web Developer Should Know
CSS (Cascading Style Sheets) is the magic ingredient that breathes life into the raw structure of HTML, transforming it into visually captivating and user-friendly interfaces. While the core concepts of CSS are relatively easy to grasp, mastering its nuances takes dedication and practice. But the re
7 min read
5 Amazing CSS Styles that Every Developer Should Know
CSS (Cascading Style sheets) helps the developer to enhance the visual appearance of web pages, In other words, we can say that CSS adds life to web pages and beautifies it. Using CSS, we can change the layout, color, fonts, images, and the best part is that for a particular style or layout we can w
5 min read
Top 7 Web Development Myths That You Must Know
In this ever-evolving technological world, e-commerce, websites, networking, and online apps have had a strong effect. Considering all these services under one head, we call it Web development. Apart from creating a dynamic website, it has other features too. For example, web engineering, network co
6 min read
7 CSS Hacks Every Developer Should Know
When it comes to web development, the user interface (UI) plays a crucial role. A well-designed layout not only attracts users but also sets the tone for their overall experience. While JavaScript often takes the spotlight, CSS remains a fundamental language, powering 96% of all websites. As a devel
6 min read
12 Web Development Checklists Every Team Should Keep Handy
A website when build should be attractive, meet the goals of businesses, and be easy to handle. There are various factors that help in keeping it successful. A comprehensive web development checklist can help in delivering a better final product. A checklist is a list that keeps track of items requi
6 min read
Top Web Developer Interview Questions and Answers(2024)
Web development is a rapidly growing field with a plethora of opportunities. To succeed, aspiring front-end and back-end developers must be proficient in a variety of skills and languages, particularly JavaScript. JavaScript is the most popular lightweight scripting and compiled programming language
15+ min read
Most Commonly Used Tags in HTML
HTML tags are the fundamental building blocks of web development, providing the structure and organization necessary for creating web pages. They include tags for headings, paragraphs, links, images, and more.Commonly used tags like <html>, <head>, and <body> are essential for crea
4 min read