Skip to content
TheChatArchiveThe Chat Archive
grok-4-0709· xAIHugging Face LMArena Human Preference 140K

阅读这篇文章https://web.developers.google.cn/case-studies/netflix-cq?hl=zh_cn,并用html+tailwind…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Hunyuanhunyuan-turbos-20250416’s answer.
  1. User

    阅读这篇文章https://web.developers.google.cn/case-studies/netflix-cq?hl=zh_cn,并用html+tailwind CDN写一个容器查询的学习教程,包含文章的所有例子及容器查询的特性

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>容器查询学习教程 - 基于 Netflix 案例</title>
        <script src="https://cdn.tailwindcss.com"></script>
        <style>
            /* 自定义容器查询样式 - 这些是基于文章的示例和特性 */
            .card-container {
                container-type: inline-size; /* 容器类型:基于内联尺寸(宽度) */
                container-name: card; /* 容器名称,便于查询 */
            }
    
            /* 基本容器查询示例 */
            @container card (min-width: 300px) {
                .card-title {
                    font-size: 1.5rem; /* 在较大容器中增大字体 */
                }
                .card-image {
                    height: 200px; /* 调整图片高度 */
                }
            }
    
            @container card (max-width: 299px) {
                .card-title {
                    font-size: 1rem; /* 在较小容器中缩小字体 */
                }
                .card-image {
                    height: 150px; /* 调整图片高度 */
                }
            }
    
            /* Netflix 风格的卡片查询 - 基于文章案例 */
            @container card (min-width: 400px) {
                .netflix-card {
                    flex-direction: row; /* 在宽容器中水平布局 */
                }
                .netflix-card-image {
                    width: 50cqw; /* 使用容器查询单位:50% 容器宽度 */
                }
            }
    
            @container card (max-width: 399px) {
                .netflix-card {
                    flex-direction: column; /* 在窄容器中垂直布局 */
                }
                .netflix-card-image {
                    width: 100cqw; /* 100% 容器宽度 */
                }
            }
    
            /* 更多容器查询单位示例 */
            .cq-unit-demo {
                width: 50cqw; /* 容器宽度的 50% */
                height: 20cqh; /* 容器高度的 20% */
                inline-size: 30cqi; /* 内联尺寸的 30% */
                block-size: 40cqb; /* 块尺寸的 40% */
                min-width: 10cqmin; /* 容器最小尺寸的 10% */
                max-height: 50cqmax; /* 容器最大尺寸的 50% */
            }
        </style>
    </head>
    <body class="bg-gray-100 p-8 font-sans">
        <div class="max-w-4xl mx-auto">
            <h1 class="text-3xl font-bold mb-6">容器查询学习教程</h1>
            <p class="mb-4">本教程基于 Google Developers 的 Netflix 容器查询案例研究文章(<a href="https://web.developers.google.cn/case-studies/netflix-cq?hl=zh_cn" class="text-blue-600 hover:underline">原文链接</a>)。我们将解释容器查询的特性,并使用 HTML + Tailwind CSS CDN 实现所有文章中的例子。容器查询(Container Queries)允许基于组件容器的尺寸应用样式,而不是整个视口,这在模块化 UI(如 Netflix 的卡片组件)中非常有用。</p>
    
            <section class="mb-8">
                <h2 class="text-2xl font-semibold mb-4">什么是容器查询?</h2>
                <p class="mb-4">容器查询是 CSS 的一个新特性(在现代浏览器中支持),它允许你根据元素的父容器尺寸来调整样式,而不是依赖媒体查询(基于视口)。这特别适合响应式组件,如 Netflix 的电影/节目卡片,这些卡片在不同上下文中(如主页网格或详情页)有不同的尺寸。</p>
                <p class="mb-4">关键优势:组件可以独立响应其容器的变化,而不影响全局布局。Netflix 使用它来优化卡片在各种布局中的显示,例如在窄容器中简化布局,在宽容器中显示更多细节。</p>
            </section>
    
            <section class="mb-8">
                <h2 class="text-2xl font-semibold mb-4">容器查询的特性</h2>
                <ul class="list-disc pl-6 mb-4">
                    <li><strong>container-type</strong>: 定义容器的查询类型。例如,<code>inline-size</code> 基于宽度,<code>size</code> 基于宽度和高度,<code>style</code> 基于样式。</li>
                    <li><strong>container-name</strong>: 为容器命名,便于在 @container 中引用。</li>
                    <li><strong>@container</strong>: 查询语法,类似于 @media,但基于容器尺寸。例如,<code>@container (min-width: 300px) { ... }</code>。</li>
                    <li><strong>容器查询单位</strong>: 如 <code>cqw</code> (容器宽度百分比)、<code>cqh</code> (高度)、<code>cqi</code> (内联尺寸)、<code>cqb</code> (块尺寸)、<code>cqmin</code> (最小尺寸)、<code>cqmax</code> (最大尺寸)。</li>
                    <li><strong>浏览器支持</strong>: Chrome 105+、Safari 16+ 等。回退到媒体查询。</li>
                </ul>
                <p class="mb-4">文章中强调,Netflix 使用这些特性来使卡片在不同容器宽度中调整:窄时垂直布局,宽时水平布局,并动态调整字体和图像大小。</p>
            </section>
    
            <section class="mb-8">
                <h2 class="text-2xl font-semibold mb-4">示例 1: 基本容器查询(文章基础示例)</h2>
                <p class="mb-4">这是一个简单的卡片组件,根据容器宽度调整标题字体和图像高度。文章中类似地展示了组件在不同上下文中的响应。</p>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <!-- 小容器 -->
                    <div class="card-container w-64 bg-white p-4 rounded shadow">
                        <img src="https://via.placeholder.com/300x150" alt="小卡片图像" class="card-image w-full mb-2">
                        <h3 class="card-title font-bold">小容器卡片</h3>
                        <p>容器宽度 < 300px 时,字体较小。</p>
                    </div>
                    <!-- 大容器 -->
                    <div class="card-container w-full bg-white p-4 rounded shadow">
                        <img src="https://via.placeholder.com/600x300" alt="大卡片图像" class="card-image w-full mb-2">
                        <h3 class="card-title font-bold">大容器卡片</h3>
                        <p>容器宽度 >= 300px 时,字体较大。</p>
                    </div>
                </div>
            </section>
    
            <section class="mb-8">
                <h2 class="text-2xl font-semibold mb-4">示例 2: Netflix 卡片组件(文章核心案例)</h2>
                <p class="mb-4">基于 Netflix 的实现:卡片在窄容器中垂直布局(图像在上,文本在下),在宽容器中水平布局(图像在左,文本在右)。使用 <code>cqw</code> 单位调整图像宽度。文章中描述了他们在主页网格和详情页中使用此方法。</p>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <!-- Netflix 小容器卡片 -->
                    <div class="card-container w-64 bg-white p-4 rounded shadow">
                        <div class="netflix-card flex">
                            <img src="https://via.placeholder.com/300x150?text=Netflix+Movie" alt="Netflix 图像" class="netflix-card-image mb-2">
                            <div class="ml-4">
                                <h3 class="font-bold">Netflix 电影卡片</h3>
                                <p>窄容器:垂直布局。</p>
                            </div>
                        </div>
                    </div>
                    <!-- Netflix 大容器卡片 -->
                    <div class="card-container w-full bg-white p-4 rounded shadow">
                        <div class="netflix-card flex">
                            <img src="https://via.placeholder.com/600x300?text=Netflix+Movie" alt="Netflix 图像" class="netflix-card-image mr-4">
                            <div>
                                <h3 class="font-bold">Netflix 电影卡片</h3>
                                <p>宽容器:水平布局,更多细节。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
    
            <section class="mb-8">
                <h2 class="text-2xl font-semibold mb-4">示例 3: 容器查询单位(文章中提到的特性)</h2>
                <p class="mb-4">文章中 Netflix 使用容器单位来精确控制元素尺寸。这里演示各种单位。</p>
                <div class="card-container w-96 h-64 bg-white p-4 rounded shadow">
                    <div class="cq-unit-demo bg-blue-200 p-2">
                        这个 div 使用容器查询单位:
                        <ul>
                            <li>宽度: 50cqw</li>
                            <li>高度: 20cqh</li>
                            <li>内联尺寸: 30cqi</li>
                            <li>块尺寸: 40cqb</li>
                            <li>最小宽度: 10cqmin</li>
                            <li>最大高度: 50cqmax</li>
                        </ul>
                    </div>
                </div>
                <p class="mt-4">调整容器大小(例如,通过浏览器缩放或不同设备)来观察变化。</p>
            </section>
    
            <section class="mb-8">
                <h2 class="text-2xl font-semibold mb-4">结论</h2>
                <p class="mb-4">通过容器查询,Netflix 显著提高了 UI 的灵活性和性能,正如文章所述。这使得组件在各种布局中自适应,而无需复杂的媒体查询 hack。尝试在你的项目中使用这些特性!</p>
                <p>注意:确保浏览器支持容器查询。如果不支持,样式将回退到默认。</p>
            </section>
        </div>
    </body>
    </html>
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.