Chase McCoy wrote a great article about the “gap problem” when creating project grids. His argument can be summarized as follows: How should we use margins in CSS to set element spacing? He pointed out that when used with flexbox, the gap attribute is not perfect enough, for example:
.grid { display: flex; gap: 10px; }
Currently using gap with flexbox is only supported in Firefox, and I have forgotten this in several projects. So pay attention to this.
Anyway, my favorite part of Chase's blog post is the technique he mentioned for Andy Bell to create responsive layouts without media queries, like this:
.grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
The function of this CSS code is as follows:
- Create a grid with 10px gap between columns and rows.
- The minimum width of each column is 150px.
- Each column has the same width (1fr).
- The grid should automatically fill as many columns as possible.
The cleverest thing about all of this is that our grid now effectively implements responsiveness due to minmax
- if the browser is resized, the grid will automatically adjust to a smaller number of columns, like this:
No media inquiry is required at all! Of course, there are several other ways to do this, but I think this approach is clever, not just because we avoid media queries — but because it teaches us to think about designing and building components in a new way.
Chase continued:
With this technique, instead of using breakpoints to specify the screen size that the project should stack, instead specify the minimum size that elements should have before stacking. I like this because it encourages developers to think about responsive design from a behavior rather than screen size perspective.
"Behavior rather than screen size" is an excellent way to think about component design! Many of the problems I have when creating components for design systems are because I have been thinking about screen sizes—mobile devices, tablets, desktops, etc.—and trying to adapt these components to these limitations.
Thinking from a behavioral perspective is always more effective because there are many other factors that affect the component besides the screen or device width we are using. Maybe we want that component to fit another component. Or we want to align some auxiliary text with it for comparison.
Either way, it is actually not entirely possible to think from the perspective of behavior rather than screen size before owning a container query, as Chris wrote:
Container queries are always the top list of improvements required in CSS. The general view is that if we have container queries, we won't write many global media queries based on page size. This is because we are actually trying to control a more local container, and the only reason we are using media queries for this now is that it is the best tool we have in CSS. I totally believe in this.
The above is the detailed content of Thinking in Behaviors, Not Screen Sizes. For more information, please follow other related articles on the PHP Chinese website!

Here's a container with some child elements:

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.