Best practices · Stacks and tools
Modern CSS Best Practices
Opens with creditsA 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.
What you will learn
9 chapters · about 26 min
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
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
- 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
- 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
- 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
- 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
- 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
- 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
- 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.
- Cascade layers, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Cascade_layers
- Container size and style queries, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Guides/Containment/Container_queries
- Using CSS custom properties (variables), MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Guides/Cascading_variables/Using_custom_properties
- Using CSS nesting, MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Guides/Nesting/Using_CSS_nesting
- :has(), MDN Web Docsdeveloper.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:has
- Baseline, web.devweb.dev/baseline
- Understanding Success Criterion 1.4.10: Reflow, W3C WAIwww.w3.org/WAI/WCAG22/Understanding/reflow.html
- Theme variables, Tailwind CSS docstailwindcss.com/docs/theme
Related on Sefism
Picked from the other libraries by shared topics.
Roadmaps
See allCheat sheets
See allBest practices
See all- React Best PracticesModern React best practices for 2026: pure components, lean state, fewer effects, React Compiler, form actions, accessibility and testing.
- TypeScript Best PracticesModern TypeScript best practices for 2026: strict settings, TypeScript 7, modeling data with unions, safe narrowing, runtime validation, generics and typed linting.
- Next.js App Router Best PracticesNext.js 16 App Router best practices: Server Components, Cache Components and use cache, secure Server Actions, proxy.ts, metadata and images.
Checklists
See all- Web Accessibility ChecklistA practical web accessibility checklist built on WCAG 2.2 level AA, with a severity and a concrete way to verify every check.
- Web Performance ChecklistA practical web performance checklist built on Core Web Vitals: LCP, INP and CLS, with a severity and a concrete way to verify every check.
Project structures
See all- React with Vite (TypeScript) project structureFor developers building a single-page React app that talks to a separate backend: an orders dashboard with auth, routing, server data, forms and tests. Product code is grouped by feature under src/features, shared pieces live in components and lib, and src/app wires providers, routes and guards together.
- Next.js (App Router) project structureFor React developers building a real web front end with Next.js: a shop dashboard with public pages, sign-in, orders and products, talking to a separate backend API. The app folder holds routes only, product code lives in feature folders, server-only code sits in src/server, and the browser side gets a proper React setup: providers, TanStack Query, forms with React Hook Form and Zod, and tests from unit to end-to-end.
- Angular project structureFor developers building a structured single-page app with Angular, the framework many enterprise and services companies use. The example is an orders dashboard with a login: standalone components, signals for state, typed forms, lazy-loaded feature routes, and a small core and shared layer around feature folders, grown from the ng new scaffold to a repository a team can run for years.
Career questions
See all- How do I choose between frontend, backend, mobile, cloud, cybersecurity, data and AI without trying them all for years?Do not try to pick the best field; pick the one whose daily problems you can tolerate on a bad day. Run a short, cheap experiment in two or three candidates, compare what you enjoyed doing rather than what sounded exciting, check the local job market, and commit for six to nine months.
- Should I become an AI engineer or stay a full stack developer?For most full stack developers this is not an either/or. AI engineering is largely full stack engineering plus models, evaluation and data. Keep your full stack strength, add AI features to real work, and move titles only when you have shipped AI work you can point to.
- What should I learn to become a web developer, and in what order?Learn the web platform first (HTML, CSS, JavaScript, how HTTP works), then one frontend framework, then a backend with a database, and build a real project at each stage. If you already know WordPress, you are not starting over: your HTML, CSS and PHP experience transfers.
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