[CSS3] Container query
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries
Use the container-type
property a value of size
, inline-size
, or normal
. These values have the following effects:
size
: the query will be based on the inline and block dimensions of the container. Affects both Width and Height.inline-size
: the query will be based on the inline dimensions of the container. Affects only Widthnormal
: The element is not a query container for any container size queries, but remains a query container for container style queries.
.container {
container-type: inline-size;
}
Once a containment context is created, you can use the @container
at-rule to write a container query.
/* Default heading styles for the card title */
.card h2 {
font-size: 1em;
}
/* Container query applied if the container is larger than 700px */
@container (min-width: 700px) {
.card h2 {
font-size: 2em;
}
}
It's possible to give a containment context a name using the container-name
property. Once named, the name can be used in a @container
query so as to target a specific container. The following example creates a containment context with the name sidebar
:
.container {
container-type: inline-size;
container-name: sidebar;
}
@container sidebar (min-width: 700px) {
.card {
display: grid;
grid-template-columns: 2fr 1fr;
}
}
The shorthand way of declaring a containment context is to use the container
property:
.container {
container: sidebar / inline-size;
}
FireFox doesn't support container query at the memont, you can use polyfill:
https://cdn.jsdelivr.net/npm/container-query-polyfill@1/dist/container-query-polyfill.modern.js
Tailwind has container query support plugin:
https://github.com/tailwindlabs/tailwindcss-container-queries
tailwind.config.js
module.exports = {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [require("@tailwindcss/container-queries")],
};
Example:
import "./App.css";
import movies from "./movies.json";
function Card({ image, title, subtitle, description }) {
return (
<div className="@xl:grid @xl:grid-cols-[1fr_3fr] mb-2">
<div
className="bg-no-repeat bg-cover bg-center h-64 min-h-full rounded-t-3xl @xl:rounded-tr-none @xl:rounded-l-3xl"
style={{
backgroundImage: `url(${image})`,
}}
/>
<div className="p-2">
<h2 className="mb-2 text-3xl font-bold">{title}</h2>
<h4 className="mb-2 italic">{subtitle}</h4>
<p className="hidden @xl:block">{description}</p>
</div>
</div>
);
}
function App() {
return (
<div className="max-w-5xl mx-auto @container">
<div className="@4xl:grid @4xl:grid-cols-[3fr_1fr]">
<main className="@container">
<Card {...movies[0]} />
<Card {...movies[1]} />
<Card {...movies[2]} />
</main>
<article className="@container">
<Card {...movies[3]} />
<Card {...movies[2]} />
<Card {...movies[1]} />
</article>
</div>
</div>
);
}
export default App;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-11-29 [Java Srping] @RestController and @Controller
2020-11-29 [Java Spring] Testing a view controller
2019-11-29 [Algorithm] BFS vs DFS
2017-11-29 [ES2017] Iterate over properties of an object with ES2017 Object.entries()
2016-11-29 [Elm] Installing and setting up Elm
2016-11-29 [Node.js] Use nodejs-dashboard event loop delay with hrtime()
2015-11-29 [Javascript] Introducing Reduce: Common Patterns