Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 135 additions & 1 deletion docs/css/responsiveness/container-queries.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,135 @@
<ComingSoon />
---
title: "CSS Container Queries"
description: "Learn how to use CSS Container Queries to style components based on the size of their parent container, enabling true component-level responsiveness."
keywords: [CSS Container Queries, '@container', container-type, container-name, component responsiveness, local scoping]
tags: [css, responsiveness, container-queries, '@container', component-design, local-scoping, modern-css, container-type, container-name]
sidebar_label: "Container Queries"
---

Container Queries are a modern, powerful feature in CSS that allows you to apply styles to an element based on the dimensions (size or style) of its nearest ancestor with containment set, rather than the global viewport size.

This is a paradigm shift in responsive design, moving from a page-centric approach to a **component-centric** approach.

<AdsComponent />
<br />

## 1. The Problem Solved

### Media Queries vs. Container Queries

Historically, we relied on **Media Queries** (`@media`).

* **Media Queries:** Apply styles based on the size of the **viewport** (the browser window). If a small card component is placed in a narrow sidebar on a large screen, a media query for a large screen would make the card look bad because it only checks the global size.

**Container Queries** (`@container`) solve this:

* **Container Queries:** Apply styles based on the size of the **parent element** (the container). The card component can now adjust its layout (e.g., stack its elements) when its container is narrow, regardless of whether the user is on a mobile phone or a large desktop monitor.

## 2. Setting Up the Container

Before you can query a container, you must establish it using the `container` or `container-type` property on the parent element.

### 2.1. `container-type` (Required)

This property defines what kind of dimension is being queried.

| Value | Description | Use Case |
| :--- | :--- | :--- |
| **`size`** | Queries both the **width and height**. | Rarely used, as it can cause infinite layout loops. |
| **`inline-size`** | Queries the size in the direction of text flow (usually **width**). | Most common and safest choice for block elements. |
| **`normal`** | The default, no containment established. | |

### 2.2. `container-name` (Optional but Recommended)

For clarity and to prevent ambiguity when containers are nested, you can give your container a name.

<AdsComponent />
<br />

### Setup Shorthand

The `container` shorthand combines both properties:

```css title="styles.css"
/* Applied to the PARENT element */
.sidebar-module {
/* Establishes containment and gives it a name for targeting */
container: card-container / inline-size;
/* Equivalent to:
container-name: card-container;
container-type: inline-size; */
}
```

## 3. The `@container` Rule

Once the parent is defined, you can use the `@container` rule on its children to apply responsive styles.

### 3.1. Syntax

The syntax is similar to a media query, but it uses the container's size instead of the viewport's size.

```css title="styles.css"
@container [container-name] ([query-feature]) {
/* Styles for the child element */
}
```

In the example above:
* **Container Name** (optional) specifies which container to query. If omitted, it queries the nearest ancestor with containment.
* **Query Feature** is the condition based on the container's dimensions (e.g., `min-width: 400px`).

### 3.2. Query Features

The features are size-based and mirror those used in media queries:

* `min-width` / `max-width` (based on container width)
* `min-height` / `max-height` (based on container height)
* `min-inline-size` / `max-inline-size` (most common)

### Example: Component Responsiveness

```css title="styles.css"
/* Styles applied to a child element inside .sidebar-module */
.card-content {
display: flex; /* Default horizontal layout */
gap: 1rem;
}

/* Query the parent named 'card-container' */
@container card-container (max-width: 400px) {
.card-content {
/* When the parent container is narrow, stack the children */
flex-direction: column;
gap: 0.5rem;
}

.card-content img {
/* Shrink the image when the container is small */
width: 100%;
height: auto;
}
}
```

<AdsComponent />
<br />

## 4. Logical Operators

Like media queries, you can combine queries using `and`, `not`, and the comma for `or`.

```css title="styles.css"
@container (min-width: 300px) and (max-width: 500px) {
/* Styles apply only if the container width is between 300px and 500px */
}
```

## Interactive Container Query Demo

Observe how the internal structure of the `Card` component changes when you resize the outer container, regardless of the overall viewport size.

<CodePenEmbed
title="Interactive Container Query Demo"
penId="emZKepL"
/>
131 changes: 130 additions & 1 deletion docs/css/responsiveness/fluid-layouts.mdx
Original file line number Diff line number Diff line change
@@ -1 +1,130 @@
<ComingSoon />
---
title: "Fluid Layouts (Relative Units and Flex/Grid)"
description: "Master the use of relative units, Flexbox, and CSS Grid to create layouts that are inherently flexible and adapt their dimensions based on the viewport and container size."
keywords: [fluid layout, relative units, vw, vh, percentage, flexbox, CSS Grid, intrinsic design, responsive web design]
tags: [fluid layout, relative units, vw, vh, percentage, flexbox, CSS Grid, intrinsic design, responsive web design]
sidebar_label: Fluid Layouts
---

A **fluid layout** (also known as a liquid layout) is one where the widths of the elements are set using relative units (like percentages or viewport units) rather than fixed pixel values. This makes the layout inherently flexible, expanding and contracting smoothly as the browser window resizes, ensuring space is always maximized.

Fluid layouts are a key component of modern responsive design, working hand-in-hand with Media Queries and Container Queries.

<AdsComponent />
<br />

## 1. Core Principle: Avoiding Fixed Units

The goal of a fluid layout is to ensure that no part of the design can cause horizontal scrollbars unless absolutely necessary.

| Unit Type | Behavior | Fluid Use |
| :--- | :--- | :--- |
| **Fixed** (`px`, `pt`) | Stays the same regardless of screen size. | Used only for properties that should *not* change (e.g., border thickness, max-width limits). |
| **Relative** (`%`, `vw`, `em`) | Changes based on the parent, root, or viewport size. | Used for widths, heights, margins, and padding. |

### Using Percentages (`%`)

Percentages are relative to the parent element. This is the oldest and most fundamental fluid unit.

```css title="styles.css"
.parent {
width: 900px; /* The maximum size */
}

.child {
/* This child will always take up 50% of the parent's current width */
width: 50%;
padding: 2%; /* Padding is also relative to the parent's width */
}
```

### Using Viewport Units (`vw`, `vh`)

Viewport units are relative to the size of the browser window itself.

* **`vw` (Viewport Width):** $1\%$ of the viewport width.
* **`vh` (Viewport Height):** $1\%$ of the viewport height.

```css title="styles.css"
.hero-section {
/* Sets the height of the hero section to 60% of the viewport height */
height: 60vh;
}
```

<AdsComponent />
<br />

## 2. Flexbox for Fluid Distribution

Flexbox is ideal for creating one-dimensional (row or column) fluid layouts where content needs to distribute space evenly or according to defined ratios.

### Example: Fluid Navigation Bar

Using `flex-grow` ensures that unused space is distributed evenly among the items.

```css title="styles.css"
.navbar {
display: flex;
justify-content: space-between;
}

.nav-item {
/* Allows the item to grow and shrink as needed */
flex-grow: 1;
text-align: center;
padding: 1rem 0;

/* Setting a base width allows the item to grow proportionally */
flex-basis: 0;
}
```

## 3. CSS Grid for Two-Dimensional Fluidity

CSS Grid is the ultimate tool for two-dimensional fluid layouts, especially when dealing with major page sections (header, sidebar, main content).

### The `fr` Unit

The `fr` (fractional unit) is the most powerful tool for fluid Grid design. It represents a fraction of the available space in the grid container.

### Example: Fluid Sidebar Layout

```css title="styles.css"
.page-layout {
display: grid;
padding: 20px;
/* Define two columns: one takes 1 fraction, the main content takes 3 fractions */
grid-template-columns: 1fr 3fr;
gap: 20px;
}
```

In this example, the sidebar will always be one-quarter ($1/4$) of the total available width, and the main content will be three-quarters ($3/4$), ensuring the layout is always perfectly proportional, regardless of the screen size.

<AdsComponent />
<br />

### `minmax()` Function

The `minmax()` function prevents columns from becoming too narrow or too wide. This combines fluid width with fixed limits, creating a highly resilient design.

```css title="styles.css"
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}
```

* **Explanation:** This creates columns that are *at least* 300px wide (`minmax(300px, 1fr)`). If the remaining space allows for more than one column, they are created. The `1fr` ensures they all share the available space equally, but they will never shrink below 300px.

## Interactive Fluid Layout Demo

This demo shows a two-column layout using the `fr` unit. Resize the pane to see the columns maintain their 1:2 ratio.

<CodePenEmbed
title="Interactive Fluid Layout Demo"
penId="wBGXpPN"
/>

In this example, the sidebar and main content areas adjust their widths fluidly based on the viewport size, maintaining their proportional relationship while ensuring usability across devices.
Loading