惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

CSS Wizardry

Front-End’s Missing Metric: The TBT Window Meet Your Users Where They Are with Obs.js Better Browser Caching with No-Vary-Search font-family Doesn’t Fall Back the Way You Think What Is CSS Containment and How Can I Use It? When All You Can Do Is All or Nothing, Do Nothing Obs.js: Context-Aware Web Performance for Everyone Low- and Mid-Tier Mobile for the Real World (2025) The Fastest Site in the Tour de France Making Sense of the Performance Extensibility API Why Do We Have a Cache-Control Request Header? HTML Is Not a Programming Language… Build for the Web, Build on the Web, Build with the Web Licensing Code on CSS Wizardry A Layered Approach to Speculation Rules Designing (and Evolving) a New Web Performance Score Core Web Vitals Colours The Ultimate Contract Templates for Tech Consultants: Protect Your Business and Get Paid Optimising for High Latency Environments Cache Grab: How Much Are You Leaving on the Table? blocking=render: Why would you do that?! Correctly Configure (Pre) Connections The Three Cs: 🤝 Concatenate, 🗜️ Compress, 🗳️ Cache What Is the Maximum max-age? How to Clear Cache and Cookies on a Customer’s Device The Ultimate Low-Quality Image Placeholder Technique Core Web Vitals for Search Engine Optimisation: What Do We Need to Know? The HTTP/1-liness of HTTP/2 In Defence of DOM­Content­Loaded Site-Speed Topography Remapped Why Not document.write()? Speeding Up Async Snippets Critical CSS? Not So Fast! Measure What You Impact, Not What You Influence Optimising Largest Contentful Paint Measuring Web Performance in Mobile Safari Site-Speed Topography Speed Up Google Fonts Real-World Effectiveness of Brotli Performance Budgets, Pragmatically Lazy Pre-Browsing with Prefetch Making Cloud.typography Fast(er) Time to First Byte: What It Is and How to Improve It Self-Host Your Static Assets Tips for Technical Interviews Cache-Control for Civilians Bandwidth or Latency: When to Optimise for Which ITCSS × Skillshare What If? CSS and Network Performance The Three Types of Performance Testing Getting to Know a Legacy Codebase Image Inconsistencies: How and When Browsers Download Images Identifying, Auditing, and Discussing Third Parties My Digital Music Setup Measuring the Hard-to-Measure Finding Dead CSS The Fallacies of Distributed Computing (Applied to Front-End Performance) Ten Years Old Relative Requirements Airplanes and Ashtrays Performance and Resilience: Stress-Testing Third Parties Refactoring Tunnels Little Things I Like to Do with Git Writing Tidy Code Configuring Git and Vim Base64 Encoding & Performance, Part 2: Gathering Data Base64 Encoding & Performance, Part 1: What’s Up with Base64? Code Smells in CSS Revisited Typography for Developers Moving CSS Wizardry onto HTTPS and HTTP/2 Ack for CSS Developers A New Year, a New Focus Preparing Vim for Apple’s Touch Bar Choosing the Correct Average CSS Shorthand Syntax Considered an Anti-Pattern CSS Wizardry Newsletter Nesting Your BEM? Improving Perceived Performance with Multiple Background Images Continue Normalising Your CSS Pure CSS Content Filter Pragmatic, Practical, and Progressive Theming with Custom Properties Refactoring CSS: The Three I’s Speaker’s Checklist: Before and After Your Talk Improving Your CSS with Parker The Importance of !important: Forcing Immutability in CSS Mixins Better for Performance Managing Typography on Large Apps White October Events Workshop Partnership BEMIT: Taking the BEM Naming Convention a Step Further Travelling Like You Want to, When You Have To Contextual Styling: UI Components, Nesting, and Implementation Detail Subtleties with Self-Chained Classes Cyclomatic Complexity: Logic in CSS Immutable CSS Can CSS Be Too Modular? More Transparent UI Code with Namespaces When to use @extend; when to use a mixin The Specificity Graph CSS Wizardry Ltd.: Year 1 in review
Responsive grid systems; a solution?
Harry Roberts · 2013-02-07 · via CSS Wizardry

Written by on CSS Wizardry.

Table of Contents

Independent writing is brought to you via my wonderful Supporters.

  1. Taking stock
  2. Building the grid system
    1. Featured case study: NHS
  3. Defining the breakpoints
  4. Lots of classes in the CSS
  5. Implementation
  6. Recap
  7. Lots of classes in the HTML?
  8. Final word

Grid systems in responsive design are something of a hot topic; a lot of people are trying to solve them but I don’t feel we’re there quite yet.

However! The solution I have devised for inuit.css – I feel – is probably one of the best currently available. In this article I’m going to cover how it works, and the principles behind it.

Before we dive into it, let’s outline some of the problems and requirements we face with responsive grid systems:

  • Must have different traits at different sizes because you need to be able to tell a column to be – for example – full-width on narrow devices and half-width on wider ones.
  • Must be fluid between breakpoints because we need to cover all bases.
  • Must have enough control to decide which columns will transform and at which point because a blanket rule of, say, ‘all half-width columns become full-width at XYZ breakpoint’ are no good; we might want to collapse some but leave others half-width.
  • Classes should ideally still make sense at all breakpoints because .span-6 doesn’t make sense when it’s transformed to behave like a .span-12 on narrow screens.

The easy solution to the responsive grid problem might be to make a huge upfront decision. For example, deciding that, at a certain device width, you are going to make all columns halve, and then at the narrowest width you want them all to collapse into full-width columns.

This might work, but is a huge design decision that will likely prove very restrictive. You might not want all your layout to linearise, but a grid system like this would cause it to.

If you’re using Sass, it might be nice to do something like:

.content{
    @extend .span-12;
    @media screen and (min-width:720px){
        @extend .span-6;
    }
    @media screen and (min-width:1200px){
        @extend .span-8;
    }
}

But of course, you currently can’t @extend out of a media query. Where does that leave us?

Taking stock

Sometimes I think we all need to slow down, sit back, look at what we’ve got, and take stock of things. Take things way, way back to basics and start again.

If we redefine our problem, and think on much more simple terms, then we’re looking at this:

I want elements to take on multiple different and conflicting forms at different points.

Basically, we want one thing (a grid column) to be several things (different widths) at different states (breakpoints).

The easiest way to make one thing behave in multiple different ways is, simply, multiple style hooks (i.e. classes). To make one thing behave in a variety of different (and opposing; remember, a grid cannot be full-width and half-width at the same time) ways is to have a separate class for each of those states.

Even with media queries, it is naive to think you can simply, consistently and cleanly make an arbitrary array of things all behave completely differently. If you want a div to have three states then it’s somewhat naive to think you can handle that nicely without three corresponding classes.

We also have to think with some degree of pragmatism; one valiant but misguided attitude a lot of developers seem to have is the desire to be all things to all men. This is admirable, but often counter-intuitive and counter-productive. We need to accept that – when faced with complex problems such as responsive grids – we cannot be everything to everyone.

So, if we act pragmatically and take stock of our situation then we have a much more defined baseline to start building on.

Clean HTML purists, please stick with me!

Building the grid system

Featured case study: NHS

How I helped the NHS rapidly build a brand new product.

Read case study…

In order to combat our .span-12 style of inappropriate classes, we need to think about working with classes like .one-whole, .three-fifths, .five-twelfths and so on. These mean that, instead of spanning x columns, we are now telling a column to take up x percentage of the available width.

What I do with inuit.css is abstract these widths right out and away from the grid system entirely. This means that, although they can be used on the grid system, they are not exclusive to it. The benefit of this is that I could have, say, a photo that I wish to take up half the width of my page: <img src="foo.jpg" alt="" class="one-half">. This would not previously, necessarily, have been possible if they were tied into the grid system too much.

This now means that the grid architecture itself is very slim, just some floats and some clearfixing, as well as some gutter settings.

So, my structural grid stuff lives alone, and the widths merely lay on top of this architecture to provide the sizing.

We still have the problem, however, that .one-half doesn’t make sense if I want to make it a .one-whole at a certain breakpoint…

Defining the breakpoints

The beauty of high-level page layout (i.e. a grid system) is that this is something that is based on device/screen/viewport size.

Grid systems are incredibly high-level scaffolding and page structure, so they will likely only change in the shift between breakpoints (and often devices), for example a layout might typically be linear for mobile (or smallest screens), a little wider for tablet (smaller screens), wider still for desktop (or wider screens).

Using this diagram from none other than Luke Wroblewski we can choose our layout-specific breakpoints as being:

  • Palm for things that fit in our hands (like phones).
  • Lap for devices that are not handheld, but ‘lap’ devices (like tablets).
  • Portable for either of the above.
  • Desk for stationary devices (like TVs, PCs etc).

If we accept this generalisation that means we can have multiple width classes like .one-half, .palm-one-half that all live in media queries:

/**
 * Palm-sized devices
 */
@media screen and (min-width:[width of your choosing]){
    .palm-one-whole{
        width:100%;
    }
    ...
}

/**
 * Lap-sized devices
 */
@media screen and (min-width:[width of your choosing]){
    .lap-one-whole{
        width:100%;
    }
    ...
}

And so on…

Now we have a range of classes that only do certain things at certain sizes, tied to our targeted device sizes:

<div class="one-whole  lap-three-fifths  desk-one-half">

N.B. It is widely regarded (and rightly so) that you should not base media queries on device sizes but on ‘what looks best’. This, for the most part, is very true, but we need to think pragmatically. If we want a flexible, recyclable and efficient grid system then we need these absolutes in place.

Because we are writing a grid system to fire at fixed breakpoints it is important to:

  1. Make sure the values you use for your breakpoints aren’t based on actual devices (e.g. iPhone in portrait) but are based on numbers that work best.
  2. Ensure that your grid remains fluid between breakpoints so that even if it doesn’t reformat, it will still resize.

This all comes back to needing to think pragmatically and not trying to be all things to all people; if we want the ability to rapidly create pages with a responsive grid system then we also have to accept that we need to use concrete values.

Lots of classes in the CSS

A problem you may have noticed is that we are now creating a whole new grid system for each of our breakpoints. Now it’s no longer just .one-whole, .one-half, .one-quarter etc, but one of those classes for every media query we have. Initially this seems like a lot, but

This level of repetition will compress incredibly well. We all know we should be gzipping our plain text assets anyway, but it’s more important than ever with things like this. Gzip works best on repeated strings, so the more repeated strings in your code then the better compression ratio you will get. Although repetition in code is bad (usually more maintenance, less efficient etc) it’s actually better in terms of compression. Experimenting with the width classes covered in this blog post, I managed to achieve a compression ratio of 81%. This basically means that the compressed size of the classes is actually less than one fifth its original size over the wire. Five breakpoints worth of classes actually compress to less than one uncompressed one on its own.

That all seemed pretty long winded but it’s an important point that needs making; the flexibility a grid system gives us is worth the extra classes if we’re compressing our CSS.

Implementation

Now we’re at a point where we can start building things! We can construct responsive layouts like:

<div class="content  one-whole  desk-three-quarters">
    ...
</div>
<div class="sub-content  one-whole  desk-one-quarter">
    ...
</div>

That’s a very timid example, but you can see how extensible, flexible and reusable that is. It also allows for incredibly quick development, both prototyping and production.

Recap

So, to let that all sink in, let’s go over what we’re doing…

  • We have a series of breakpoints for our device classifications (palm, desk etc).
  • Each breakpoint’s media query contains its corresponding width classes (e.g. .desk-one-tenth, .palm-two-thirds etc).
  • We have the ability to use those classes in our HTML knowing that they only ever work in their respective media query range.

Lots of classes in the HTML?

A lot of people cringe at the thought of so many classes in their HTML, but get this…

Although we can’t @extend out of media queries, we can @extend into them!

That means that instead of having:

<div class="content  one-whole  desk-three-quarters">

You could just have this Sass:

.content{
    @extend .one-whole;
    @extend .desk-three-quarters;
}

Even better, if you use Sass’ silent classes you could use:

.content{
    @extend %one-whole;
    @extend %desk-three-quarters;
}

This means that the classes won’t get rendered into your CSS at all, and you can avoid using them in your HTML! This means that, if you are using Sass and its silent classes, you will have a class available to you for every width at every breakpoint, but they won’t get created until you call them.

Final word

So there we have it, a fully responsive grid system. All the power of an abstracted grid system with responsiveness baked in.

I have bundled all this up into a micro library called csswizardry-grids. Please feel free to try it and use it.