Huzi Blogs
Blog
Blog
Disclaimer & Data Privacy Policy
Project by huzi.pk

© 2026 blogs.huzi.pk. All Rights Reserved.

    Back to all posts
    CSS

    5 Common CSS Mistakes and How to Fix Them

    By Huzi

    CSS is a powerful language, but its deceptive simplicity can lead developers down a path of frustration. Writing CSS that is scalable, maintainable, and easy to debug is a skill that takes practice. Many developers, especially those new to the language, fall into the same common traps. This article covers five of the most frequent CSS mistakes and how you can avoid them to write cleaner, more professional code.

    1. Abusing the !important Rule

    This is perhaps the most notorious mistake. You have a style that isn't applying, and after minutes of frustration, you add !important to force it to work. Problem solved, right? Wrong.

    The Problem: !important is a sledgehammer. It breaks the natural cascade and specificity rules of CSS, making your stylesheet incredibly difficult to debug and manage in the long run. When you start using !important to override other !important rules, you've entered a battle you can't win.

    The Fix: Instead of reaching for !important, understand why your style isn't applying. It's almost always a specificity issue.

    • Increase Specificity: Make your selector more specific. For example, div.card p is more specific than just p.
    • Selector Order: If two selectors have the same specificity, the one that appears later in the stylesheet wins.
    • Use Browser DevTools: The "Styles" tab in your browser's developer tools is your best friend. It shows you which styles are being applied and which are being overridden, and by what selectors.

    When is !important okay? Very rarely. The most common acceptable use is in utility classes that must override any other style, for example, a hidden class:

    .hidden {
      display: none !important;
    }
    

    2. Using "Magic Numbers"

    Magic numbers are arbitrary pixel values used for widths, heights, margins, and padding without a clear reason.

    The Problem: Layouts built with magic numbers are brittle. A small change in content, like a longer title or an extra line of text, can break the entire design. They are also terrible for responsive design, as a margin-left: 342px; that looks good on your monitor will be a disaster on a mobile screen.

    The Fix: Use a system and relative units.

    • Use Relative Units: Prefer %, rem, em, vw, and vh over px for layout and typography. rem is excellent for creating scalable UIs.
    • Embrace Flexbox and Grid: Modern layout tools like Flexbox and CSS Grid are designed to create fluid, responsive layouts without magic numbers. Use properties like gap, justify-content, and align-items to manage spacing.
    • Use CSS Custom Properties (Variables): Define a spacing scale for your application. This creates consistency and makes global changes easy.
      :root {
        --spacing-sm: 0.5rem;
        --spacing-md: 1rem;
        --spacing-lg: 2rem;
      }
      
      .card {
        padding: var(--spacing-md);
      }
      

    3. Overly Specific Selectors

    Long, complex selectors are a sign of fragile CSS.

    The Problem: A selector like div#main-content .container > ul.item-list li a.link is extremely hard to override. To change the style of that link, you'd need an even more specific selector. This leads to a specificity war and bloated CSS. It also tightly couples your CSS to your HTML structure; if you change a div to a section, the style breaks.

    The Fix: Keep selectors short and use classes.

    • Prefer Classes: Instead of relying on a chain of HTML tags, give the element you want to style a meaningful class name.
    • Follow a Naming Convention: Methodologies like BEM (Block, Element, Modifier) help you write modular, decoupled CSS. A BEM selector might look like .card__title or .button--primary. These have low specificity and are easy to understand.

    Bad:

    
    #sidebar div.user-profile ul > li.active a {
      color: red;
    }
    

    Good:

    .user-profile__link--active {
      color: red;
    }
    

    4. Not Using a CSS Reset

    Every browser has its own default stylesheet (the "user agent stylesheet"). This is why an <h1> has a certain size and margin by default. The problem is that these defaults are inconsistent across different browsers.

    The Problem: Without a reset, your website will look slightly different in Chrome, Firefox, and Safari. You'll spend time writing CSS to "undo" browser-specific styles, leading to inconsistent results.

    The Fix: Use a modern CSS reset. A reset neutralizes default browser styling, giving you a clean, consistent base to build upon.

    • Modern Resets: Popular options include Eric Meyer's Reset CSS, Normalize.css, and more modern approaches like Andy Bell's Modern CSS Reset.
    • How to Use: Simply include the reset file at the very top of your CSS, before any of your own styles.

    A simple reset might start like this:

    /* Box-sizing reset */
    *, *::before, *::after {
      box-sizing: border-box;
    }
    
    /* Remove default margin */
    body, h1, h2, p, ul, li {
      margin: 0;
    }
    

    5. Writing Non-Responsive CSS

    In today's multi-device world, failing to write responsive CSS is not an option.

    The Problem: A site designed with fixed widths will be unusable on mobile devices, requiring users to zoom and pan horizontally, which is a terrible user experience.

    The Fix: Adopt a mobile-first mindset and use responsive units and media queries.

    • Mobile-First: Design for the smallest screen first, then use min-width media queries to add complexity for larger screens. This approach is generally simpler and results in less CSS.
    • Use Fluid Layouts: Use max-width on containers to prevent them from becoming too wide on large screens, while allowing them to shrink on small ones.
    • Responsive Units: Use rem for font sizes and vw for elements that should scale with the viewport width.

    Example of a Mobile-First Approach:

    /* Base styles (for mobile) */
    .container {
      padding: 1rem;
    }
    
    .grid {
      display: grid;
      grid-template-columns: 1fr; /* 1 column on mobile */
      gap: 1rem;
    }
    
    /* Tablet and up */
    @media (min-width: 768px) {
      .grid {
        grid-template-columns: 1fr 1fr; /* 2 columns */
      }
    }
    
    /* Desktop and up */
    @media (min-width: 1024px) {
      .grid {
        grid-template-columns: repeat(4, 1fr); /* 4 columns */
      }
    }
    

    By avoiding these common mistakes, you'll write CSS that is not only more effective but also a pleasure to work with.

    Advertisements


    You Might Also Like

    Elegant Sequins Embroidered Lawn Suit 3-Pc (2025) | Diamond Organza Dupatta

    Elegant Sequins Embroidered Lawn Suit 3-Pc (2025) | Diamond Organza Dupatta

    PKR 4700

    Taupe-Brown Embroidered Chiffon Party & Wedding Dress – Unstitched Luxury Outfit with Zari & Sequins Work

    Taupe-Brown Embroidered Chiffon Party & Wedding Dress – Unstitched Luxury Outfit with Zari & Sequins Work

    PKR 7700

    Luxury Heavy Schiffli Embroidered Lawn Suit 3-Pc | Digital Print Silk Dupatta

    Luxury Heavy Schiffli Embroidered Lawn Suit 3-Pc | Digital Print Silk Dupatta

    PKR 6550

    Heavy Handwork Embroidered Organza Wedding Suit | 4-Side Dupatta & Jamawar Trouser

    Heavy Handwork Embroidered Organza Wedding Suit | 4-Side Dupatta & Jamawar Trouser

    PKR 8500

    Luxury Heavy Schiffli Embroidered Lawn Suit 3-Pc | Embroidered Organza Dupatta

    Luxury Heavy Schiffli Embroidered Lawn Suit 3-Pc | Embroidered Organza Dupatta

    PKR 6650

    Advertisements


    Related Posts

    CSS
    CSS Grid vs. Flexbox: Which One Should You Use?
    A practical guide to understanding the key differences between CSS Grid and Flexbox. Learn when to use each for creating powerful, responsive layouts and see examples of how they can work together.

    By Huzi

    Read More
    CSS
    A Practical Guide to CSS Variables (Custom Properties)
    Unlock the power of dynamic and maintainable stylesheets with CSS Custom Properties, commonly known as CSS Variables. This guide covers everything from basic syntax to advanced techniques like theming.

    By Huzi

    Read More
    Linux
    Fix: Wi-Fi Not Working After Arch Linux Installation (2025 Guide)
    A step-by-step guide to fix Wi-Fi issues on a fresh Arch Linux install. Learn to enable NetworkManager, install drivers for Intel & Broadcom, and diagnose your wireless adapter.

    By Huzi

    Read More
    Celebs
    28 Years Later: First Look at Cillian Murphy and the Bone Temple Horror
    As production begins on '28 Years Later,' we explore the return of Cillian Murphy and the terrifying prospect of the 'Bone Temple.'

    By Huzi

    Read More
    DIY
    Easy DIY Home Decor for Small Rooms + How to Organise a Tiny Apartment + Eco-Friendly Upgrades on a Budget
    Peel-&-stick magic, cupboard-turned-coffee-bars and other wallet-friendly wizardry to turn cramped into cosy—without drilling holes your landlord will charge for.

    By Huzi

    Read More
    Lifestyle
    Authentic Pakistani Chicken Biryani Recipe
    Biryani isn't cooked "" it's composed layer by layer, like a symphony of spices. Learn how to make authentic Pakistani Chicken Biryani with this step-by-step recipe.

    By Huzi

    Read More