-
Notifications
You must be signed in to change notification settings - Fork 23.2k
Ff151 CSSContainerRules.conditions docs #43995
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
hamishwillee
wants to merge
11
commits into
mdn:main
Choose a base branch
from
hamishwillee:ff151_csscontainerrule_conditions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+825
−134
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f15e1b6
FF15l CSSContainerRule.conditions docs
hamishwillee f391f57
CSSContainerRule - fix up the other properties
hamishwillee 4e8341c
easy code review fixes 1
hamishwillee 4f2e5dc
code review easy suggestions 2
hamishwillee 00d5eea
code review suggestions 3
hamishwillee cf4e334
code review 4
hamishwillee ece2568
Apply suggestions from code review
hamishwillee 1ebc518
properties move text into description
hamishwillee e3980e5
typos
hamishwillee f46b78d
Apply suggestions from code review
hamishwillee ecc05b8
Update files/en-us/web/api/csscontainerrule/index.md
hamishwillee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
files/en-us/web/api/csscontainerrule/conditions/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,167 @@ | ||||||
| --- | ||||||
| title: "CSSContainerRule: conditions property" | ||||||
| short-title: conditions | ||||||
| slug: Web/API/CSSContainerRule/conditions | ||||||
| page-type: web-api-instance-property | ||||||
| browser-compat: api.CSSContainerRule.conditions | ||||||
| --- | ||||||
|
|
||||||
| {{ APIRef("CSSOM") }} | ||||||
|
|
||||||
| The read-only **`conditions`** property of the {{domxref("CSSContainerRule")}} interface represents an associated CSS {{cssxref("@container")}} at-rule as an array of objects, where each object represents a single container condition. | ||||||
|
|
||||||
| ## Value | ||||||
|
|
||||||
| An array of objects where each object has the form: | ||||||
|
|
||||||
| ```js | ||||||
| { name: "<container-name>", query: "<container-query>" }; | ||||||
| ``` | ||||||
|
|
||||||
| Either the `name` or `query` may be the empty string, but not both. | ||||||
|
|
||||||
| ## Description | ||||||
|
|
||||||
| The **`conditions`** property represents an associated CSS {{cssxref("@container")}} at-rule as an array of objects. | ||||||
|
|
||||||
| Each object represents a container condition as a `name` string property and a `query` string property, either of which may be the empty string if not defined. | ||||||
| The `name` represents the name of a container, and the `query` string represents the set of feature tests that must be true for the particular container condition to match. | ||||||
|
|
||||||
| For example, given the following {{cssxref("@container")}}: | ||||||
|
|
||||||
| ```css | ||||||
| @container sidebar (width >= 700px), (height >= 400px) { | ||||||
| /* Styles */ | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| The `conditions` would be an array like this: | ||||||
|
|
||||||
| ```js | ||||||
| [ | ||||||
| { name: "sidebar", query: "(width >= 700px)" }, | ||||||
| { name: "", query: "(height >= 400px)" }, | ||||||
| ]; | ||||||
| ``` | ||||||
|
|
||||||
| ## Examples | ||||||
|
|
||||||
| See also [Examples](/en-US/docs/Web/API/CSSContainerRule#examples) in `CSSContainerRule`. | ||||||
|
|
||||||
| ### Basic usage | ||||||
|
|
||||||
| The example shows how multiple container conditions are represented in the `conditions` property. | ||||||
|
|
||||||
| Note that we've hidden the logging code, as it is not relevant. | ||||||
|
|
||||||
| ```html hidden | ||||||
| <pre id="log"></pre> | ||||||
| ``` | ||||||
|
|
||||||
| ```js hidden | ||||||
| const logElement = document.querySelector("#log"); | ||||||
| function log(text) { | ||||||
| logElement.innerText = `${logElement.innerText}${text}\n`; | ||||||
| logElement.scrollTop = logElement.scrollHeight; | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ```css hidden | ||||||
| #log { | ||||||
| height: 100px; | ||||||
| overflow: scroll; | ||||||
| padding: 0.5rem; | ||||||
| border: 1px solid black; | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| #### HTML | ||||||
|
|
||||||
| First, we define the HTML for a `card` contained within a `post`. | ||||||
| These are represented by two nested {{htmlelement("div")}} elements. | ||||||
|
|
||||||
| ```html | ||||||
| <div class="post"> | ||||||
| <div class="card"> | ||||||
| <h2>Card title</h2> | ||||||
| <p>Card content</p> | ||||||
| </div> | ||||||
| </div> | ||||||
| ``` | ||||||
|
|
||||||
| #### CSS | ||||||
|
|
||||||
| The CSS for the container element specifies the type of the container, and may also specify a name. | ||||||
| The card has a default font size, which is overridden when it is contained inside a `sidebar` `@container` when its width is greater than or equal to `700px`, or when it is inside a container named `other-name`. | ||||||
| Note that this condition is contrived to demonstrate how multiple conditions are represented (`other-name` doesn't actually do anything). | ||||||
|
|
||||||
| ```html | ||||||
| <style id="example-styles"> | ||||||
| .post { | ||||||
| container-type: inline-size; | ||||||
| container-name: sidebar; | ||||||
| } | ||||||
|
|
||||||
| /* Default heading styles for the card title */ | ||||||
| .card h2 { | ||||||
| font-size: 1em; | ||||||
| } | ||||||
|
|
||||||
| @container sidebar (width >= 700px), other-name { | ||||||
| .card { | ||||||
| font-size: 2em; | ||||||
| } | ||||||
| } | ||||||
| </style> | ||||||
| ``` | ||||||
|
|
||||||
| #### JavaScript | ||||||
|
|
||||||
| The code below gets the {{domxref("HTMLStyleElement")}} associated with the example using its `id`, and then uses its `sheet` property to get the {{domxref("StyleSheet")}}. | ||||||
| From the `StyleSheet` we get the set of `cssRules` added to the sheet. | ||||||
| Since we added the `@container` as the third rule above, we can access the associated `CSSContainerRule` using the third entry (index "2") in the `cssRules`. | ||||||
|
|
||||||
| ```js | ||||||
| const exampleStylesheet = document.getElementById("example-styles").sheet; | ||||||
| const exampleRules = exampleStylesheet.cssRules; | ||||||
| const containerRule = exampleRules[2]; // a CSSContainerRule representing the container rule. | ||||||
| ``` | ||||||
|
|
||||||
| We then use the `containerRule` to log the value of the `conditions` property. | ||||||
|
|
||||||
| ```js | ||||||
| if ("conditions" in CSSContainerRule.prototype) { | ||||||
| log("CSSContainerRule.conditions:"); | ||||||
| containerRule.conditions.forEach((item) => { | ||||||
| const jsonString = JSON.stringify(item); | ||||||
| log(` ${jsonString}`); | ||||||
| }); | ||||||
| } else { | ||||||
| log("CSSContainerRule.conditions is not supported."); | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| > [!NOTE] | ||||||
| > On browsers that don't support `conditions`, you may be able to use {{domxref("CSSContainerRule.containerName")}} and {{domxref("CSSContainerRule.containerQuery")}}, provided that the `@container` only specifies one container condition. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| > For more information, see the [Feature testing](/en-US/docs/Web/API/CSSContainerRule#feature_testing) example in `CSSContainerRule`. | ||||||
|
|
||||||
| #### Results | ||||||
|
|
||||||
| The example output is shown below. | ||||||
|
|
||||||
| {{EmbedLiveSample("Basic usage","100%","300px")}} | ||||||
|
|
||||||
| ## Specifications | ||||||
|
|
||||||
| {{Specifications}} | ||||||
|
|
||||||
| ## Browser compatibility | ||||||
|
|
||||||
| {{Compat}} | ||||||
|
|
||||||
| ## See also | ||||||
|
|
||||||
| - CSS {{cssxref("container")}} shorthand property | ||||||
| - [CSS containment module](/en-US/docs/Web/CSS/Guides/Containment) | ||||||
| - [Container queries](/en-US/docs/Web/CSS/Guides/Containment/Container_queries) | ||||||
| - [Using container size and style queries](/en-US/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries) | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.