Mistakes to avoid while writing CSS

When diving into web development, CSS often seems like the easiest part. After all, it’s just styling, right? But if you’re not careful, things may easily get muddy as you advance and strive for a more professional approach. Despite its resemblance to basic English, writing CSS efficiently requires attention to detail.

Here are some comman mistakes you should avoid:

1. Overusing !important

h1 {
  margin: 0 !important;
}

Using !important, one style rule take precedence over another. However, its indiscriminate use can lead to complications in code maintenance and debugging. This is because !important overrides all previous styles applied to an element within the cascade, potentially obscuring the intended style hierarchy.

2. Not using shorthand property

In web development, removing redundant code and optimizing it are essential since they improve productivity and maintainability. Let us see an example where we have to provide the margins for a <h1> element

h1 { 
  margin-top: 20px; 
  margin-right: 10px; 
  margin-bottom: 20px; 
  margin-left 10px; 
}

You must be thinking that something is off. We are repeating ourselves several times. However, there is clear and concise way of writing this using CSS shorthand properties.

h1 { 
  margin: 50px 0; 
}

We achieve the same outcome while reducing the code length by three lines. This not only enhances the readability of the code but also streamlines maintenance tasks.

3. Excessive Reliance on ID Selectors

#heading {
  color: #f9f9f9;
}

#subHeading {
  color: #F0F0FF,
}

IDs in CSS serve as unique identifiers for an element. Relying heavily on IDs will impact the flexibility of styling and introduce complications, particularly in large, complex applications. This approach also makes overriding styling more difficult. Instead, favoring class selectors provides greater ease and flexibility in CSS usage.

4. More inline styling

Excessive use of inline styling can clutter code and pose maintenance challenges. Consider the following example:

html
<h1 style={ font-size: “12px”, margin-left: “10px”>Hello World! </h1>

Inline styling hampers scalability and reusability, as each element’s design is defined individually. It also complicates debugging. It’s preferable to employ centralized styling or internal stylesheets for better organization and easier maintenance.

5. Neglecting Responsive Design

.container {
  width: 600px;
}

Utilizing fixed units like “px” renders your application non-responsive. It’s advisable to opt for responsive units such as “%”, viewpoint units (“vw”, “vh”) to ensure adaptability across various devices. This approach enhances the appearance of your application on multiple screen sizes and mitigates numerous CSS-related issues.

6. Consolidating all Styles into One Stylesheet

This topic often hinges on personal preference among developers. For me, dividing CSS into separate files proves advantageous because it provides clear organization, making it easier to locate and modify specific code. However, if managing styles becomes cumbersome, consider importing all stylesheets into a single file using the @import rule.

@import url("button.css"); @import url("typography.css");

Although it is fine to have all styles in one file, maintainability can be substantially improved by breaking them up into smaller files.

Did you find this article valuable?

Support Parul Verma by becoming a sponsor. Any amount is appreciated!