CSS gets a bad rap. In fact, in a recent poll with over 30,000 responses, CSS topped the list as the most painful technology for web developers. People say it’s broken, chaotic, and just plain hard. Some even think it’s a conspiracy designed to protect the app stores! But here’s the thing: CSS is awesome. Sure, it’s bloated and difficult to master, but that’s because it’s evolved over the last 25 years.
When CSS first came out, Netscape was the top browser, and the idea of responsive layouts wasn’t even on the horizon. As more browsers entered the market, each implemented CSS in its own way. This led to code that worked in one browser but not the other, forcing developers to write confusing vendor prefixes just to make things work everywhere.
How To Make CSS Less Painful – 10 TIPS
I get it—hating CSS is easy. But today, we’re diving into some modern CSS techniques to make it cleaner, more efficient, and yes, even fun. By the end of this therapy session, you’ll learn how to avoid some of the bad code you shouldn’t be writing in 2021 (or beyond).
Ready? Let’s get into it!
1. Learn the Box Model
First things first: Don’t learn CSS by relying on frameworks like Bootstrap or Tailwind. Sure, they can help you build a slick UI quickly, but using them is like getting married to the framework. You won’t learn CSS fundamentals—you’ll learn Tailwind. And if you ever want to switch to something else, well, you’re in for a rough divorce.
When you learn basic CSS, you gain more control over your code, creativity, and freedom. The best advice I didn’t get until later in my web dev career? Learn the CSS box model.
Think of every HTML element as a box. Inside that box, you have content that can have width and height. You can add padding around the content to give it some breathing room. Then you can add a border around the outside and finally, some invisible space around the border called a margin.
Everything in CSS related to layout and positioning revolves around the box model. Open up Chrome DevTools, and you’ll see how the box model is computed for any element on the page. It’s a crucial foundation that makes everything else in CSS click.
Example:
div {
width: 200px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}
Here, the div
will have a total width of 250px (content width + padding + border), with space around it thanks to the margin.
2. Firefox DevTools are Amazeballs
While Chrome DevTools are great, when it comes to debugging CSS, Firefox is a hidden gem. Their dev tools are generally superior, especially for CSS work. If you inspect an element in Firefox, you’ll see a breakdown of the box model just like in Chrome, but Firefox goes a step further. You can edit properties directly and get a detailed breakdown of what’s influencing the box model.
Firefox also provides useful annotations, like when an element is causing overflow, and it has fantastic visual aids for Flexbox and Grid layouts. If you haven’t tried Firefox for CSS debugging yet, give it a shot!
3. Flexbox is Fantastic
One of the trickiest parts of CSS is layout—positioning elements relative to each other. The classic “How do I center a div?” problem can be solved a hundred ways, but none of them are intuitive. Until Flexbox, that is.
Flexbox allows you to create a flexible column or row layout with ease. When you set an element to display: flex;
, it gains an x-axis (main axis) and a y-axis (cross axis). You can align its children in the center with justify-content: center;
(for the main axis) and align-items: center;
(for the cross axis). Flexbox is my go-to for layouts—except when things get too complex.
Example:
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
This will perfectly center any content inside the container, both horizontally and vertically. Easy, right?
4. Grid is Great
When you have a big, complex UI with many intersecting rows and columns, Flexbox can get messy. That’s where Grid comes in. Unlike Flexbox, which deals with individual rows and columns, Grid allows you to think about the entire layout. It’s like the old table layouts of the early web, but much more developer-friendly.
With Grid, you can define your layout in terms of rows and columns, and CSS handles the rest. It’s especially great for eliminating unnecessary HTML elements that you’d otherwise need with Flexbox.
Example:
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto;
}
Here, we’ve created a grid with three columns: the first and third columns take up one fraction of space, while the middle column takes up two fractions. It’s clean, simple, and powerful.
5. Clamp it Down
Responsive design often means adjusting the width of elements based on the device or viewport. Traditionally, you’d do this with media queries, but as your project grows, media queries can quickly become a nightmare.
Enter the clamp()
function. Instead of writing multiple media queries, you can use clamp()
to set a minimum value, a preferred value, and a maximum value all in one line of code.
Example:
.article {
width: clamp(200px, 50%, 800px);
}
This sets the width to 50%, but never smaller than 200px or larger than 800px. It’s a massive code saver and makes your styles much more flexible.
6. Aspect Ratio One-Liner
Ever had to code a responsive image or video with a fixed aspect ratio? You probably used the old padding-top trick. Well, you can forget that hack now. With the aspect-ratio
property, maintaining aspect ratios is a breeze.
Example:
.video {
aspect-ratio: 16 / 9;
}
This one line of CSS replaces that whole padding hack, making your code cleaner and more intuitive.
7. Variables for Variables
CSS has custom properties, aka variables, which make your code more flexible and easier to maintain. Instead of repeating the same values over and over, define them as variables and reference them throughout your styles. This way, if you need to change a value, you only have to do it in one place.
Example:
:root {
--primary-color: #3498db;
}
button {
background-color: var(--primary-color);
}
Now, you can easily switch up your primary color across your entire site by changing just one line of code.
8. Fancy Calculations
CSS isn’t just for styling anymore—it can do math too! The calc()
function allows you to perform basic calculations directly in your styles. You can even combine different units, which comes in handy when you need more precise control.
Example:
.container {
width: calc(100% - 50px);
}
This sets the width to the full viewport width minus 50 pixels, giving you a lot of flexibility in your layouts.
9. State Management Counter
Did you know that CSS can manage state? With CSS counters, you can keep track of a running count without writing any JavaScript. This is super useful for automatically numbering headings or list items.
Example:
h1 {
counter-increment: section;
}
h1::before {
content: counter(section) ". ";
}
Now, every h1
will automatically be numbered in the order they appear on the page—no manual updates needed.
10. Finding focus-within
Building a dropdown menu? You might think you need JavaScript to manage the open and closed states, but CSS’s :focus-within
pseudo-class can save you a lot of trouble. It stays active as long as any child element has focus, which means you can handle state transitions without any JavaScript.
Example:
.dropdown:focus-within .menu {
display: block;
}
Now, when a user clicks into the dropdown, the menu stays open even if they click on something inside it. Less JavaScript = more happiness.
Bonus: Treat an Incurable Disease
Remember those browser vendor prefixes? Yeah, they’re like herpes—they’re not going away. But you can manage them with tools like PostCSS and Autoprefixer. These tools automatically add vendor prefixes and let you use modern CSS features, even if they’re not fully supported in all browsers yet.
Shortest Path Algorithms Explained (Dijkstra’s & Bellman-Ford)
And that’s a wrap! I hope these tips make CSS less painful and more fun for you. Leave a comment with your favorite CSS pro tip, and I’ll pick a few winners to receive a one-of-a-kind t-shirt next week. Thanks for reading, and I’ll see you in the next one!