DEV Community

Cover image for CSS Concepts That Will Actually Make Your Life as a Front-End Developer Easier
Muhammad Usman
Muhammad Usman

Posted on • Originally published at pixicstudio.Medium

CSS Concepts That Will Actually Make Your Life as a Front-End Developer Easier

CSS has a pretty simple syntax, but there are a lot of fundamental principles in how it works that catch people off guard. This can lead to confusion and a lot of frustration. There’s also times where there’s multiple ways to do the same thing, and that can lead to extra frustration as well because you don’t know which one is better in any given situation.

Please support my original publication, where I have originally published this article. Your support means alot to me: CSS Concepts That Will Actually Make Your Life as a Front-End Developer Easier

CSS really is one of those things that when you first start off with it, it just seems like it’s going to be the easiest thing in the world and quickly can lead to times where you just want to throw your computer out the window. So with that in mind, let’s look at eight extremely important CSS concepts that when you understand them can really help simplify things and make your life much easier when you’re writing CSS.

As always, here’s the cdoe with working examples:

1. The Box Model (and Why You Need box-sizing: border-box)

This is one of those things where it’s just really important to do this in every file you ever work in. Start your CSS with this:

* {
box-sizing: border-box;
}
Enter fullscreen mode Exit fullscreen mode

The star selector means “select everything.” Now, over time you’re also going to start using pseudo-elements (don’t worry if you don’t know what those are yet, we’ll talk about them later), and when you do, you’ll want to update this to:

*, *::before, *::after {
box-sizing: border-box;
}
Enter fullscreen mode Exit fullscreen mode

Just get in the habit of including the pseudo-elements now, even if you’re not using them yet, because you’re going to need them eventually.

CSS Model BoxHere’s why this matters. Let’s say you have an element and you give it a width of 400 pixels and padding of 20 pixels. The default behavior (called content-box) means that width is just the content itself, so you're adding 20 pixels on the left and 20 pixels on the right on top of that 400 pixels. Your element is actually 440 pixels wide now.

With box-sizing: border-box, that 400 pixels includes the padding. The padding pushes inward, and the element stays 400 pixels wide. This makes things much easier to calculate and to really be able to gauge how big something will be.

This isn’t as important as it used to be in the old days when we had float-based layouts and widths and heights were much more prevalent, but there’s still things where we set explicit widths and this is going to save you a lot of trouble in the long run. Just get in the habit of putting this at the top of every CSS file you create.

2. Specificity (The Reason Your Styles Aren’t Working)

Specificity is how specific our selectors are in our CSS, and it’s probably the number one reason beginners get frustrated when their styles don’t apply.

There tends to be three levels of selectors:

Element selectors (lowest specificity):

h2 {
color: red;
}
Enter fullscreen mode Exit fullscreen mode

Class selectors (medium specificity):

.color-accent {
color: purple;
}
Enter fullscreen mode Exit fullscreen mode

ID selectors (highest specificity):

#example {
color: lime;
}
Enter fullscreen mode Exit fullscreen mode

Here’s the thing: even if you have an h2 selector lower down in your CSS file that says the color should be red, if that h2 has a class on it with a different color, the class will win. It doesn’t matter where it is in the cascade because a class selector has more importance than an element selector.

CSS SpecificityThe same goes for IDs, an ID selector will always beat a class selector, which will always beat an element selector.

You can also boost specificity with descendant selectors. For example:

.dark-background h3 {
color: red;
}
Enter fullscreen mode Exit fullscreen mode

This is more specific than just h3 because it's a class selector and an element selector combined together.

When something isn’t working, your dev tools can really help you out. Right-click on the element, inspect it, and you’ll see all the styles being applied. The ones that are crossed out are being overridden, and you can see exactly why, what selector is winning and where it’s coming from in your CSS file.

In general, try to avoid too much nesting and overly specific selectors. A lot of design systems use single class selectors for good reason, they keep specificity manageable and predictable.

3. Inheritance (Write Less CSS by Letting Things Flow Down)

Inheritance is when properties are passed down from a parent element to its children and grandchildren. This is one of those things you hear about early on but then you forget about it or you don’t really understand the implications of it.

Here’s the key: anything related to typography inherits by default. This includes color, font-family, font-size, text-align, line-height, and so on. Anything that is not related to typography generally does not inherit.

CSS InheritanceThis means you can set styles once on the body (or html element) and they’ll flow down to everything inside:

body {
color: #333;
font-size: 1.25rem;
font-family: Arial, sans-serif;
}
Enter fullscreen mode Exit fullscreen mode

Now all your paragraphs, headings, and other text elements will inherit these styles. You don’t have to select each one individually.

The big advantage here is you get to write a lot less code. You set it once and then you don’t have to worry about it again. Of course, you can always overwrite things with more specific selectors when you need to.

One quick note: form elements don’t actually inherit font properties like you’d expect them to. You’ll often see this in CSS resets:

button, input, textarea, select {
font: inherit;
}
Enter fullscreen mode Exit fullscreen mode

This forces these elements to start inheriting all the font properties like you would expect them to in the first place. The font: inherit; is a shorthand that inherits font-family, font-size, font-weight, line-height, and all the other font-related properties at once.

As much as possible, rely on inheritance. It means you get to write a lot less code and you can pick and choose places where you want to break out of that. One of the places where people go wrong is they start selecting everything and trying to style everything individually, and it just creates so much more work for you.

4. CSS Units (Getting the Right Size for the Job)

Understanding different units is fundamental for creating layouts that work across different screen sizes and situations. There are a lot of different units in CSS, and knowing which one to use can be confusing.

CSS Units*Absolute units:*

  • px - Pixels are fixed units. Good for borders and small, precise measurements, but not great for responsive design when used for layout.

Relative units:

  • % - Percentage of the parent element's size. Great for widths.
  • em - Relative to the font-size of the element itself. Compounds when nested (can be tricky).
  • rem - Relative to the root (html) font-size. Doesn't compound, making it more predictable. Great for spacing and font sizes.
  • vw/vh - Viewport width and height. 1vw is 1% of the viewport width. Great for full-screen sections.

For beginners, here’s a simple starting point:

  • Use rem for font sizes and spacing (padding, margin)
  • Use % or fr (in grid) for widths
  • Use px for small things like borders
  • Use vw/vh when you need something relative to the viewport

The reason rem is so popular is because if a user changes their browser's default font size (for accessibility), everything scales proportionally. If you used px everywhere, nothing would scale.

You don’t need to memorize all of this right away. Start with rem for most things and px for borders, and you'll be in good shape. As you get more comfortable, you can experiment with the other units.

5. Display Property (How Elements Actually Behave)

The display property is fundamental to understanding how elements behave on the page. Every HTML element has a default display value, and understanding this is crucial.

CSS Display Property*display: block* Block elements take up the full width available and start on a new line. Think of divs, paragraphs, headings, they stack vertically. You can set width and height on block elements.

display: inline Inline elements only take up as much width as they need and don’t start on a new line. Think of spans, links, strong tags, they flow within text. You can’t set width and height on inline elements.

display: inline-block This is a hybrid, it flows inline like an inline element, but you can set width and height on it like a block element. Really useful for things like navigation items or buttons that need to sit next to each other but have specific dimensions.

display: none This removes the element from the page completely, it doesn’t take up any space. Different from visibility: hidden which hides it but leaves a gap where it would be.

display: flex and display: grid These are your layout powerhouses. We’ll talk more about them in the next section.

Understanding display is important because sometimes you need to change an element’s default behavior. Maybe you want a link to behave like a block so you can give it padding and make it easier to click. Or you want list items to sit next to each other instead of stacking.

6. Layouts with Flexbox and Grid (Not Positioning!)

When you’re building layouts, you’re going to want to choose either flexbox or grid. If you haven’t learned either one of them yet, you will eventually want to learn both.

CSS Layouts with Flexbox and GridHere’s my recommendation: start with Grid. It’s easier to understand when you’re just getting started, and it gives you a lot more control for building page layouts.

Why Grid for layouts:

.columns {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 1rem;
}
Enter fullscreen mode Exit fullscreen mode

That’s it. You have three equal columns with space between them. The fr unit is a fraction of the available space - one fraction for each column. It's flexible and responsive by default.

Why Flexbox is also important: Flexbox is amazing for component-level layouts, things like navigation bars, cards, centering content, spacing items evenly. Once you’re comfortable with the basics of Grid, add Flexbox to your toolkit.

.nav {
display: flex;
justify-content: space-between;
align-items: center;
}
Enter fullscreen mode Exit fullscreen mode

What to avoid: Don’t use positioning (position: absolute, position: fixed) for layouts. Positioning is for tweaks and specific UI patterns, not for building your main page structure. You learn position absolute, set a width and height, put it where you want, that’s like the worst possible way you could actually build a layout.

You might also see old tutorials with floats. If you see things that come up on building a layout with floats, completely avoid it. Floats were a hack we used before flexbox and grid existed. All modern layouts are built with flexbox or grid.

These days we don’t need to set widths as much because the parent container does all the work for us. You set up the grid or flex container, and the children just fit into the space they’re given.

7. Pseudo-classes and Pseudo-elements (Making Things Interactive)

Pseudo-classes and pseudo-elements are incredibly powerful tools that let you style elements based on their state or add content without touching your HTML. They look similar but do different things, and the way you write them is slightly different too.

CSS Pseudo-classes and Pseudo-elements*Pseudo-classes* use a single colon (:) and are all about state. They let you style elements when something happens to them:

a:hover {
color: blue;
text-decoration: underline;
}
Enter fullscreen mode Exit fullscreen mode
button:focus {
  outline: 2px solid blue;
}
input:invalid {
  border-color: red;
}
li:first-child {
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

These are essential for creating interactive experiences. Your links need hover states, your form inputs need focus styles for accessibility, and you often want to style the first or last item in a list differently.

Pseudo-elements use a double colon (::) and let you add decorative content or style specific parts of an element:

.quote::before {
content: '"';
font-size: 2em;
color: gray;
}
Enter fullscreen mode Exit fullscreen mode
p::first-line {
font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode
.card::after {
content: '';
display: block;
width: 100%;
height: 3px;
background: blue;
margin-top: 1rem;
}
Enter fullscreen mode Exit fullscreen mode

The ::before and ::after pseudo-elements are particularly useful for adding decorative elements without cluttering your HTML. You can create icons, dividers, or visual effects purely with CSS.

One thing to remember: pseudo-elements need the content property to show up, even if it's just content: ''. Without it, they won't render at all.

And here’s why back in the box model section I included *::before and *::after in the box-sizing rule - pseudo-elements create actual boxes on the page that need the same box-sizing behavior as everything else. If you don't include them, you might run into weird sizing issues later on.

8. Responsive Design with Media Queries (Making It Work Everywhere)

Your website needs to work on phones, tablets, laptops, and big desktop monitors. That’s just reality these days. And the way we make that happen is with media queries.

CSS Responsive Design with Media QueriesMedia queries let you apply different styles based on screen size:

/* Mobile-first approach - styles for small screens first */
.container {
padding: 1rem;
}
Enter fullscreen mode Exit fullscreen mode
/* Tablets and up */
@media (min-width: 768px) {
.container {
padding: 2rem;
}
}
Enter fullscreen mode Exit fullscreen mode
/* Desktop and up */
@media (min-width: 1024px) {
.container {
padding: 3rem;
max-width: 1200px;
margin: 0 auto;
}
}
Enter fullscreen mode Exit fullscreen mode

I recommend a mobile-first approach. Write your base styles for mobile devices, then use min-width media queries to add complexity as the screen gets bigger. This makes more sense because:

  • Mobile is the most constrained environment — if it works on mobile, you can build up from there
  • It’s easier to add complexity than to strip it away
  • Most traffic these days is mobile anyway

You can also use media queries for other things:

/* Dark mode */
@media (prefers-color-scheme: dark) {
body {
background: #222;
color: #fff;
}
}
Enter fullscreen mode Exit fullscreen mode
/* Print styles */
@media print {
nav, footer {
display: none;
}
}
Enter fullscreen mode Exit fullscreen mode

Common breakpoints you’ll see are:

  • 640px or 768px for tablets
  • 1024px or 1280px for desktop
  • But honestly, your breakpoints should be based on your content, not specific devices

Don’t stress too much about getting the perfect breakpoints from the start. As you build, you’ll see where your design breaks and that’s where you add a media query. Your browser dev tools let you test this easily — just toggle the responsive design mode and resize the viewport to see where things start looking wonky.

You don’t need to master responsive design right away. Start with making things work on mobile, then add a breakpoint or two as you need them. Over time you’ll get a feel for where you typically need to adjust things.

Bonus: Separate Layout from Content

This last one is a little bit based on what I just showed you with layouts. As much as possible, try to create a separation between the way you’re styling your layouts and the content itself.

CSS Separating Layout from ContentWhat I mean by that is having classes that do specific jobs:

/* This class handles layout */.columns {  display: grid;  grid-template-columns: 1fr 1fr 1fr;  gap: 1rem;}
Enter fullscreen mode Exit fullscreen mode
/* This class handles content styling */.card {  background: #333;  color: white;  padding: 2rem;}
Enter fullscreen mode Exit fullscreen mode

Then in your HTML:

<div class="columns">
  <div class="card">Content here</div>
  <div class="card">Content here</div>
  <div class="card">Content here</div>
</div>
Enter fullscreen mode Exit fullscreen mode

The columns class is setting up the layout structure, and the card class is styling what the content inside that layout looks like. They have separate jobs.

This makes your life a lot easier because:

  • It’s easier to find selectors when you need to modify something
  • You run into fewer conflicts with the cascade and specificity
  • You can reuse these classes in different combinations
  • Your code is more maintainable

You could even apply both classes to the same element if needed:

<div class="columns card">
Enter fullscreen mode Exit fullscreen mode

The point is that each class has a specific job, and that makes everything more predictable and easier to work with.

These eight concepts, the box model, specificity, inheritance, CSS units, the display property, modern layouts, pseudo-classes and pseudo-elements, and responsive design, form the foundation of writing good CSS. When you understand how these work together, CSS stops feeling like magic (or a source of frustration) and starts making logical sense.

You don’t need to master all of these at once. Start with one, get comfortable with it, and move on to the next. But understanding these fundamentals will save you countless hours of debugging and make you much more confident when writing CSS.


Did you learn something good today?
Then show some love. 🍡
Your support always made my day. 🌎
© Muhammad Usman
WordPress Developer | Website Strategist | SEO Specialist
Don’t forget to subscribe to Developer’s Journey to show your support.
Don’t forget to bookmark this list for your next project.

Top comments (0)