Skip to content

chore: update README with container queries usage and examples #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ A collection of tips to help take your CSS skills pro.
1. [Set `display: none` on Line Breaks Used as Spacing](#set-display-none-on-line-breaks-used-as-spacing)
1. [Use `:empty` to Hide Empty HTML Elements](#use-empty-to-hide-empty-html-elements)
1. [Use `margin-inline` instead of `margin`](#use-margin-inline-instead-of-margin)
1. [Use Container Queries for Component Scoped Responsiveness](#use-containerqueries-for-component-scoped-responsiveness)

### Use a CSS Reset

Expand Down Expand Up @@ -698,6 +699,62 @@ The same can be done for `margin-block` with defines the block start and end mar

<sup>[Back to top](#contents)</sup>

### Use Container Queries for Component Scoped Responsiveness

When you make an element a “query container,” anything inside it (headings, paragraphs, etc.) becomes sensitive to that container’s own size. In plain terms:

- The container is the box you’ve marked with `container-type`.

- Its child elements are simply the content within that box.

- Your `@container` rules watch the container’s width and/or height and then apply new styles to those children (or to the container itself) once the box crosses the breakpoints you’ve defined.

#### 1. Establish a container

```css
.card {
/* Enable width-based container queries */
container-type: inline-size;

/* Or track both width & height */
/* Use 'size' to enable queries based on width, height, or both */
/* container-type: size; */

/* Optional: name the container to target it explicitly in queries */
container-name: card;
}
```

#### 2. Write your @container rule
```css
/* Query a named container by width */
@container card-width (min-width: 400px) {
.card-width__title {
font-size: 1.5rem;
color: royalblue;
}
}

/* Query a named container by height */
@container card-height (min-height: 200px) {
.card-height__content {
padding: 1.5rem;
background: lightcoral;
}
}

/* Query for both width and height on the same container */
@container card-both (min-width: 400px) and (min-height: 200px) {
.card-both {
border-color: darkgreen;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
}
```
#### [Demo](https://codepen.io/mohan-dand/pen/yyyjrdG)

<sup>[Back to top](#contents)</sup>

## Translations

> [!NOTE]
Expand Down