Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Stacks and tools

Modern CSS Best Practices

Opens with credits

A practical guide to writing CSS that scales, checked against MDN, web.dev Baseline, the W3C accessibility guidelines and the Tailwind CSS docs. It covers controlling the cascade with layers, design tokens in custom properties, modern layout, responsive and container based design, :has() and native nesting, accessibility and motion, and using Tailwind CSS v4 well.

Written by Tauseef Fayyaz9 chapters, 37 practicesAbout 26 min27 quiz questionsReviewed 13 October 2026

What you will learn

9 chapters · about 26 min

  1. 1Foundations: support, units and a sane baseBefore writing component styles, decide which features you can rely on, how you size things, and what every element starts from.4 practices · 3 min
  2. 2The cascade, specificity and cascade layersMost CSS pain comes from styles fighting each other. This chapter covers keeping specificity low and using cascade layers to decide which group of styles wins.4 practices · 3 min
  3. Custom properties and design tokensCustom properties turn colours, spacing and type into named, themeable values. This chapter covers organizing them as tokens, scoping, fallbacks and typed properties with @property.4 practices · 3 minOpens with credits
  4. Layout with Grid and FlexboxGrid and Flexbox replace floats, positioning tricks and most layout media queries. This chapter covers choosing between them and the patterns that make layouts adapt on their own.4 practices · 3 minOpens with credits
  5. Responsive designResponsive design is about content working at every size and zoom level, not about a few device widths. This chapter covers mobile first styles, breakpoints, fluid type and reflow.5 practices · 4 minOpens with credits
  6. Container queriesContainer queries let a component respond to the space it is given rather than the size of the window. This chapter covers setting up containers, querying them and container units.4 practices · 3 minOpens with credits
  7. Modern selectors and native nesting:has(), :is(), :where() and native nesting remove many reasons to reach for JavaScript or a preprocessor. This chapter covers using them well and their traps.4 practices · 2 minOpens with credits
  8. Accessibility, focus, colour schemes and motionCSS decides whether people can see where they are on a page, read the text and use it without discomfort. This chapter covers focus styles, reduced motion, colour schemes and hiding content correctly.4 practices · 3 minOpens with credits
  9. Using Tailwind CSS wellTailwind CSS v4 is built on the same modern CSS: cascade layers, custom properties and container queries. This chapter covers theme tokens, class detection, custom CSS and responsive variants.4 practices · 2 minOpens with credits

About this guide

These CSS best practices are for developers who can make a page look right but want styles that stay manageable as a project grows: no specificity wars, no !important piles, no breakpoints that only work on the designer's laptop. The advice is checked against MDN's CSS guides and reference, web.dev's Baseline definitions, the W3C's WCAG 2.2 understanding documents and the Tailwind CSS documentation.

CSS has changed more in the last few years than in the decade before. Cascade layers let you decide which group of styles wins, without fighting specificity. Container queries let a component respond to the space it actually has, not the size of the window. :has() selects a parent based on its children. Nesting is native, with no preprocessor. Registered custom properties can be typed and animated. These features are now available across the major browsers, and they replace many of the hacks older codebases still carry.

The first chapters cover the foundations: checking browser support with Baseline, then the cascade and specificity, then custom properties as design tokens. The middle chapters cover layout, responsive design and container queries. The last chapters cover modern selectors and nesting, accessibility and motion, and how these ideas apply when you use Tailwind CSS.

Most examples are plain CSS, so they apply whatever framework you use. Where Tailwind does things differently, the last chapter shows how.

Common questions

What are CSS cascade layers and when should I use them?

Cascade layers group styles with @layer and let you decide which group wins before specificity is compared. Use them to order resets, base styles, third party CSS, components and utilities, so you never need higher specificity or !important to override something. Remember that unlayered styles beat layered ones.

Should I use container queries or media queries?

Use media queries for page level layout and user preferences such as dark mode and reduced motion. Use container queries for reusable components that appear in different sized places, so each adapts to the space it actually has. Mark the parent with container-type: inline-size first.

Is CSS nesting supported without Sass?

Yes, browsers parse nested CSS natively. The main differences from Sass are that compound selectors need &, as in &.active, and string concatenation such as &__title does not work. Check MDN for current Baseline status if you support older browsers.

When should I use Flexbox vs Grid?

Use Flexbox for items in one direction whose sizes come from their content, such as toolbars and button groups. Use Grid for layouts in rows and columns, or when the container should decide the sizes, such as page layouts and card grids. Both support gap for spacing.

How do I avoid specificity problems in CSS?

Style with single classes instead of IDs and long selectors, use :where() for defaults that should be easy to override, and use cascade layers to order groups of styles. Avoid !important, except deliberately in an early layer.

How do I use CSS variables with Tailwind CSS v4?

Define tokens that should become utility classes inside @theme, such as --color-brand, which creates bg-brand and also outputs a CSS variable. Define plain variables that should not create utilities in :root. Both can be used with var() in custom CSS.

Sources (8)

Checked against these, last on 13 October 2026.

  1. Cascade layers, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Cascade_layers
  2. Container size and style queries, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_queries
  3. Using CSS custom properties (variables), MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Guides/Cascading_variables/Using_custom_properties
  4. Using CSS nesting, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Guides/Nesting/Using_CSS_nesting
  5. :has(), MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:has
  6. Baseline, web.devweb.dev/baseline
  7. Understanding Success Criterion 1.4.10: Reflow, W3C WAIwww.w3.org/WAI/WCAG22/Understanding/reflow.html
  8. Theme variables, Tailwind CSS docstailwindcss.com/docs/theme

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com