Modern CSS Code Snippets: Stop writing CSS like it's 2015

(modern-css.com)

145 points | by eustoria 5 hours ago ago

52 comments

  • ktpsns 3 hours ago ago

    CSS in 2025: Let's write html inlined styles as if it was 2005 and separation of formatting/representation was never invented. I talk of tailwind, of course.

    • namuol an hour ago ago

      The deadest horse in web development is the myth of “separation of concerns”

      • appplication 39 minutes ago ago

        You can separate concerns without violating locality of behavior, and that’s exactly what tailwind does.

        It admittedly does not do a good job at being very DRY but I think that’s poorly applied to HTML/CSS in general, and the most DRY css is often over abstracted to the point of becoming nigh uninterpretable.

        • nine_k 25 minutes ago ago

          When I write CSS, I most often do not want the locality of behavior. I instead want uniformity of behavior, hence "semantic" styles. Even the trivial light / dark mode switching is pain with Tailwind, when classes like "color-gray-200" are routinely applied.

          • appplication 19 minutes ago ago

            I’d somewhat agree with you there, but I usually use variables for uniformity. I do see arguments against tailwind but find anytime I’ve tried to do anything else it just feels like bikeshedding on internals for the same end result.

            Really what I want to see is beautiful TDD for CSS so that uniformity can be enforced, but I’m not sure that exists.

            • nine_k 6 minutes ago ago

              Variables are hugely helpful, I agreee. IDK about bikeshedding. I'm very used to writing React code that normally declares no styles for components at all, and having CSS that style components using 1-2 classes, specific to these components. Container components control margins, <body> controls general things like fonts.

              It seems that what solves the problem is a good component library. "But I need red text here!" For what reason? It's a warning. OK, we've got <Text variant="warning">, it will be styled appropriately, and will look like every other warning in the application.

    • crooked-v 2 hours ago ago

      Tailwind is a direct response to how the "C" in "CSS" actually sucks, so there's no surprise that it's so popular.

    • mattlondon 2 hours ago ago

      Wait until you see React & JSX...

      At least html and CSS are both presentation. React/JSX now confuses presentation and business logic.

      • lateforwork 2 hours ago ago

        > React/JSX now confuses presentation and business logic

        React was originally designed to be the "V in MVC". You can still use it that way. React becomes very simple when you only use it as the V in MVC.

        • azangru 2 hours ago ago

          What are the M and the C, and how do they talk to the V in this case?

          • apsurd 2 hours ago ago

            react can be pure functions that take in props. Given a set of props, ideally data primitives, the outputted view is guaranteed. it's nice.

            In practice, the entire JS ecosystem enjoys flying off the rails, every season, but it's not strictly react's fault.

            To answer your question, however those props get into the component is up the the M & C. can be async server, or shoved in as json in the script tag.

            • azangru an hour ago ago

              If you move the data (the M and the C) entirely out of react, and only pass it in via props, there would be only one place — the root react node — where the props could get into react. Is this what you have in mind? Or are you envisioning multiple root nodes?

              • apsurd an hour ago ago

                Well, i've always been a fan of the island architecture that effectively mounts root nodes as little islands of isolated state, yes.

                Mainly this avoids the hell that global state SPA patterns produce: redux, reducer patterns in general, and 8 thousand context providers.

                I do think there's use cases that warrant global in-memory state, but it's such a pain in the ass to maintain and evolve, i'd always plan against it. Every html node in your app does not need to know about literally everything going on and react instantly to it. it just doesn't.

                Just make another page!

                Also: so the islands pattern can be as fancy or rudimentary as desired. they can bootstrap themselves via async endpoints, they can be shipped as web components even, or they can be static, pre-hydrated in some manner.

                • kittbuilds an hour ago ago

                  The islands pattern is underrated for maintainability. I've found the biggest win isn't even the state isolation — it's that each island can have a completely independent upgrade path. You can rewrite one island from React to vanilla JS (or whatever comes next) without touching anything else.

                  The global state SPA pattern fails for a more fundamental reason than just being painful to maintain: it creates an implicit contract between every component in the app. Change one reducer and you're debugging side effects three layers away. Islands make the contract explicit — each one owns its data, full stop.

                  The one gotcha I've hit is cross-island communication. PostMessage works but gets messy. Custom events on a shared DOM ancestor end up being the cleanest pattern for the rare cases where islands genuinely need to coordinate.

              • tim1994 an hour ago ago

                With signals you can avoid the prop drilling. I think signals can help a lot with this approach

                • azangru an hour ago ago

                  I think the parent wants to separate the V from the M/C. If you smuggle signals inside of components to avoid prop drilling, you would be coupling the M/C and the V. I suppose that's not what the parent has in mind.

          • lateforwork an hour ago ago

            M stands for Model layer. This layer handles business logic and knows nothing about UI. It does not have any html or CSS.

            V stands for View. This layer handles HTML and CSS. You can use React here.

            C stands for Controller. Controllers know about Views and Models and which model objects to instantiate for which view. It makes REST API calls and does caching, and handles errors. Controllers know about the application state and decide what page to display next.

            For an application written in this style see: https://github.com/wisercoder/eureka/tree/master/webapp/Clie...

            (This app doesn't use React, but does use TSX, and you could use React as well).

          • cbarrick 2 hours ago ago

            - M for Model: your data model. - V for View: views of your data. - C for Controller: does stuff with your data.

      • madeofpalk an hour ago ago

        I think you're confusing business logic with view logic.

      • bromuro an hour ago ago

        React is great for MVVM indeed. Who is still using MVC in 2026?

        • interlocutor an hour ago ago

          Ever heard of Django? ASP.NET? Most UI frameworks, including ASP.NET Core, Spring Boot (Java based framework), Ruby on Rails, and Django (Python) are all based on MVC.

        • flowerlad an hour ago ago

          MVVM was invented by Microsoft for 2-way syncing in WPF. Today we know 2-way syncing is a mistake.

          Who uses MVC in 2026? Pretty much every framework out there, including Java frameworks and Python frameworks and .net

          • ozim 8 minutes ago ago

            You have any more sources on MVVM being a mistake?

            I found WPF rather nice to work with. Same with knockout.js and Angular I don’t see much downsides.

            Everyone can write bad code of course in each of them but I think it was working quite well.

        • techpression an hour ago ago

          Adding to sibling comments, Phoenix. And it’s a damn nice experience at that.

    • h4x0rr 3 hours ago ago

      Yeah let's do that. You have everything related to your component on place instead of jumping between files.

      • lawn 2 hours ago ago

        Is jumping between files supposed to be difficult or something?

        • afiori 2 hours ago ago

          Without a lot of discipline it is very easy to end up with a css with lots of unclear and hard to guess effects. Eg consider the case of <A type=1><B><A type=2></A></B></A> where A and B are complex templates. Any selector with the " " operator on A risk expanding to the inner A even if it was intended only for the outer. Similarly a :has selector might catch a descendant of the wrong element.

          @scope fixes a lot of this, but it is a complex problem. With tailwind you mostly have to worry about inheritance

          • robertoandred an hour ago ago

            This problem was solved a long time ago with CSS Modules.

            • christophilus 27 minutes ago ago

              I prefer almost anything to CSS modules, so this bike shedding topic is probably very subjective.

        • chrisweekly 2 hours ago ago

          Colocation is a useful principle in component-based architecture.

          • apsurd an hour ago ago

            In my lived experience, shared components just become another problem. Especially in a fledgling company, the iteration velocity is actually negatively affected by shared libs because there's always overhead to (not) break legacy. so shared components bloat to address every evolving need.

            And now with AI generated code i see so many wrapper patterns that forward endless props down, it's crazy!

            TLDR: i almost always end up branching out into evergreen "reusable" components anyway.

            Very unlikely the component library the CTO asked claude to DRY up the code with, is the one to rule them all.

        • runarberg 2 hours ago ago

          Also modern CSS is often written in a <style> tag either in a native web component or in a framework which supports single file component like vue or svelte.

        • ewuhic 2 hours ago ago

          Is staying in one file supposed to be difficult or something?

          • luckylion an hour ago ago

            this is grey text from tailwindcss.com, I wouldn't call it easy and readable.

            <div class="relative before:absolute before:top-0 before:h-px before:w-[200vw] before:bg-gray-950/5 dark:before:bg-white/10 before:-left-[100vw] after:absolute after:bottom-0 after:h-px after:w-[200vw] after:bg-gray-950/5 dark:after:bg-white/10 after:-left-[100vw]"><p class="max-w-(--breakpoint-md) px-2 text-base/7 text-gray-600 max-sm:px-4 dark:text-gray-400">Because Tailwind is so low-level, it never encourages you to design the same site twice. Some of your favorite sites are built with Tailwind, and you probably had no idea.</p></div>

  • deadghost an hour ago ago

    So where are we at with utility libs (tailwind/tachyon) vs inline css in js vs preprocessors (sass/scss) vs vanilla modern css?

    • gassi an hour ago ago

      Wherever you want to be.

        Tailwind works for your team? Go for it.
        Inline CSS for your solo project? By all means.
        Still stuck on SASS? It'll keep working just fine.
        All in on modern CSS? More power to ya!
  • Bengalilol 3 hours ago ago

    Me: cool, let's be creative, I love 2026.

    Browsers: Yeah, but beware of limited availability, most of those creative examples are in the 40-50% browsers support range.

    • graypegg 2 hours ago ago

      In the past this was a major issue that meant useful features were only ever usable after IE/Safari finally supported them half a decade later, but it has seriously gotten better. Sadly as a result of Chromium's overbearing presence, but it's a helpful outcome at least.

      https://wpt.fyi/interop-2025

  • lol768 an hour ago ago

    I'm confused, many of these examples state that they don't work in my browser (Firefox) - but the live demo works fine? Are the demos poly-filled?

    • nine_k 19 minutes ago ago

      No, it says: "Limited availability".

      These green percentage points at the bottom left show how many of the Web audience (IDK by what measurements) runs a browser supporting this feature. A ton of them are even below 50%.

      So I suppose it's not "modern CSS", but more like "latest Chrome CSS". Best viewed in Internet Explorer 5.

  • cwillu 2 hours ago ago

    Stop pinning things to the edges of the screen and window. Some sites have literally over 50% of the viewable area taken up by irrelevant static elements. Let the content scroll, like god intended.

  • anematode 2 hours ago ago

    Random pet peeve... it annoys me when people have old browser-specific aliases to standardized CSS properties. For example, -o-tab-size and -moz-tab-size instead of just tab-size. Those properties haven't done anything on Opera/Firefox for a decade!

  • piskov 3 hours ago ago

    2015 is good enough.

    For example instead of grid center, one can use flex and margin auto.

    If you are building really nation-wide products, there are still a lot of guys in corporate with old windows (where even chrome stopped updating like win7). Or, you know, old or poor people with PC from 2008.

    Also don’t forget guys with mobile phones: not like one could easily install a browser there. Especially on phones which no longer receive updates.

    So writing CSS like it is 2015 is great. Not because it feels great but because it is what caring about your users (and business) is.

    Otherwise you’ll get humbled by your clients soon enough. And in corporate they won’t even be your clients unless you support old stuff: IE 11 is a great target if you really want to shine.

    • nicoburns 3 hours ago ago

      I definitely don't agree with all of these, but grid centering is pretty nice and has a lot fewer quirks than Flexbox based solutions.

    • perardi an hour ago ago

      I dare you to find any analytics, anywhere, that show any IE 11 usage.

      It would be utterly negligent to still be running IE in a corporate environment. It’s a huge security risk.

  • jgalt212 3 hours ago ago

    CSS and JavaScript are like two dysfunctional law enforcement agencies fighting over jurisdiction.

    • sublinear 2 hours ago ago

      All web standards are like this, and then the battle continues when it comes to browser implementation.

  • user3939382 2 hours ago ago

    CSS is the only thing from browsers we actually need. The rest can be done in a terminal. Contemporary terminals could even render the UI with way less memory. The browser is a nightmare because it wasn’t architected to run applications.

    • azangru 2 hours ago ago

      > The browser ... wasn’t architected to run applications.

      Could you explain this? What prevents the browser from running applications? How should it have been architected otherwise if running applications was the goal?

  • laacz 3 hours ago ago

    Is it just me or gradients and tile grid with specific hover effects are AI generated stuff giveaways? Maybe it's old people yelling at clouds, but I'm very reluctant to trust the site, when I see these signs.

    • apsurd 3 hours ago ago

      AI got it from people though.

      I too am saddened by the instant-polish marketing pages everyone and their grandma deploys to Render, but also some people at some point in time really did make these effects. And they are nice. HTML based UIs will always have a place in my heart.

      Btw: actually I think webflow did more to pump this stuff out to the masses. The animate on scroll being the biggest offender. It's so good, but not for every literal text paragraph on your local bakery's website.

    • samhh 3 hours ago ago

      You’re right not to trust it, it’s wrongly calling sibling-index() widely available. And that’s the first example I checked.