repo_id
stringlengths
22
103
file_path
stringlengths
41
147
content
stringlengths
181
193k
__index_level_0__
int64
0
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/animation-delay/index.md
--- title: animation-delay slug: Web/CSS/animation-delay page-type: css-property browser-compat: css.properties.animation-delay --- {{CSSRef}} The **`animation-delay`** [CSS](/en-US/docs/Web/CSS) property specifies the amount of time to wait from applying the animation to an element before beginning to perform the animation. The animation can start later, immediately from its beginning, or immediately and partway through the animation. {{EmbedInteractiveExample("pages/css/animation-delay.html")}} It is often convenient to use the shorthand property {{cssxref("animation")}} to set all animation properties at once. ## Syntax ```css /* Single animation */ animation-delay: 3s; animation-delay: 0s; animation-delay: -1500ms; /* Multiple animations */ animation-delay: 2.1s, 480ms; /* Global values */ animation-delay: inherit; animation-delay: initial; animation-delay: revert; animation-delay: revert-layer; animation-delay: unset; ``` ### Values - `{{cssxref("&lt;time&gt;")}}` - : The time offset, from the moment at which the animation is applied to the element, at which the animation should begin. This may be specified in either seconds (`s`) or milliseconds (`ms`). The unit is required. A positive value indicates that the animation should begin after the specified amount of time has elapsed. A value of `0s`, which is the default, indicates that the animation should begin as soon as it's applied. A negative value causes the animation to begin immediately, but partway through its cycle. For example, if you specify `-1s` as the animation delay time, the animation will begin immediately but will start 1 second into the animation sequence. If you specify a negative value for the animation delay, but the starting value is implicit, the starting value is taken from the moment the animation is applied to the element. > **Note:** When you specify multiple comma-separated values on an `animation-*` property, they are applied to the animations in the order in which the {{cssxref("animation-name")}}s appear. For situations where the number of animations and `animation-*` property values do not match, see [Setting multiple animation property values](/en-US/docs/Web/CSS/CSS_animations/Using_CSS_animations#setting_multiple_animation_property_values). > **Note:** `animation-delay` has no effect on [CSS scroll-driven animations](/en-US/docs/Web/CSS/CSS_scroll-driven_animations). ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting an animation delay This animation has a delay of 2 seconds. #### HTML ```html <div class="box"></div> ``` #### CSS ```css .box { background-color: rebeccapurple; border-radius: 10px; width: 100px; height: 100px; } .box:hover { animation-name: rotate; animation-duration: 0.7s; animation-delay: 2s; } @keyframes rotate { 0% { transform: rotate(0); } 100% { transform: rotate(360deg); } } ``` #### Result Hover over the rectangle to start the animation. {{EmbedLiveSample("Setting an animation delay","100%","250")}} See [CSS animations](/en-US/docs/Web/CSS/CSS_animations/Using_CSS_animations) for examples. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS animations](/en-US/docs/Web/CSS/CSS_animations/Using_CSS_animations) - JavaScript {{domxref("AnimationEvent")}} API - Other related animation properties: {{cssxref("animation")}}, {{cssxref("animation-composition")}}, {{cssxref("animation-direction")}}, {{cssxref("animation-duration")}}, {{cssxref("animation-fill-mode")}}, {{cssxref("animation-iteration-count")}}, {{cssxref("animation-name")}}, {{cssxref("animation-play-state")}}, {{cssxref("animation-timeline")}}, {{cssxref("animation-timing-function")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/flex-basis/index.md
--- title: flex-basis slug: Web/CSS/flex-basis page-type: css-property browser-compat: css.properties.flex-basis --- {{CSSRef}} The **`flex-basis`** [CSS](/en-US/docs/Web/CSS) property sets the initial main size of a flex item. It sets the size of the content box unless otherwise set with {{Cssxref("box-sizing")}}. {{EmbedInteractiveExample("pages/css/flex-basis.html")}} In this example, the {{cssxref("flex-grow")}} and {{cssxref("flex-shrink")}} properties are both set to `1` on all three items, indicating that the flex item can grow and shrink from the initial `flex-basis`. The demo then changes the `flex-basis` on the first item. It will then grow and shrink from that flex-basis. This means that, for example, when the `flex-basis` of the first item is `200px`, it will start out at 200px but then shrink to fit the space available with the other items being at least `min-content` sized. The image below shows how the Firefox [Flexbox Inspector](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_flexbox_layouts/index.html) helps you understand the size items become: ![The Firefox Flexbox Inspector showing the size of the item once it has shrunk.](firefox-flex-basis.png) > **Note:** In case both `flex-basis` (other than `auto`) and `width` (or `height` in case of `flex-direction: column`) are set for an element, `flex-basis` has priority. ## Syntax ```css /* Specify <'width'> */ flex-basis: 10em; flex-basis: 3px; flex-basis: 50%; flex-basis: auto; /* Intrinsic sizing keywords */ flex-basis: max-content; flex-basis: min-content; flex-basis: fit-content; /* Automatically size based on the flex item's content */ flex-basis: content; /* Global values */ flex-basis: inherit; flex-basis: initial; flex-basis: revert; flex-basis: revert-layer; flex-basis: unset; ``` The `flex-basis` property is specified as either the keyword `content` or a `<'width'>`. ### Values - `<'width'>` - : Any of the following units: - {{cssxref("&lt;length&gt;")}} sets an absolute value - {{cssxref("&lt;percentage&gt;")}} sets a percentage of the width or height of a containing block's content area - `auto` uses the value of the [width](https://drafts.csswg.org/css2/#the-width-property) in horizontal writing mode, and the value of the [height](https://drafts.csswg.org/css2/#the-height-property) in vertical writing mode; when the corresponding value is also `auto`, the `content` value is used instead - `max-content` sets the intrinsic preferred width - `min-content` sets the intrinsic minimum width - `fit-content` sets the maximum possible size of a containing block's content area, bounded by the `min-content` and `max-content` values, and calculated based on the content of the current element - `content` - : Indicates automatic sizing, based on the flex item's content. > **Note:** This value was not present in the initial release of Flexible Box Layout, and thus some older implementations will not support it. The equivalent effect can be had by using `auto` together with a main size ([width](https://drafts.csswg.org/css2/#the-width-property) or [height](https://drafts.csswg.org/css2/#the-height-property)) of `auto`. > > - Originally, `flex-basis:auto` meant "look at my `width` or `height` property". > - Then, `flex-basis:auto` was changed to mean automatic sizing, and "main-size" was introduced as the "look at my `width` or `height` property" keyword. It was implemented in [Firefox bug 1032922](https://bugzil.la/1032922). > - Then, that change was reverted in [Firefox bug 1093316](https://bugzil.la/1093316), so `auto` once again means "look at my `width` or `height` property"; and a new `content` keyword is being introduced to trigger automatic sizing. ([Firefox bug 1105111](https://bugzil.la/1105111) covers adding that keyword). ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting flex item initial sizes #### HTML ```html <ul class="container"> <li class="flex flex1">1: flex-basis test</li> <li class="flex flex2">2: flex-basis test</li> <li class="flex flex3">3: flex-basis test</li> <li class="flex flex4">4: flex-basis test</li> <li class="flex flex5">5: flex-basis test</li> </ul> <ul class="container"> <li class="flex flex6">6: flex-basis test</li> </ul> ``` #### CSS ```css .container { font-family: arial, sans-serif; margin: 0; padding: 0; list-style-type: none; display: flex; flex-wrap: wrap; } .flex { background: #6ab6d8; padding: 10px; margin-bottom: 50px; border: 3px solid #2e86bb; color: white; font-size: 14px; text-align: center; position: relative; } .flex::after { position: absolute; z-index: 1; left: 0; top: 100%; margin-top: 10px; width: 100%; color: #333; font-size: 12px; } .flex1 { flex-basis: auto; } .flex1::after { content: "auto"; } .flex2 { flex-basis: max-content; } .flex2::after { content: "max-content"; } .flex3 { flex-basis: min-content; } .flex3::after { content: "min-content"; } .flex4 { flex-basis: fit-content; } .flex4::after { content: "fit-content"; } .flex5 { flex-basis: content; } .flex5::after { content: "content"; } ``` #### Results {{EmbedLiveSample('Setting_flex_item_initial_sizes', '', '360')}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - CSS Flexbox Guide: _[Basic Concepts of Flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox)_ - CSS Flexbox Guide: _[Controlling Ratios of flex items along the main axis](/en-US/docs/Web/CSS/CSS_flexible_box_layout/Controlling_ratios_of_flex_items_along_the_main_axis)_ - {{cssxref("width")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/background-position-y/index.md
--- title: background-position-y slug: Web/CSS/background-position-y page-type: css-property browser-compat: css.properties.background-position-y --- {{CSSRef}} The **`background-position-y`** [CSS](/en-US/docs/Web/CSS) property sets the initial vertical position for each background image. The position is relative to the position layer set by {{cssxref("background-origin")}}. {{EmbedInteractiveExample("pages/css/background-position-y.html")}} <!-- The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request. --> The value of this property is overridden by any declaration of the {{cssxref("background")}} or {{cssxref("background-position")}} shorthand properties applied to the element after it. ## Syntax ```css /* Keyword values */ background-position-y: top; background-position-y: center; background-position-y: bottom; /* <percentage> values */ background-position-y: 25%; /* <length> values */ background-position-y: 0px; background-position-y: 1cm; background-position-y: 8em; /* Side-relative values */ background-position-y: bottom 3px; background-position-y: bottom 10%; /* Multiple values */ background-position-y: 0px, center; /* Global values */ background-position-y: inherit; background-position-y: initial; background-position-y: revert; background-position-y: revert-layer; background-position-y: unset; ``` The `background-position-y` property is specified as one or more values, separated by commas. ### Values - `top` - : Aligns the top edge of the background image with the top edge of the background position layer. - `center` - : Aligns the vertical center of the background image with the vertical center of the background position layer. - `bottom` - : Aligns the bottom edge of the background image with the bottom edge of the background position layer. - {{cssxref("&lt;length&gt;")}} - : The offset of the given background image's horizontal edge from the corresponding background position layer's top horizontal edge. (Some browsers allow assigning the bottom edge for offset). - {{cssxref("&lt;percentage&gt;")}} - : The offset of the given background image's vertical position relative to the container. A value of 0% means that the top edge of the background image is aligned with the top edge of the container, and a value of 100% means that the _bottom_ edge of the background image is aligned with the _bottom_ edge of the container, thus a value of 50% vertically centers the background image. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Basic example The following example shows a simple background image implementation, with background-position-x and background-position-y used to define the image's horizontal and vertical positions separately. #### HTML ```html <div></div> ``` #### CSS ```css div { width: 300px; height: 300px; background-color: skyblue; background-image: url(https://mdn.dev/archives/media/attachments/2020/07/29/17350/3b4892b7e820122ac6dd7678891d4507/firefox.png); background-repeat: no-repeat; background-position-x: center; background-position-y: bottom; } ``` #### Result {{EmbedLiveSample('Basic_example', '100%', 300)}} ### Side-relative values The following example shows support for side-relative offset syntax, which allows the developer to offset the background from any edge. #### HTML ```html <div></div> ``` #### CSS ```css div { width: 300px; height: 300px; background-color: seagreen; background-image: url(https://mdn.dev/archives/media/attachments/2020/07/29/17350/3b4892b7e820122ac6dd7678891d4507/firefox.png); background-repeat: no-repeat; background-position-x: right 20px; background-position-y: bottom 10px; } ``` #### Result {{EmbedLiveSample('Side-relative_values', '100%', 300)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("background-position")}} - {{cssxref("background-position-x")}} - [Using multiple backgrounds](/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Using_multiple_backgrounds)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/column-rule-style/index.md
--- title: column-rule-style slug: Web/CSS/column-rule-style page-type: css-property browser-compat: css.properties.column-rule-style --- {{CSSRef}} The **`column-rule-style`** [CSS](/en-US/docs/Web/CSS) property sets the style of the line drawn between columns in a multi-column layout. {{EmbedInteractiveExample("pages/css/column-rule-style.html")}} ## Syntax ```css /* <'border-style'> values */ column-rule-style: none; column-rule-style: hidden; column-rule-style: dotted; column-rule-style: dashed; column-rule-style: solid; column-rule-style: double; column-rule-style: groove; column-rule-style: ridge; column-rule-style: inset; column-rule-style: outset; /* Global values */ column-rule-style: inherit; column-rule-style: initial; column-rule-style: revert; column-rule-style: revert-layer; column-rule-style: unset; ``` The `column-rule-style` property is specified as a single `<'border-style'>` value. ### Values - `<'border-style'>` - : Is a keyword defined by {{ cssxref("border-style") }} describing the style of the rule. The styling must be interpreted as in the collapsing border model. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting a dashed column rule #### HTML ```html <p> This is a bunch of text split into three columns. The `column-rule-style` property is used to change the style of the line that is drawn between columns. Don't you think that's wonderful? </p> ``` #### CSS ```css p { column-count: 3; column-rule-style: dashed; } ``` #### Result {{ EmbedLiveSample('Setting_a_dashed_column_rule') }} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Multiple-column Layout](/en-US/docs/Learn/CSS/CSS_layout/Multiple-column_Layout) - {{CSSXref("column-rule")}} - {{CSSXref("column-rule-width")}} - {{CSSXref("column-rule-color")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_colon_popover-open/index.md
--- title: ":popover-open" slug: Web/CSS/:popover-open page-type: css-pseudo-class browser-compat: css.selectors.popover-open --- {{CSSRef}} The **`:popover-open`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) represents a {{domxref("Popover API", "popover", "", "nocode")}} element (i.e. one with a [`popover` attribute](/en-US/docs/Web/HTML/Global_attributes/popover)) that is in the showing state. You can use this to apply style to popover elements only when they are shown. ## Syntax ```css :popover-open { /* ... */ } ``` ## Examples By default, popovers appear in the middle of the viewport. The default styling is achieved like this in the UA stylesheet: ```css [popover] { position: fixed; inset: 0; width: fit-content; height: fit-content; margin: auto; border: solid; padding: 0.25em; overflow: auto; color: CanvasText; background-color: Canvas; } ``` To override the default styles and get the popover to appear somewhere else on your viewport, you could need to override the above styles with something like this: ```css :popover-open { width: 200px; height: 100px; position: absolute; inset: unset; bottom: 5px; right: 5px; margin: 0; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Popover API](/en-US/docs/Web/API/Popover_API) - [`popover`](/en-US/docs/Web/HTML/Global_attributes/popover) HTML global attribute
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/text-emphasis-style/index.md
--- title: text-emphasis-style slug: Web/CSS/text-emphasis-style page-type: css-property browser-compat: css.properties.text-emphasis-style --- {{CSSRef}} The **`text-emphasis-style`** [CSS](/en-US/docs/Web/CSS) property sets the appearance of emphasis marks. It can also be set, and reset, using the {{cssxref("text-emphasis")}} shorthand. {{EmbedInteractiveExample("pages/css/text-emphasis-style.html")}} ## Syntax ```css /* Initial value */ text-emphasis-style: none; /* No emphasis marks */ /* <string> values */ text-emphasis-style: "x"; text-emphasis-style: "点"; text-emphasis-style: "\25B2"; text-emphasis-style: "*"; text-emphasis-style: "foo"; /* Should NOT be used. It may be computed to or rendered as 'f' only */ /* Keyword values */ text-emphasis-style: filled; text-emphasis-style: open; text-emphasis-style: dot; text-emphasis-style: circle; text-emphasis-style: double-circle; text-emphasis-style: triangle; text-emphasis-style: filled sesame; text-emphasis-style: open sesame; /* Global values */ text-emphasis-style: inherit; text-emphasis-style: initial; text-emphasis-style: revert; text-emphasis-style: revert-layer; text-emphasis-style: unset; ``` ### Values - `none` - : No emphasis marks. - `filled` - : The shape is filled with solid color. If neither `filled` nor `open` is present, this is the default. - `open` - : The shape is hollow. - `dot` - : Display small circles as marks. The filled dot is `'•'` (`U+2022`), and the open dot is `'◦'` (`U+25E6`). - `circle` - : Display large circles as marks. The filled circle is `'●'` (`U+25CF`), and the open circle is `'○'` (`U+25CB`). - `double-circle` - : Display double circles as marks. The filled double-circle is `'◉'` (`U+25C9`), and the open double-circle is `'◎'` (`U+25CE`). - `triangle` - : Display triangles as marks. The filled triangle is `'▲'` (`U+25B2`), and the open triangle is `'△'` (`U+25B3`). - `sesame` - : Display sesames as marks. The filled sesame is `'﹅'` (`U+FE45`), and the open sesame is `'﹆'` (`U+FE46`). - `<string>` - : Display the given string as marks. Authors should not specify more than one _character_ in `<string>`. The UA may truncate or ignore strings consisting of more than one grapheme cluster. ## Formal definition {{CSSInfo}} ## Formal syntax {{csssyntax}} ## Examples ### Basic example ```css h2 { -webkit-text-emphasis-style: sesame; text-emphasis-style: sesame; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - The related properties {{cssxref('text-emphasis-color')}}, {{cssxref('text-emphasis')}}. - The {{cssxref('text-emphasis-position')}} property allowing to define the position of the emphasis marks.
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/background-color/index.md
--- title: background-color slug: Web/CSS/background-color page-type: css-property browser-compat: css.properties.background-color --- {{CSSRef}} The **`background-color`** [CSS](/en-US/docs/Web/CSS) property sets the background color of an element. {{EmbedInteractiveExample("pages/css/background-color.html")}} ## Syntax ```css /* Keyword values */ background-color: red; background-color: indigo; /* Hexadecimal value */ background-color: #bbff00; /* Fully opaque */ background-color: #bf0; /* Fully opaque shorthand */ background-color: #11ffee00; /* Fully transparent */ background-color: #1fe0; /* Fully transparent shorthand */ background-color: #11ffeeff; /* Fully opaque */ background-color: #1fef; /* Fully opaque shorthand */ /* RGB value */ background-color: rgb(255 255 128); /* Fully opaque */ background-color: rgb(117 190 218 / 50%); /* 50% transparent */ /* HSL value */ background-color: hsl(50 33% 25%); /* Fully opaque */ background-color: hsl(50 33% 25% / 75%); /* 75% opaque, i.e. 25% transparent */ /* Special keyword values */ background-color: currentcolor; background-color: transparent; /* Global values */ background-color: inherit; background-color: initial; background-color: revert; background-color: revert-layer; background-color: unset; ``` The `background-color` property is specified as a single `<color>` value. ### Values - {{cssxref("&lt;color&gt;")}} - : The uniform color of the background. It is rendered behind any {{cssxref("background-image")}} that is specified, although the color will still be visible through any transparency in the image. ## Accessibility concerns It is important to ensure that the contrast ratio between the background color and the color of the text placed over it is high enough that people experiencing low vision conditions will be able to read the content of the page. Color contrast ratio is determined by comparing the luminance of the text and background color values. In order to meet current [Web Content Accessibility Guidelines (WCAG)](https://www.w3.org/WAI/standards-guidelines/wcag/), a ratio of 4.5:1 is required for text content and 3:1 for larger text such as headings. Large text is defined as 18.66px and [bold](/en-US/docs/Web/CSS/font-weight) or larger, or 24px or larger. - [WebAIM: Color Contrast Checker](https://webaim.org/resources/contrastchecker/) - [MDN Understanding WCAG, Guideline 1.4 explanations](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable#guideline_1.4_make_it_easier_for_users_to_see_and_hear_content_including_separating_foreground_from_background) - [Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0](https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html) ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### HTML ```html <div class="exampleone">Lorem ipsum dolor sit amet, consectetuer</div> <div class="exampletwo">Lorem ipsum dolor sit amet, consectetuer</div> <div class="examplethree">Lorem ipsum dolor sit amet, consectetuer</div> ``` ### CSS ```css .exampleone { background-color: transparent; } .exampletwo { background-color: rgb(153 102 153); color: rgb(255 255 204); } .examplethree { background-color: #777799; color: #ffffff; } ``` ### Result {{EmbedLiveSample("Examples", 200, 150)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Multiple backgrounds](/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Using_multiple_backgrounds) - The {{cssxref("&lt;color&gt;")}} data type - Other color-related properties: {{cssxref("color")}}, {{cssxref("border-color")}}, {{cssxref("outline-color")}}, {{cssxref("text-decoration-color")}}, {{cssxref("text-emphasis-color")}}, {{cssxref("text-shadow")}}, {{cssxref("caret-color")}}, and {{cssxref("column-rule-color")}} - [Applying color to HTML elements using CSS](/en-US/docs/Web/CSS/CSS_colors/Applying_color)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_doublecolon_view-transition-old/index.md
--- title: "::view-transition-old" slug: Web/CSS/::view-transition-old page-type: css-pseudo-element status: - experimental browser-compat: css.selectors.view-transition-old --- {{CSSRef}}{{SeeCompatTable}} The **`::view-transition-old`** [CSS](/en-US/docs/Web/CSS) [pseudo-element](/en-US/docs/Web/CSS/Pseudo-elements) represents the "old" view state of a view transition — a static screenshot of the old view, before the transition. During a view transition, `::view-transition-old` is included in the associated pseudo-element tree as explained in [The view transition process](/en-US/docs/Web/API/View_Transitions_API#the_view_transition_process), provided there's an "old" view state to represent. It is only ever a child of a {{cssxref("::view-transition-image-pair")}}, and never has any children. It is a replaced element, and therefore can be manipulated with properties such as {{cssxref("object-fit")}} and {{cssxref("object-position")}}. It has natural dimensions equal to the content's size. The following default styling is included in the UA stylesheet: ```css @keyframes -ua-view-transition-fade-out { to { opacity: 0; } } html::view-transition-old(*) { position: absolute; inset-block-start: 0; inline-size: 100%; block-size: auto; animation-name: -ua-view-transition-fade-out; animation-duration: inherit; animation-fill-mode: inherit; } ``` > **Note:** Additional view transition style sheet styles are also setup to animate `::view-transition-old`. These are dynamically generated during the view transition; see the specification [setup transition pseudo-elements](https://drafts.csswg.org/css-view-transitions-1/#setup-transition-pseudo-elements) and [update pseudo-element styles](https://drafts.csswg.org/css-view-transitions-1/#update-pseudo-element-styles) sections for more details. ## Syntax ```css-nolint ::view-transition-old(<pt-name-selector>) { /* ... */ } ``` `<pt-name-selector>` can be one of the following values: - `*` - : Causes the pseudo-element to match all view transition groups. - `root` - : Causes the pseudo-element to match the default `root` view transition group created by the user agent to contain the view transition for the overall page, meaning any element not assigned to its own specific view transition group via the {{cssxref("view-transition-name")}} property. - {{cssxref("custom-ident")}} - : Causes the pseudo-element to match a specific view transition group created by assigning the given {{cssxref("custom-ident")}} to an element via the {{cssxref("view-transition-name")}} property. ## Examples ```css figcaption { view-transition-name: figure-caption; } @keyframes grow-x { from { transform: scaleX(0); } to { transform: scaleX(1); } } @keyframes shrink-x { from { transform: scaleX(1); } to { transform: scaleX(0); } } ::view-transition-old(figure-caption), ::view-transition-new(figure-caption) { height: auto; right: 0; left: auto; transform-origin: right center; } ::view-transition-old(figure-caption) { animation: 0.25s linear both shrink-x; } ::view-transition-new(figure-caption) { animation: 0.25s 0.25s linear both grow-x; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) - [Smooth and simple transitions with the View Transitions API](https://developer.chrome.com/docs/web-platform/view-transitions/)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/transform-origin/index.md
--- title: transform-origin slug: Web/CSS/transform-origin page-type: css-property browser-compat: css.properties.transform-origin --- {{CSSRef}} The **`transform-origin`** [CSS](/en-US/docs/Web/CSS) property sets the origin for an element's transformations. {{EmbedInteractiveExample("pages/css/transform-origin.html")}} The transform origin is the point around which a transformation is applied. For example, the transform origin of the [`rotate()`](/en-US/docs/Web/CSS/transform-function/rotate) function is the center of rotation. In effect, this property wraps a pair of translations around the element's other transformations. The first translation moves the transform origin to the true origin at <math><mrow><mo stretchy="false">(</mo><mn>0</mn><mo>,</mo><mn>0</mn><mo stretchy="false">)</mo></mrow></math>. Then the other transformations are applied, and because the transform origin is at <math><mrow><mo stretchy="false">(</mo><mn>0</mn><mo>,</mo><mn>0</mn><mo stretchy="false">)</mo></mrow></math>, those transformations act about the transform origin. Finally, the opposite translation is applied, moving the transform origin back to its original location. Consequently, this definition ```css transform-origin: -100% 50%; transform: rotate(45deg); ``` results in the same transformation as ```css transform-origin: 0 0; transform: translate(-100%, 50%) rotate(45deg) translate(100%, -50%); ``` Reading from right to left, `translate(100%, -50%)` is the translation to bring the transform origin to the true origin, `rotate(45deg)` is the original transformation, and `translate(-100%, 50%)` is the translation to restore the transform origin to its original location. By default, the origin of a transform is `center`. ## Syntax ```css /* One-value syntax */ transform-origin: 2px; transform-origin: bottom; /* x-offset | y-offset */ transform-origin: 3cm 2px; /* x-offset-keyword | y-offset */ transform-origin: left 2px; /* x-offset-keyword | y-offset-keyword */ transform-origin: right top; /* y-offset-keyword | x-offset-keyword */ transform-origin: top right; /* x-offset | y-offset | z-offset */ transform-origin: 2px 30% 10px; /* x-offset-keyword | y-offset | z-offset */ transform-origin: left 5px -3px; /* x-offset-keyword | y-offset-keyword | z-offset */ transform-origin: right bottom 2cm; /* y-offset-keyword | x-offset-keyword | z-offset */ transform-origin: bottom right 2cm; /* Global values */ transform-origin: inherit; transform-origin: initial; transform-origin: revert; transform-origin: revert-layer; transform-origin: unset; ``` The `transform-origin` property may be specified using one, two, or three values, where each value represents an offset. Offsets that are not explicitly defined are reset to their corresponding [initial values](/en-US/docs/Web/CSS/initial_value). If a single {{cssxref("&lt;length&gt;")}} or {{cssxref("&lt;percentage&gt;")}} value is defined, it represents the horizontal offset. If two or more values are defined and either no value is a keyword, or the only used keyword is `center`, then the first value represents the horizontal offset and the second represents the vertical offset. - One-value syntax: - The value must be a {{cssxref("&lt;length&gt;")}}, a {{cssxref("&lt;percentage&gt;")}}, or one of the keywords `left`, `center`, `right`, `top`, and `bottom`. - Two-value syntax: - One value must be a {{cssxref("&lt;length&gt;")}}, a {{cssxref("&lt;percentage&gt;")}}, or one of the keywords `left`, `center`, and `right`. - The other value must be a {{cssxref("&lt;length&gt;")}}, a {{cssxref("&lt;percentage&gt;")}}, or one of the keywords `top`, `center`, and `bottom`. - Three-value syntax: - The first two values are the same as for the two-value syntax. - The third value must be a {{cssxref("&lt;length&gt;")}}. It always represents the Z offset. ### Values - _x-offset_ - : Is a {{cssxref("&lt;length&gt;")}} or a {{cssxref("&lt;percentage&gt;")}} describing how far from the left edge of the box the origin of the transform is set. - _offset-keyword_ - : Is one of the `left`, `right`, `top`, `bottom`, or `center` keyword describing the corresponding offset. - _y-offset_ - : Is a {{cssxref("&lt;length&gt;")}} or a {{cssxref("&lt;percentage&gt;")}} describing how far from the top edge of the box the origin of the transform is set. - _x-offset-keyword_ - : Is one of the `left`, `right`, or `center` keyword describing how far from the left edge of the box the origin of the transform is set. - _y-offset-keyword_ - : Is one of the `top`, `bottom`, or `center` keyword describing how far from the top edge of the box the origin of the transform is set. - _z-offset_ - : Is a {{cssxref("&lt;length&gt;")}} (and never a {{cssxref("&lt;percentage&gt;")}} which would make the statement invalid) describing how far from the user eye the z=0 origin is set. The keywords are convenience shorthands and match the following {{cssxref("&lt;percentage&gt;")}} values: | Keyword | Value | | -------- | ------ | | `left` | `0%` | | `center` | `50%` | | `right` | `100%` | | `top` | `0%` | | `bottom` | `100%` | ## Formal definition {{CSSInfo}} > **Note:** The initial value of `transform-origin` is `0 0` for all SVG elements except for root `<svg>` elements and `<svg>` elements that are a direct child of a [foreignObject](/en-US/docs/Web/SVG/Element/foreignObject), and whose `transform-origin` is `50% 50%`, like other CSS elements. See the [SVG transform-origin](/en-US/docs/Web/SVG/Attribute/transform-origin) attribute for more information. ## Formal syntax {{csssyntax}} ## Examples ### A demonstration of various transform values This example shows the effect of choosing different `transform-origin` values for a variety of transformation functions. ```html hidden <div class="container"> <div class="example"> <div class="box box1">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: none; </pre> <div class="example"> <div class="box box2">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: rotate(30deg); </pre> <div class="example"> <div class="box box3">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: rotate(30deg); transform-origin: 0 0; </pre> <div class="example"> <div class="box box4">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: rotate(30deg); transform-origin: 100% 100%; </pre> <div class="example"> <div class="box box5">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: rotate(30deg); transform-origin: -1em -3em; </pre> <div class="example"> <div class="box box6">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: scale(1.7); </pre> <div class="example"> <div class="box box7">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: scale(1.7); transform-origin: 0 0; </pre> <div class="example"> <div class="box box8">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: scale(1.7); transform-origin: 100% -30%; </pre> <div class="example"> <div class="box box9">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: skewX(50deg); transform-origin: 100% -30%; </pre> <div class="example"> <div class="box box10">&nbsp;</div> <div class="box original">&nbsp;</div> </div> <pre> transform: skewY(50deg); transform-origin: 100% -30%; </pre> </div> ``` ```css hidden .container { display: grid; grid-template-columns: 200px 100px; gap: 20px; } .example { position: relative; margin: 0 2em 4em 5em; } .box { display: inline-block; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; } .original { position: absolute; left: 0; opacity: 20%; } .box1 { transform: none; } .box2 { transform: rotate(30deg); } .box3 { transform: rotate(30deg); transform-origin: 0 0; } .box4 { transform: rotate(30deg); transform-origin: 100% 100%; } .box5 { transform: rotate(30deg); transform-origin: -1em -3em; } .box6 { transform: scale(1.7); } .box7 { transform: scale(1.7); transform-origin: 0 0; } .box8 { transform: scale(1.7); transform-origin: 100% -30%; } .box9 { transform: skewX(50deg); transform-origin: 100% -30%; } .box10 { transform: skewY(50deg); transform-origin: 100% -30%; } ``` {{EmbedLiveSample('A_demonstration_of_various_transform_values', '', 1350) }} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS transforms](/en-US/docs/Web/CSS/CSS_transforms/Using_CSS_transforms) - <https://css-tricks.com/almanac/properties/t/transform-origin/>
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_colon_picture-in-picture/index.md
--- title: ":picture-in-picture" slug: Web/CSS/:picture-in-picture page-type: css-pseudo-class browser-compat: css.selectors.picture-in-picture --- {{CSSRef}} The **`:picture-in-picture`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) matches the element which is currently in picture-in-picture mode. ## Syntax ```css :picture-in-picture { /* ... */ } ``` ## Usage notes The `:picture-in-picture` pseudo-class lets you configure your stylesheets to automatically adjust the size, style, or layout of content when a video switches back and forth between picture-in-picture and traditional presentation modes. ## Examples In this example, a video has a box shadow when it is displayed in the floating window. ### HTML The page's HTML looks like this: ```html <h1>MDN Web Docs Demo: :picture-in-picture pseudo-class</h1> <p> This demo uses the <code>:picture-in-picture</code> pseudo-class to automatically change the style of a video entirely using CSS. </p> <video id="pip-video"></video> ``` The {{HTMLElement("video")}} with the ID `"pip-video"` will change between having a red box shadow or not, depending on whether or not it is displayed in the picture-in-picture floating window. ### CSS The magic happens in the CSS. ```css :picture-in-picture { box-shadow: 0 0 0 5px red; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Picture-in-picture API](/en-US/docs/Web/API/Picture-in-Picture_API) - {{DOMxRef("HTMLVideoElement.requestPictureInPicture()")}} - {{DOMxRef("HTMLVideoElement.disablePictureInPicture")}} - {{DOMxRef("Document.pictureInPictureEnabled")}} - {{DOMxRef("Document.exitPictureInPicture()")}} - {{DOMxRef("Document.pictureInPictureElement")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/background-position-x/index.md
--- title: background-position-x slug: Web/CSS/background-position-x page-type: css-property browser-compat: css.properties.background-position-x --- {{CSSRef}} The **`background-position-x`** [CSS](/en-US/docs/Web/CSS) property sets the initial horizontal position for each background image. The position is relative to the position layer set by {{cssxref("background-origin")}}. {{EmbedInteractiveExample("pages/css/background-position-x.html")}} <!-- The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request. --> The value of this property is overridden by any declaration of the {{cssxref("background")}} or {{cssxref("background-position")}} shorthand properties applied to the element after it. ## Syntax ```css /* Keyword values */ background-position-x: left; background-position-x: center; background-position-x: right; /* <percentage> values */ background-position-x: 25%; /* <length> values */ background-position-x: 0px; background-position-x: 1cm; background-position-x: 8em; /* Side-relative values */ background-position-x: right 3px; background-position-x: left 25%; /* Multiple values */ background-position-x: 0px, center; /* Global values */ background-position-x: inherit; background-position-x: initial; background-position-x: revert; background-position-x: revert-layer; background-position-x: unset; ``` The `background-position-x` property is specified as one or more values, separated by commas. ### Values - `left` - : Aligns the left edge of the background image with the left edge of the background position layer. - `center` - : Aligns the center of the background image with the center of the background position layer. - `right` - : Aligns the right edge of the background image with the right edge of the background position layer. - {{cssxref("&lt;length&gt;")}} - : The offset of the given background image's left vertical edge from the background position layer's left vertical edge. (Some browsers allow assigning the right edge for offset). - {{cssxref("&lt;percentage&gt;")}} - : The offset of the given background image's horizontal position relative to the container. A value of 0% means that the left edge of the background image is aligned with the left edge of the container, and a value of 100% means that the _right_ edge of the background image is aligned with the _right_ edge of the container, thus a value of 50% horizontally centers the background image. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Basic example The following example shows a simple background image implementation, with background-position-x and background-position-y used to define the image's horizontal and vertical positions separately. #### HTML ```html <div></div> ``` #### CSS ```css div { width: 300px; height: 300px; background-color: skyblue; background-image: url(https://mdn.dev/archives/media/attachments/2020/07/29/17350/3b4892b7e820122ac6dd7678891d4507/firefox.png); background-repeat: no-repeat; background-position-x: center; background-position-y: bottom; } ``` #### Result {{EmbedLiveSample('Basic_example', '100%', 300)}} ### Side-relative values The following example shows support for side-relative offset syntax, which allows the developer to offset the background from any edge. #### HTML ```html <div></div> ``` #### CSS ```css div { width: 300px; height: 300px; background-color: seagreen; background-image: url(https://mdn.dev/archives/media/attachments/2020/07/29/17350/3b4892b7e820122ac6dd7678891d4507/firefox.png); background-repeat: no-repeat; background-position-x: right 20px; background-position-y: bottom 10px; } ``` #### Result {{EmbedLiveSample('Side-relative_values', '100%', 300)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("background-position")}} - {{cssxref("background-position-y")}} - [Using multiple backgrounds](/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Using_multiple_backgrounds)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/css_colors/index.md
--- title: CSS colors slug: Web/CSS/CSS_colors page-type: css-module spec-urls: - https://drafts.csswg.org/css-color/ - https://drafts.csswg.org/css-color-5/ --- {{CSSRef}} The **CSS colors** module defines colors, color types, color blending, opacity, and how you can apply these colors and effects to HTML content. While this module has only two CSS properties, {{cssxref("color")}} and {{cssxref("opacity")}}, over 20 CSS and SVG properties, CSS images, at-rules, and @media rules depend on these two properties. ### Colors in action The color syntax converter below shows the values of the currently selected color in [red-green-blue](/en-US/docs/Web/CSS/color_value/rgb) (RGB), [hexadecimal](/en-US/docs/Web/CSS/hex-color) (HEX), [hue, saturation, and lightness](/en-US/docs/Web/CSS/color_value/hsl) (HSL), and [hue, whiteness, and blackness](/en-US/docs/Web/CSS/color_value/hwb) (HWB) CSS color formats. All the RGB, HEX, HSL, and HWB color values here, while written differently, represent the same color value. {{EmbedGHLiveSample("css-examples/modules/colors.html", '100%', 450)}} Selecting a color via the [color picker](/en-US/docs/Web/HTML/Element/input/color) and an opacity via the [slider](/en-US/docs/Web/HTML/Element/input/range) updates the RGB, HEX, HSL, and HWB values. When you choose a new color or opacity value, the color of the background and the slider update via the CSS properties {{cssxref("background-color")}} and {{cssxref("accent-color")}}, respectively. To see the code for this color syntax converter, [view the source on GitHub](https://github.com/mdn/css-examples/blob/main/modules/colors.html). ## Reference ### Properties - {{cssxref("color")}} - {{cssxref("opacity")}} ### At-rules and descriptors - {{cssxref("@color-profile")}} - [`components`](/en-US/docs/Web/CSS/@color-profile#descriptors) descriptor - [`rendering-intent`](/en-US/docs/Web/CSS/@color-profile#descriptors) descriptor - [`src`](/en-US/docs/Web/CSS/@color-profile#descriptors) descriptor ### Functions - Absolute color functions, including: - [`rgb()`](/en-US/docs/Web/CSS/color_value/rgb) and its `rgba()` alias - [`hsl()`](/en-US/docs/Web/CSS/color_value/hsl) and its `hsla()` alias - [`hwb()`](/en-US/docs/Web/CSS/color_value/hwb) - [`lab()`](/en-US/docs/Web/CSS/color_value/lab) - [`lch()`](/en-US/docs/Web/CSS/color_value/lch) - [`oklab()`](/en-US/docs/Web/CSS/color_value/oklab) - [`oklch()`](/en-US/docs/Web/CSS/color_value/oklch) - [`color()`](/en-US/docs/Web/CSS/color_value/color) - [`color-contrast()`](/en-US/docs/Web/CSS/color_value/color-contrast) {{Experimental_Inline}} - [`color-mix()`](/en-US/docs/Web/CSS/color_value/color-mix) - [`device-cmyk()`](/en-US/docs/Web/CSS/color_value/device-cmyk) {{Experimental_Inline}} - {{CSSXref("color_value/light-dark", "light-dark()")}} {{Experimental_Inline}} ### Data types - {{cssxref("&lt;color&gt;")}} - [`<absolute-color-function>`](#functions) - {{cssxref("hex-color")}} - {{cssxref("named-color")}} - {{cssxref("alpha-value")}} - {{cssxref("hue")}} - {{cssxref("system-color")}} - [`<colorspace-params>`](/en-US/docs/Web/CSS/color_value/color#using_predefined_colorspaces_with_color) ### Keywords - [`currentcolor`](/en-US/docs/Web/CSS/color_value#currentcolor_keyword) - [`transparent`](/en-US/docs/Web/CSS/named-color#transparent) ### Interfaces - `CSSColorProfileRule` {{Experimental_Inline}} ## Guides - [Applying color to HTML elements using CSS](/en-US/docs/Web/CSS/CSS_colors/Applying_color) - : A guide to using CSS to apply color to a variety of types of content. All color-related CSS properties are touched upon. - [Understanding color and luminance](/en-US/docs/Web/Accessibility/Understanding_Colors_and_Luminance) - : Color perception and using colors with color insensitive (color blind) users, reduced vision users and users with vestibular disorders or other neurological disorders in mind. - [WCAG 1.4.1: Color contrast](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Color_contrast) - : Explanation of contrast requirements between background and foreground content to ensure legibility. - [Color picker tool](/en-US/docs/Web/CSS/CSS_colors/Color_picker_tool) - : This tool makes it easy to create, adjust, and experiment with custom colors. ## Related concepts - CSS properties that are part of other specifications: - {{cssxref("accent-color")}} - {{cssxref("background-color")}} - {{cssxref("background-image")}} - {{cssxref("border-color")}} - {{cssxref("box-shadow")}} - {{cssxref("caret-color")}} - {{cssxref("color")}} - {{cssxref("color-scheme")}} - {{cssxref("column-rule-color")}} - {{cssxref("outline-color")}} - {{cssxref("scrollbar-color")}} - {{cssxref("text-decoration-color")}} - {{cssxref("text-emphasis-color")}} - {{cssxref("text-shadow")}} - {{cssxref("-webkit-tap-highlight-color")}} - SVG color properties that are part of other specifications: - [`fill`](/en-US/docs/Web/SVG/Attribute/fill) - [`flood-color`](/en-US/docs/Web/SVG/Attribute/flood-color) - [`lighting-color`](/en-US/docs/Web/SVG/Attribute/lighting-color) - [`stop-color`](/en-US/docs/Web/SVG/Attribute/stop-color) - [`stroke`](/en-US/docs/Web/SVG/Attribute/stroke) - SVG [`color`](/en-US/docs/Web/SVG/Attribute/color) attribute - {{glossary("Color wheel")}} glossary term - {{glossary("Interpolation")}} glossary term - The [`@font-palette-values`](/en-US/docs/Web/CSS/@font-palette-values) at-rule [`override-colors`](/en-US/docs/Web/CSS/@font-palette-values/override-colors) descriptor - The [`@color-profile`](/en-US/docs/Web/CSS/@color-profile) at-rule - The [`color-gamut`](/en-US/docs/Web/CSS/@media/color-gamut) @media feature - The [`forced-colors`](/en-US/docs/Web/CSS/@media/forced-colors) @media feature ## Specifications {{Specifications}} ## See also - [CSS color adjustment](/en-US/docs/Web/CSS/CSS_color_adjustment) module and the {{cssxref("print-color-adjust")}} property. - [CSS images](/en-US/docs/Web/CSS/CSS_images) module, which is where CSS [`<gradient>`](/en-US/docs/Web/CSS/gradient) images are defined. - The [`VideoColorSpace`](/en-US/docs/Web/API/VideoColorSpace) interface - The SVG [`<feColorMatrix>`](/en-US/docs/Web/SVG/Element/feColorMatrix) element - [Canvas API: applying styles and colors](/en-US/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors#colors)
0
data/mdn-content/files/en-us/web/css/css_colors
data/mdn-content/files/en-us/web/css/css_colors/applying_color/index.md
--- title: Applying color to HTML elements using CSS slug: Web/CSS/CSS_colors/Applying_color page-type: guide --- {{CSSRef}} This article is a primer introducing each of the ways CSS color can be used in HTML. The use of color is a fundamental form of human expression. Children experiment with color before they even have the manual dexterity to draw. Maybe that's why color is one of the first things people often want to experiment with when learning to develop websites. With [CSS](/en-US/docs/Web/CSS), there are lots of ways to add color to your [HTML](/en-US/docs/Web/HTML) [elements](/en-US/docs/Web/HTML/Element) to create just the look you want. We're going to touch on most of what you'll need to know when using color, including a [list of what you can color and what CSS properties are involved](#things_that_can_have_color), [how you describe colors](#how_to_describe_a_color), and how to actually [use colors both in stylesheets and in scripts](#using_color). We'll also take a look at how to [let the user pick a color](#letting_the_user_pick_a_color). Then we'll wrap things up with a brief discussion of how to [use color wisely](#using_color_wisely): how to select appropriate colors, keeping in mind the needs of people with differing visual capabilities. ## Things that can have color At the element level, everything in HTML can have color applied to it. Instead, let's look at things in terms of the kinds of things that are drawn in the elements, such as text and borders and so forth. For each, we'll see a list of the CSS properties that apply color to them. At a fundamental level, the {{cssxref("color")}} property defines the foreground color of an HTML element's content and the {{cssxref("background-color")}} property defines the element's background color. These can be used on just about any element. ### Text Whenever an element is rendered, these properties are used to determine the color of the text, its background, and any decorations on the text. - {{cssxref("color")}} - : The color to use when drawing the text and any [text decorations](/en-US/docs/Learn/CSS/Styling_text/Fundamentals#font_style_font_weight_text_transform_and_text_decoration) (such as the addition of under- or overlines, strike-through lines, and so forth. - {{cssxref("background-color")}} - : The text's background color. - {{cssxref("text-shadow")}} - : Configures a shadow effect to apply to text. Among the options for the shadow is the shadow's base color (which is then blurred and blended with the background based on the other parameters). See [Text drop shadows](/en-US/docs/Learn/CSS/Styling_text/Fundamentals#text_drop_shadows) to learn more. - {{cssxref("text-decoration-color")}} - : By default, text decorations (such as underlines, strikethroughs, etc.) use the `color` property as their colors. However, you can override that behavior and use a different color for them with the `text-decoration-color` property. - {{cssxref("text-emphasis-color")}} - : The color to use when drawing emphasis symbols adjacent to each character in the text. This is used primarily when drawing text for East Asian languages. - {{cssxref("caret-color")}} - : The color to use when drawing the {{Glossary("caret")}} (sometimes referred to as the text input cursor) within the element. This is only useful in elements that are editable, such as {{HTMLElement("input")}} and {{HTMLElement("textarea")}} or elements whose HTML [`contenteditable`](/en-US/docs/Web/HTML/Global_attributes#contenteditable) attribute is set. ### Boxes Every element is a box with some sort of content, and has a background and a border in addition to whatever contents the box may have. - [Borders](#borders_2) - : See the section [Borders](#borders_2) for a list of the CSS properties you can use to set the colors of a box's borders. - {{cssxref("background-color")}} - : The background color to use in areas of the element that have no foreground content. - {{cssxref("column-rule-color")}} - : The color to use when drawing the line separating columns of text. - {{cssxref("outline-color")}} - : The color to use when drawing an outline around the outside of the element. This outline is different from the border in that it doesn't get space set aside for it in the document (so it may overlap other content). It's generally used as a focus indicator, to show which element will receive input events. ### Borders Any element can have a [border](/en-US/docs/Learn/CSS/Building_blocks/Backgrounds_and_borders) drawn around it. A basic element border is a line drawn around the edges of the element's content. See [The box model](/en-US/docs/Learn/CSS/Building_blocks/The_box_model) to learn about the relationship between elements and their borders, and the article [Styling borders using CSS](/en-US/docs/Learn/CSS/Building_blocks/Backgrounds_and_borders) to learn more about applying styles to borders. You can use the {{cssxref("border")}} shorthand property, which lets you configure everything about the border in one shot (including non-color features of borders, such as its [width](/en-US/docs/Web/CSS/border-width), [style](/en-US/docs/Web/CSS/border-style) (solid, dashed, etc.), and so forth. - {{cssxref("border-color")}} - : Specifies a single color to use for every side of the element's border. - {{cssxref("border-left-color")}}, {{cssxref("border-right-color")}}, {{cssxref("border-top-color")}}, and {{cssxref("border-bottom-color")}} - : Lets you set the color of the corresponding side of the element's border. - {{cssxref("border-block-start-color")}} and {{cssxref("border-block-end-color")}} - : With these, you can set the color used to draw the borders which are closest to the start and end of the block the border surrounds. In a left-to-right writing mode (such as the way English is written), the block start border is the top edge and the block end is the bottom. This differs from the inline start and end, which are the left and right edges (corresponding to where each line of text in the box begins and ends). - {{cssxref("border-inline-start-color")}} and {{cssxref("border-inline-end-color")}} - : These let you color the edges of the border closest to the beginning and the end of the start of lines of text within the box. Which side this is will vary depending on the {{cssxref("writing-mode")}}, {{cssxref("direction")}}, and {{cssxref("text-orientation")}} properties, which are typically (but not always) used to adjust text directionality based on the language being displayed. For example, if the box's text is being rendered right-to-left, then the `border-inline-start-color` is applied to the right side of the border. ### Other ways to use color CSS isn't the only web technology that supports color. There are graphics technologies that are available on the web which also support color. - The HTML [Canvas API](/en-US/docs/Web/API/Canvas_API) - : Lets you draw 2D bitmapped graphics in a {{HTMLElement("canvas")}} element. See our [Canvas tutorial](/en-US/docs/Web/API/Canvas_API/Tutorial) to learn more. - [SVG](/en-US/docs/Web/SVG) (Scalable Vector Graphics) - : Lets you draw images using commands that draw specific shapes, patterns, and lines to produce an image. SVG commands are formatted as XML, and can be embedded directly into a web page or can be placed in the page using the {{HTMLElement("img")}} element, just like any other type of image. - [WebGL](/en-US/docs/Web/API/WebGL_API) - : The Web Graphics Library is an OpenGL ES-based API for drawing high-performance 2D and 3D graphics on the Web. See our [WebGL tutorial](/en-US/docs/Web/API/WebGL_API/Tutorial) to find out more. ## How to describe a color In order to represent a color in CSS, you have to find a way to translate the analog concept of "color" into a digital form that a computer can use. This is typically done by breaking down the color into components, such as how much of each of a set of primary colors to mix together, or how bright to make the color. As such, there are several ways you can describe color in CSS. For more detailed discussion of each of the color value types, see the reference for the CSS {{cssxref("&lt;color&gt;")}} unit. ### Keywords A set of standard color names is defined that lets you use keywords instead of numeric representations if you choose this way of describing colors, although there must be a keyword representing the exact color you want to use. Color keywords include the standard primary and secondary colors (such as `red`, `blue`, or `orange`), shades of gray (from `black` to `white`, including colors like `darkgray` and `lightgrey`), and a variety of other blended colors, including `lightseagreen`, `cornflowerblue`, and `rebeccapurple`. See [Named colors](/en-US/docs/Web/CSS/named-color) for more information on color keywords. ### RGB values There are three ways to represent an RGB color in CSS. #### Hexadecimal string notation Hexadecimal string notation represents a color using hexadecimal digits to represent each of the color components (red, green, and blue). It may also include a fourth component: the alpha channel (or opacity). Each color component can be represented as a number between 0 and 255 (0x00 and 0xFF) or, optionally, as a number between 0 and 15 (0x0 and 0xF). All components _must_ be specified using the same number of digits. If you use the single-digit notation, the final color is computed by using each component's digit twice; that is, `"#D"` becomes `"#DD"` when drawing. A color in hexadecimal string notation always begins with the character `"#"`. After that come the hexadecimal digits of the color code. The string is case-insensitive. - `"#rrggbb"` - : Specifies a fully-opaque color whose red component is the hexadecimal number `0xrr`, green component is `0xgg`, and blue component is `0xbb`. - `"#rrggbbaa"` - : Specifies a color whose red component is the hexadecimal number `0xrr`, green component is `0xgg`, and blue component is `0xbb`. The alpha channel is specified by `0xaa`; the lower this value is, the more translucent the color becomes. - `"#rgb"` - : Specifies a color whose red component is the hexadecimal number `0xrr`, green component is `0xgg`, and blue component is `0xbb`. - `"#rgba"` - : Specifies a color whose red component is the hexadecimal number `0xrr`, green component is `0xgg`, and blue component is `0xbb`. The alpha channel is specified by `0xaa`; the lower this value is, the more translucent the color becomes. As an example, you can represent the opaque color bright blue as `"#0000ff"` or `"#00f"`. To make it 25% opaque, you can use `"#0000ff44"` or `"#00f4"`. #### RGB functional notation RGB (Red/Green/Blue) functional notation, like hexadecimal string notation, represents colors using their red, green, and blue components (as well as, optionally, an alpha channel component for opacity). However, instead of using a string, the color is defined using the CSS function {{cssxref("color_value", "rgb()", "#rgb()")}}. This function accepts as its input parameters the values of the red, green, and blue components and an optional fourth parameter, the value for the alpha channel. Legal values for each of these parameters are: - `red`, `green`, and `blue` - : Each must be an {{cssxref("&lt;integer&gt;")}} value between 0 and 255 (inclusive), or a {{cssxref("&lt;percentage&gt;")}} from 0% to 100%. - `alpha` - : The alpha channel is specified as a percentage between 0% (fully transparent) and 100% (fully opaque), or a number between 0.0 (equivalent to 0%) and 1.0 (equivalent to 100%). For example, a bright red that's 50% opaque can be represented as `rgb(255 0 0 / 50%)` or `rgb(100% 0 0 / 0.5)`. ### Color functions with a hue component Aside from `rgb()`, you can use `hsl()` and `hwb()` which take a `<hue>` component. A [`<hue>`](/en-US/docs/Web/CSS/hue) is the property that allows us to tell the difference or similarity between colors like red, orange, yellow, green, blue, etc. The key concept is that you can specify a hue in an [`<angle>`](/en-US/docs/Web/CSS/angle) because most of the color models describe hues using a {{glossary("color wheel")}}. For more information on classifying these functions, see the [Color spaces and color models](#color_spaces_and_color_models) section. #### HSL functional notation Designers and artists often prefer to work using the [HSL](https://en.wikipedia.org/wiki/HSL_and_HSV) (Hue/Saturation/Luminosity) color method. The `hsl()` CSS function is similar to the `rgb()` function in usage otherwise. The diagram below shows an HSL color cylinder. Hue defines the color as an [`<angle>`](/en-US/docs/Web/CSS/angle) on a circular {{glossary("color wheel")}}. Saturation is a percentage of how much of the way between being a shade of gray and having the maximum possible amount of the given hue. As the value of luminance (or lightness) increases, the color transitions from the darkest to the brightest possible (from black to white). ![HSL color cylinder](640px-hsl_color_solid_cylinder.png) Image courtesy of user [SharkD](https://commons.wikimedia.org/wiki/User:SharkD) on [Wikipedia](https://en.wikipedia.org/), distributed under the [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/) license. The value of the hue (H) component of an HSL color is an angle from red around through yellow, green, cyan, blue, and magenta (ending up back at red again at 360°) that identifies what the base color is. The value can be specified in any {{cssxref("&lt;angle&gt;")}} unit supported by CSS, including degrees (`deg`), radians (`rad`), gradians (`grad`), or turns (`turn`). But this doesn't control how vivid or dull, or how bright or dark the color is. The saturation (S) component of the color specifies what percentage of the final color is comprised of the specified hue. The rest is defined by the grey level provided by the luminance (L) component. Think of it like creating the perfect paint color: 1. You start with base paint that's the maximum intensity possible for a given color, such as the most intense blue that can be represented by the user's screen. This is the **hue** (H) component: a value representing the angle around the {{glossary("color wheel")}} for the vivid hue we want to use as our base. 2. Then select a greyscale paint that corresponds how bright you want the color to be; this is the luminance. Do you want it to be very bright and nearly white, or very dark and closer to black, or somewhere in between? This is specified using a percentage, where 0% is perfectly black and 100% is perfectly white (regardless of the saturation or hue). In between values are a literal grey area. 3. Now that you have a grey paint and a perfectly vivid color, you need to mix them together. The saturation (S) component of the color indicates what percentage of the final color should be comprised of that perfectly vivid color. The rest of the final color is made up of the grey paint that represents the saturation. You can also optionally include an alpha channel, to make the color less than 100% opaque. Here are some sample colors in HSL notation: ```css hidden table { border: 1px solid black; font: 16px "Open Sans", Helvetica, Arial, sans-serif; border-spacing: 0; border-collapse: collapse; } th, td { border: 1px solid black; padding: 4px 6px; text-align: left; } th { background-color: hsl(0 0% 75%); } ``` ```html <table> <thead> <tr> <th scope="col">Color in HSL notation</th> <th scope="col">Example</th> </tr> </thead> <tbody> <tr> <td><code>hsl(90deg 100% 50%)</code></td> <td style="background-color: hsl(90deg 100% 50%);">&nbsp;</td> </tr> <tr> <td><code>hsl(90 100% 50%)</code></td> <td style="background-color: hsl(90 100% 50%);">&nbsp;</td> </tr> <tr> <td><code>hsl(0.15turn 50% 75%)</code></td> <td style="background-color: hsl(0.15turn 50% 75%);">&nbsp;</td> </tr> <tr> <td><code>hsl(0.15turn 90% 75%)</code></td> <td style="background-color: hsl(0.15turn 90% 75%);">&nbsp;</td> </tr> <tr> <td><code>hsl(0.15turn 90% 50%)</code></td> <td style="background-color: hsl(0.15turn 90% 50%);">&nbsp;</td> </tr> <tr> <td><code>hsl(270deg 90% 50%)</code></td> <td style="background-color: hsl(270deg 90% 50%);">&nbsp;</td> </tr> </tbody> </table> ``` {{EmbedLiveSample("HSL_functional_notation", 300, 260)}} > **Note:** When you omit the hue's unit, it's assumed to be in degrees (`deg`). #### HWB functional notation Much like the HSL functional notation above, the [hwb()](/en-US/docs/Web/CSS/color_value/hwb) function uses the same hue value. But instead of lightness and saturation you specify whiteness and blackness values in percentages. Values are **not** separated with a comma and an optional alpha value can be included (it must be preceded by a forward slash `/`). Here are some examples of using HWB notation: ```css /* These examples all specify varying shades of a lime green. */ hwb(90 10% 10%) hwb(90 10% 10%) hwb(90 50% 10%) hwb(90deg 10% 10%) hwb(1.5708rad 60% 0%) hwb(.25turn 0% 40%) /* Same lime green but with an alpha value */ hwb(90 10% 10% / 0.5) hwb(90 10% 10% / 50%) ``` ### Color spaces and color models The hexadecimal, named colors, and the `rgb()` function all use the [RGB](/en-US/docs/Glossary/RGB) model and are associated with the sRGB (`srgb`) color space. You might see "color model" and "color space" used interchangeably, but there is a difference which is worth noting: - A color model is the mathematical model that represents colors using numeric values. Color models describe how to create the available colors within a color space. - A color space is a system for grouping colors so that describing any given color is consistent. If you transform a color between different color spaces, it will look identical. The `hsl()` and `hwb()` functions we've used above use the sRGB color space, and both use _cylindrical_ models; this is why a `<hue>` angle lets you control the color's properties like on a [color wheel](/en-US/docs/Glossary/Color_wheel). Let's take a look at other color spaces that are available and why you might use them. #### CIELAB and Oklab color spaces The CIELAB and Oklab color spaces are based on human vision experiments and represent the entire range of colors that humans can see. The primary purpose of these models is that they are uniform so that a given distance between any two points in the color space should appear equally different to a viewer. For the CIELAB color space, you can use the `lab()` and `lch()` functions. The `lch()` function uses lightness (L), chroma (C), and hue (H) and the Lab model uses lightness (L), red/green-ness, and yellow/blue-ness along the "a" and "b" axes (rectangular coordinates) in the color space. The example below shows the effect of changing component values in the `lab()` and `lch()` functions where each row modifies a single component. The first row shows changes to the `lch()` lightness value, while the second row changes the `lch()` hue component. The third row changes the `lab()` "b" axis, so we have a different effect than `lch()` as we're gradually adding more yellow to the color rather than cycling through the entire color wheel: ```css hidden live-sample___lch-colors /* Varying shades of purple */ .container { display: grid; font-family: sans-serif; font-size: 14px; color: white; grid-template-columns: repeat(6, 1fr); gap: 4px; } .container div { border-radius: 8px; padding: 0.75rem; } #lch-20 { background-color: lch(20 40 0); } #lch-30 { background-color: lch(30 40 0); } #lch-40 { background-color: lch(40 40 0); } #lch-50 { background-color: lch(50 40 0); } #lch-60 { background-color: lch(60 40 0); } #lch-70 { background-color: lch(70 40 0); } #lch-hue-10 { background-color: lch(70 40 10); } #lch-hue-20 { background-color: lch(70 40 20); } #lch-hue-30 { background-color: lch(70 40 30); } #lch-hue-40 { background-color: lch(70 40 40); } #lch-hue-50 { background-color: lch(70 40 50); } #lch-hue-60 { background-color: lch(70 40 60); } #lab-10 { background-color: lab(70 40 10); } #lab-20 { background-color: lab(70 40 20); } #lab-30 { background-color: lab(70 40 30); } #lab-40 { background-color: lab(70 40 40); } #lab-50 { background-color: lab(70 40 50); } #lab-60 { background-color: lab(70 40 60); } ``` ```html hidden live-sample___lch-colors <div class="container"> <div id="lch-20">lch(20 40 0)</div> <div id="lch-30">lch(30 40 0)</div> <div id="lch-40">lch(40 40 0)</div> <div id="lch-50">lch(50 40 0)</div> <div id="lch-60">lch(60 40 0)</div> <div id="lch-70">lch(70 40 0)</div> <div id="lch-hue-10">lch(70 40 10)</div> <div id="lch-hue-20">lch(70 40 20)</div> <div id="lch-hue-30">lch(70 40 30)</div> <div id="lch-hue-40">lch(70 40 40)</div> <div id="lch-hue-50">lch(70 40 50)</div> <div id="lch-hue-50">lch(70 40 60)</div> <div id="lab-10">lab(70 40 10)</div> <div id="lab-20">lab(70 40 20)</div> <div id="lab-30">lab(70 40 30)</div> <div id="lab-40">lab(70 40 40)</div> <div id="lab-50">lab(70 40 50)</div> <div id="lab-60">lab(70 40 60)</div> </div> ``` {{embedlivesample("lch-colors", '100', '150') }} Oklab is a color space that uses the same model type as CIELAB but is built using additional numerical optimization steps, so the values are considered to be more accurate than CIELAB. Because of this optimization, hues are more perceptually uniform, making Oklab an excellent choice for gradients. If you already understand `lab()` and `lch()`, you can use these functions in a similar way: ```css .oklch-red { color: oklch(0.93 0.39 28); } .oklab-green { color: oklab(0.87 -0.2 0.18); } ``` #### The `color()` function If you want explicit control over color spaces when defining colors, you can use the [`color()`](/en-US/docs/Web/CSS/color_value/color) function. This is useful to describe a color for high-definition devices with wider color [gamuts](/en-US/docs/Glossary/Gamut). For example, if we wanted to show the `display-p3 0 0 1` color, which is outside of the sRGB gamut, you can use a [`@media`](/en-US/docs/Web/CSS/@media/color-gamut) at-rule for detecting if the client hardware supports colors in this range: ```css .vibrant { background-color: color(display-p3 0 0 1); /* Equivalent to out-of-gamut rgb(-27 55 153) */ } @media (color-gamut: p3) { .vibrant { background-color: color(display-p3 0 0 1); } } ``` ## Using color Now that you know what CSS properties exist that let you apply color to elements and the formats you can use to describe colors, you can put this together to begin to make use of color. As you may have seen from the list under [Things that can have color](#things_that_can_have_color), there are plenty of things you can color with CSS. Let's look at this from two sides: using color within a {{Glossary("stylesheet")}}, and adding and changing color using {{Glossary("JavaScript")}} code to alter the styles of elements. ### Specifying colors in stylesheets The easiest way to apply color to elements—and the way you'll usually do it—is to specify colors in the CSS that's used when rendering elements. While we won't use every single property mentioned previously, we'll look at a couple of examples. The concept is the same anywhere you use color. Let's take a look at an example, starting by looking at the results we're trying to achieve: {{EmbedLiveSample("Specifying_colors_in_stylesheets", 650, 150)}} #### HTML The HTML responsible for creating the above example is shown here: ```html <div class="wrapper"> <div class="box boxLeft"> <p>This is the first box.</p> </div> <div class="box boxRight"> <p>This is the second box.</p> </div> </div> ``` This is pretty simple, using a {{HTMLElement("div")}} as a wrapper around the contents, which consists of two more `<div>`s, each styled differently with a single paragraph ({{HTMLElement("p")}}) in each box. The magic happens, as usual, in the CSS, where we'll apply colors and define the layout for the HTML above. #### CSS We'll look at the CSS to create the above results a piece at a time, so we can review the interesting parts one by one. ```css .wrapper { width: 620px; height: 110px; margin: 0; padding: 10px; border: 6px solid mediumturquoise; } ``` The `.wrapper` class is used to assign styles to the {{HTMLElement("div")}} that encloses all of our other content. This establishes the size of the container using {{cssxref("width")}} and {{cssxref("height")}} as well as its {{cssxref("margin")}} and {{cssxref("padding")}}. Of more interest to our discussion here is the use of the {{cssxref("border")}} property to establish a border around the outside edge of the element. This border is a solid line, 6 pixels wide, in the color `mediumturquoise`. Our two colored boxes share a number of properties in common, so next we establish a class, `.box`, that defines those shared properties: ```css .box { width: 290px; height: 100px; margin: 0; padding: 4px 6px; font: 28px "Marker Felt", "Zapfino", cursive; display: flex; justify-content: center; align-items: center; } ``` In brief, `.box` establishes the size of each box, as well as the configuration of the font used within. We also take advantage of [CSS Flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout) to easily center the contents of each box. We enable `flex` mode using {{cssxref("display", "display: flex")}}, and set both {{cssxref("justify-content")}} and {{cssxref("align-items")}} to `center`. Then we can create a class for each of the two boxes that defines the properties that differ between the two. ```css .boxLeft { float: left; background-color: rgb(245 130 130); outline: 2px solid darkred; } ``` The `.boxLeft` class—which, cleverly, is used to style the box on the left—floats the box to the left, then sets up the colors: - The box's background color is set by changing the value of the CSS {{cssxref("background-color")}} property to `rgb(245 130 130)`. - An outline is defined for the box. Unlike the more commonly used `border`, {{cssxref("outline")}} doesn't affect layout at all; it draws over the top of whatever may happen to be outside the element's box instead of making room as `border` does. This outline is a solid, dark red line that's two pixels thick. Note the use of the `darkred` keyword when specifying the color. - Notice that we're not explicitly setting the text color. That means the value of {{cssxref("color")}} will be inherited from the nearest containing element that defines it. By default, that's black. ```css .boxRight { float: right; background-color: hsl(270deg 50% 75%); outline: 4px dashed rgb(110 20 120); color: hsl(0deg 100% 100%); text-decoration: underline wavy #88ff88; text-shadow: 2px 2px 3px black; } ``` > **Note:** When you try to show it in Safari, it will not show properly. Because Safari doesn't support `text-decoration: underline wavy #88ff88`. Finally, the `.boxRight` class describes the unique properties of the box that's drawn on the right. It's configured to float the box to the right so that it appears next to the previous box. Then the following colors are established: - The `background-color` is set using the HSL value specified using `hsl(270deg 50% 75%)`. This is a medium purple color. - The box's `outline` is used to specify that the box should be enclosed in a four pixel thick dashed line whose color is a somewhat deeper purple (`rgb(110 20 120)`). - The foreground (text) color is specified by setting the {{cssxref("color")}} property to `hsl(0deg 100% 100%)`. This is one of many ways to specify the color white. - We add a green wavy line under the text with {{cssxref("text-decoration")}}. - Finally, a bit of a shadow is added to the text using {{cssxref("text-shadow")}}. Its `color` parameter is set to `black`. ## Letting the user pick a color There are many situations in which your website may need to let the user select a color. Perhaps you have a customizable user interface, or you're implementing a drawing app. Maybe you have editable text and need to let the user choose the text color. Or perhaps your app lets the user assign colors to folders or items. Although historically it's been necessary to implement your own [color picker](https://en.wikipedia.org/wiki/Color_picker), HTML now provides support for browsers to provide one for your use through the {{HTMLElement("input")}} element, by using `"color"` as the value of its [`type`](/en-US/docs/Web/HTML/Element/input#type) attribute. The `<input>` element represents a color only in the [hexadecimal string notation](#hexadecimal_string_notation) covered above. ### Example: Picking a color Let's look at a simple example, in which the user can choose a color. As the user adjusts the color, the border around the example changes to reflect the new color. After finishing up and picking the final color, the color picker's value is displayed. {{EmbedLiveSample("Example_Picking_a_color", 525, 275)}} > **Note:** On macOS, you indicate that you've finalized selection of the color by closing the color picker window. #### HTML The HTML here creates a box that contains a color picker control (with a label created using the {{HTMLElement("label")}} element) and an empty paragraph element ({{HTMLElement("p")}}) into which we'll output some text from our JavaScript code. ```html <div id="box"> <label for="colorPicker">Border color:</label> <input type="color" value="#8888ff" id="colorPicker" /> <p id="output"></p> </div> ``` #### CSS The CSS establishes a size for the box and some basic styling for appearances. The border is also established with a 2-pixel width and a border color. ```css #box { width: 500px; height: 200px; border: 2px solid rgb(245 220 225); padding: 4px 6px; font: 16px "Lucida Grande", "Helvetica", "Arial", "sans-serif"; } ``` #### JavaScript The script here handles the task of updating the starting color of the border to match the color picker's value. Then two event handlers are added to deal with input from the [`<input type="color">`](/en-US/docs/Web/HTML/Element/input/color) element. ```js const colorPicker = document.getElementById("colorPicker"); const box = document.getElementById("box"); const output = document.getElementById("output"); box.style.borderColor = colorPicker.value; colorPicker.addEventListener( "input", (event) => { box.style.borderColor = event.target.value; }, false, ); colorPicker.addEventListener( "change", (event) => { output.innerText = `Color set to ${colorPicker.value}.`; }, false, ); ``` The {{domxref("Element/input_event", "input")}} event is sent every time the value of the element changes; that is, every time the user adjusts the color in the color picker. Each time this event arrives, we set the box's border color to match the color picker's current value. The {{domxref("HTMLElement/change_event", "change")}} event is received when the color picker's value is finalized. We respond by setting the contents of the `<p>` element with the ID `"output"` to a string describing the finally selected color. ## Using color wisely Making the right choices when selecting colors when designing a website can be a tricky process, especially if you aren't well-grounded in art, design, or at least basic color theory. The wrong color choice can render your site unattractive, or even worse, leave the content unreadable due to problems with contrast or conflicting colors. Worse still, if using the wrong colors can result in your content being outright unusable by people with certain vision problems, particularly color blindness. ### Finding the right colors Coming up with just the right colors can be tricky, especially without training in art or design. Fortunately, there are tools available that can help you. While they can't replace having a good designer helping you make these decisions, they can definitely get you started. #### Base color The first step is to choose your **base color**. This is the color that in some way defines your website or the subject matter of the site. Just as we associate green with the beverage [Mountain Dew](https://en.wikipedia.org/wiki/Mountain_Dew) and one might think of the color blue in relationship with the sky or the ocean, choosing an appropriate base color to represent your site is a good place to start. There are plenty of ways to select a base color; a few ideas include: - A color that is naturally associated with the topic of your content, such as the existing color identified with a product or idea or a color representative of the emotion you wish to convey. - A color that comes from imagery associated with what your content is about. If you're creating a website about a given item or product, choose a color that's physically present on that item. - Browse websites that let you look at lots of existing color palettes and images to find inspiration. When trying to decide upon a base color, you may find that browser extensions that let you select colors from web content can be particularly handy. Some of these are even specifically designed to help with this sort of work. For example, the website [ColorZilla](https://www.colorzilla.com/) offers an extension ([Chrome](https://www.colorzilla.com/chrome/) / [Firefox](https://www.colorzilla.com/firefox/)) that offers an eyedropper tool for picking colors from the web. It can also take averages of the colors of pixels in various sized areas or even a selected area of the page. > **Note:** The advantage to averaging colors can be that often what looks like a solid color is actually a surprisingly varied number of related colors all used in concert, blending to create a desired effect. Picking just one of these pixels can result in getting a color that on its own looks very out of place. #### Fleshing out the palette Once you have decided on your base color, there are plenty of online tools that can help you build out a palette of appropriate colors to use along with your base color by applying color theory to your base color to determine appropriate added colors. Many of these tools also support viewing the colors filtered so you can see what they would look like to people with various forms of color blindness. See [Color and accessibility](#color_and_accessibility) for a brief explanation of why this matters. A few examples (all free to use as of the time this list was last revised): - [MDN's color picker tool](/en-US/docs/Web/CSS/CSS_colors/Color_picker_tool) - [Paletton](https://paletton.com/) - [Adobe Color CC online color wheel](https://color.adobe.com/create/color-wheel) When designing your palette, be sure to keep in mind that in addition to the colors these tools typically generate, you'll probably also need to add some core neutral colors such as white (or nearly white), black (or nearly black), and some number of shades of gray. > **Note:** Usually, you are far better off using the smallest number of colors possible. By using color to accentuate rather than adding color to everything on the page, you keep your content easy to read and the colors you do use have far more impact. ### Color theory resources A full review of color theory is beyond the scope of this article, but there are plenty of articles about color theory available, as well as courses you can find at nearby schools and universities. A couple of useful resources about color theory: - [Color Science](https://www.khanacademy.org/computing/pixar/color) ([Khan Academy](https://www.khanacademy.org/) in association with [Pixar](https://www.pixar.com/)) - : An online course which introduces concepts such as what color is, how it's perceived, and how to use colors to express ideas. Presented by Pixar artists and designers. - [Color theory](https://en.wikipedia.org/wiki/Color_theory) on Wikipedia - : Wikipedia's entry on color theory, which has a lot of great information from a technical perspective. It's not really a resource for helping you with the color selection process, but is still full of useful information. ### Color and accessibility There are several ways color can be an {{Glossary("accessibility")}} problem. Improper or careless use of color can result in a website or app that a percentage of your target audience may not be able to use adequately, resulting in lost traffic, lost business, and possibly even a public relations problem. So it's important to consider your use of color carefully. You should do at least basic research into [color blindness](https://en.wikipedia.org/wiki/Color_blindness). There are several kinds; the most common is red-green color blindness, which causes people to be unable to differentiate between the colors red and green. There are others, too, ranging from inabilities to tell the difference between certain colors to total inability to see color at all. > **Note:** The most important rule: never use color as the only way to know something. If, for example, you indicate success or failure of an operation by changing the color of a shape from white to green for success and red for failure, users with red-green color-blindness won't be able to use your site properly. Instead, perhaps use both text and color together, so that everyone can understand what's happening. For more information about color blindness, see the following articles: - [Medline Plus: Color Blindness](https://medlineplus.gov/colorblindness.html) (United States National Institute of Health) - [American Academy of Ophthalmology: What Is Color Blindness?](https://www.aao.org/eye-health/diseases/what-is-color-blindness) - [Color Blindness & Web Design](https://www.usability.gov/get-involved/blog/2010/02/color-blindness.html) (Usability.gov: United States Department of Health and Human Services) ### Palette design example Let's consider a quick example of selecting an appropriate color palette for a site. Imagine that you're building a website for a new game that takes place on the planet Mars. So let's do a [Google search for photos of Mars](https://www.google.com/search?q=Mars&tbm=isch). Lots of good examples of Martian coloration there. We carefully avoid the mockups and the photos from movies. And we decide to use a photo taken by one of the Mars landers humanity has parked on the surface over the last few decades, since the game takes place on the planet's surface. We use a color picker tool to select a sample of the color we choose. Using an eyedropper tool, we identify a color we like and determine that the color in question is `#D79C7A`, which is an appropriate rusty orange-red color that's so stereotypical of the Martian surface. Having selected our base color, we need to build out our palette. We decide to use [Paletton](https://www.paletton.com/) to come up with the other colors we need. Upon opening Paletton, we see: ![Right after loading Paletton.](paletton1.png) Next, we enter our color's hex code (`D79C7A`) into the "Base RGB" box at the bottom-left corner of the tool: ![After entering base color](paletton2.png) We now see a monochromatic palette based on the color we picked from the Mars photo. If you need a lot of related colors for some reason, those are likely to be good ones. But what we really want is an accent color. Something that will pop along side the base color. To find that, we click the "add complementary" toggle underneath the menu that lets you select the palette type (currently "Monochromatic"). Paletton computes an appropriate accent color; clicking on the accent color down in the bottom-right corner tells us that this color is `#508D7C`. ![Now with complementary colors included.](paletton3.png) If you're unhappy with the color that's proposed to you, you can change the color scheme, to see if you find something you like better. For example, if we don't like the proposed greenish-blue color, we can click the Triad color scheme icon, which presents us with the following: ![Triad color scheme selected](paletton4.png) That greyish blue in the top-right looks pretty good. Clicking on it, we find that it's `#556E8D`. That would be used as the accent color, to be used sparingly to make things stand out, such as in headlines or in the highlighting of tabs or other indicators on the site: ![Triad color scheme selected](paletton-color-detail.png) Now we have our base color and our accent. On top of that, we have a few complementary shades of each, just in case we need them for gradients and the like. The colors can then be exported in a number of formats so you can make use of them. Once you have these colors, you will probably still need to select appropriate neutral colors. Common design practice is to try to find the sweet spot where there's just enough contrast that the text is crisp and readable but not enough contrast to become harsh for the eyes. It's easy to go too far in one way or another so be sure to get feedback on your colors once you've selected them and have examples of them in use available. If the contrast is too low, your text will tend to be washed out by the background, leaving it unreadable, but if your contrast is too high, the user may find your site garish and unpleasant to look at. ### Color, backgrounds, contrast, and printing What looks good on screen may look very different on paper. In addition, ink can be expensive, and if a user is printing your page, they don't necessarily need all the backgrounds and such using up their precious ink when all that matters is the text itself. Most browsers, by default, remove background images when printing documents. If your background colors and images have been selected carefully and/or are crucial to the usefulness of the content, you can use the CSS {{cssxref("print-color-adjust")}} property to tell the browser that it should not make adjustments to the appearance of content. The default value of `print-color-adjust`, `economy`, indicates that the browser is allowed to make appearance changes as it deems necessary in order to try to optimize the legibility and/or print economy of the content, given the type of output device the document is being drawn onto. You can set `print-color-adjust` to `exact` to tell the browser that the element or elements on which you use it have been designed specifically to best work with the colors and images left as they are. With this set, the browser won't tamper with the appearance of the element, and will draw it as indicated by your CSS. > **Note:** There is no guarantee, though, that `print-color-adjust: exact` will result in your CSS being used exactly as given. > If the browser provides user preferences to change the output (such as a "don't print backgrounds" checkbox in a print dialog box), that overrides the value of `print-color-adjust`. ## See also - [Drawing graphics](/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Drawing_graphics) - [Graphics on the web](/en-US/docs/Learn/HTML/Multimedia_and_embedding/Images_in_HTML#other_graphics_on_the_web) - [CSS color module](/en-US/docs/Web/CSS/CSS_colors)
0
data/mdn-content/files/en-us/web/css/css_colors
data/mdn-content/files/en-us/web/css/css_colors/color_picker_tool/index.md
--- title: Color picker tool slug: Web/CSS/CSS_colors/Color_picker_tool page-type: guide --- {{CSSRef}} This tool makes it easy to create, adjust, and experiment with custom colors for the web. It also makes it easy to convert between various [color formats](/en-US/docs/Web/CSS/color_value) supported by [CSS](/en-US/docs/Web/CSS), including: HEXA colors, RGB (Red/Green/Blue) and HSL (Hue/Saturation/Lightness). Control over the alpha channel is also supported. As you adjust the parameters that define the color, it gets displayed in all three standard Web CSS formats. In addition, based on the currently-selected color, a palette for HSL and HSV, as well as alpha, is generated. The "eyedropper" style color picker box can be toggled between HSL or HSV format. You can also test colors and how they overlap one another by dragging them into the box at the bottom of the tool and moving them over one another. Adjust their relative Z index values to move them forward and behind one another. This tool will help you identify the perfect CSS colors to apply to your [HTML](/en-US/docs/Web/HTML). {{EmbedGHLiveSample("css-examples/tools/color-picker/", '100%', 900)}} The generated colors you create above can be used anywhere color is used in CSS and HTML, unless otherwise noted. ## See also For more on using color, see: - [Applying color to HTML elements using CSS](/en-US/docs/Web/CSS/CSS_colors/Applying_color) - [Fundamental text and font styling](/en-US/docs/Learn/CSS/Styling_text/Fundamentals) - [Styling borders using CSS](/en-US/docs/Learn/CSS/Building_blocks/Backgrounds_and_borders) - [Changing background styles using CSS](/en-US/docs/Learn/CSS/Building_blocks/Backgrounds_and_borders) - [Color and color contrast](/en-US/docs/Learn/Accessibility/CSS_and_JavaScript#color_and_color_contrast) For additional tools, see: - [Border-image generator](/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Border-image_generator) - : This interactive tool lets you visually create border images (the {{cssxref("border-image")}} property). - [Border-radius generator](/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Border-radius_generator) - : This interactive tool lets you visually create rounded corners (the {{cssxref("border-radius")}} property). - [Box-shadow generator](/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Box-shadow_generator) - : This interactive tool lets you visually create shadows behind elements (the {{cssxref("box-shadow")}} property).
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_colon_any-link/index.md
--- title: ":any-link" slug: Web/CSS/:any-link page-type: css-pseudo-class browser-compat: css.selectors.any-link --- {{CSSRef}} The **`:any-link`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) selector represents an element that acts as the source anchor of a hyperlink, independent of whether it has been visited. In other words, it matches every {{HTMLElement("a")}} or {{HTMLElement("area")}} element that has an `href` attribute. Thus, it matches all elements that match {{cssxref(":link")}} or {{cssxref(":visited")}}. {{EmbedInteractiveExample("pages/tabbed/pseudo-class-any-link.html", "tabbed-shorter")}} ## Syntax ```css :any-link { /* ... */ } ``` ## Examples ### HTML ```html <a href="https://example.com">External link</a><br /> <a href="#">Internal target link</a><br /> <a>Placeholder link (won't get styled)</a> ``` ### CSS ```css a:any-link { border: 1px solid blue; color: orange; } /* WebKit browsers */ a:-webkit-any-link { border: 1px solid blue; color: orange; } ``` ### Result {{EmbedLiveSample('Examples')}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Creating hyperlinks](/en-US/docs/Learn/HTML/Introduction_to_HTML/Creating_hyperlinks) - Matches HTML elements: [`<a>`](/en-US/docs/Web/HTML/Element/a) and [`<area>`](/en-US/docs/Web/HTML/Element/area) with an [`href`](/en-US/docs/Web/HTML/Element/a#href) attribute - Related CSS selectors: - [`:visited`](/en-US/docs/Web/CSS/:visited) - [`:link`](/en-US/docs/Web/CSS/:link)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/element/index.md
--- title: element() slug: Web/CSS/element page-type: css-function status: - experimental browser-compat: css.types.image.element --- {{CSSRef}}{{SeeCompatTable}} The **`element()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) defines an {{cssxref("&lt;image&gt;")}} value generated from an arbitrary HTML element. This image is live, meaning that if the HTML element is changed, the CSS properties using the resulting value are automatically updated. A particularly useful scenario for using this would be to render an image in an HTML {{HTMLElement("canvas")}} element, then use that as a background. On Gecko browsers, you can use the non-standard {{domxref("document.mozSetImageElement()")}} method to change the element being used as the background for a given CSS background element. ## Syntax ```css element(id) ``` where: - _id_ - : The ID of an element to use as the background, specified using the HTML attribute #_id_ on the element. ## Examples These examples work in builds of Firefox that support `-moz-element()`. ### A somewhat realistic example This example uses a hidden {{HTMLElement("div")}} as a background. The background element uses a gradient, but also includes text that is rendered as part of the background. ```html <div style="width:400px; height:400px; background:-moz-element(#myBackground1) no-repeat;"> <p>This box uses the element with the #myBackground1 ID as its background!</p> </div> <div style="overflow:hidden; height:0;"> <div id="myBackground1" style="width:1024px; height:1024px; background-image: linear-gradient(to right, red, orange, yellow, white);"> <p style="transform-origin:0 0; rotate: 45deg; color:white;"> This text is part of the background. Cool, huh? </p> </div> </div> ``` {{EmbedLiveSample("A_somewhat_realistic_example")}} The {{HTMLElement("div")}} element with the ID "myBackground1" is used as the background for the content including the paragraph "This box uses the element with the #myBackground1 ID as its background!". ### Page Preview This <a href="https://iamvdo.me/en/blog/css-element-function"> example based on Vincent De Oliveira's</a> creates a preview of the `<div id="css-source">` inside `<div id="css-result">`. #### HTML ```html <div id="css-source"> <h1>Page Preview</h1> </div> <div id="css-result"></div> ``` #### CSS ```css #css-result { background: -moz-element(#css-source) no-repeat; width: 256px; height: 32px; background-size: 80%; border: dashed; } ``` #### Result {{EmbedLiveSample("Page_Preview")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("image/image", "image()")}} - {{cssxref("image/image-set", "image-set()")}} - {{cssxref("&lt;image&gt;")}} - {{cssxref("&lt;gradient&gt;")}} - {{cssxref("cross-fade", "cross-fade()")}} - {{domxref("document.mozSetImageElement()")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/math-style/index.md
--- title: math-style slug: Web/CSS/math-style page-type: css-property browser-compat: css.properties.math-style --- {{CSSRef}} The `math-style` property indicates whether MathML equations should render with normal or compact height. ## Syntax ```css /* Keyword values */ math-style: normal; math-style: compact; /* Global values */ math-style: inherit; math-style: initial; math-style: revert; math-style: revert-layer; math-style: unset; ``` ### Values - `normal` - : The initial value, indicates normal rendering. - `compact` - : The math layout on descendants tries to minimize the logical height. ## Formal definition {{cssinfo}} ## Formal syntax {{CSSSyntax}} ## Examples ### Changing the style of a formula to compact #### CSS ```css math { math-style: normal; } .compact { math-style: compact; } ``` #### HTML ```html <p> Normal height <math> <mrow> <munderover> <mo>∑</mo> <mrow> <mi>n</mi> <mo>=</mo> <mn>1</mn> </mrow> <mrow> <mo>+</mo> <mn>∞</mn> </mrow> </munderover> </mrow> </math> and compact height <math class="compact"> <mrow> <munderover> <mo>∑</mo> <mrow> <mi>n</mi> <mo>=</mo> <mn>1</mn> </mrow> <mrow> <mo>+</mo> <mn>∞</mn> </mrow> </munderover> </mrow> </math> equations. </p> ``` #### Result {{EmbedLiveSample("Examples")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("math-depth")}} - {{cssxref("font-size")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/integer/index.md
--- title: <integer> slug: Web/CSS/integer page-type: css-type browser-compat: css.types.integer --- {{CSSRef}} The **`<integer>`** [CSS](/en-US/docs/Web/CSS) [data type](/en-US/docs/Web/CSS/CSS_Types) is a special type of {{cssxref("number")}} that represents a positive or negative whole number. Integers can be used in numerous CSS properties and descriptors, such as the {{cssxref("column-count")}}, {{cssxref("counter-increment")}}, {{cssxref("grid-column")}}, {{cssxref("grid-row")}}, and {{cssxref("z-index")}} properties and the {{cssxref("@counter-style/range", "range")}} descriptor. ## Syntax The `<integer>` data type consists of one or several decimal digits, 0 through 9 inclusive, optionally preceded by a single `+` or `-` sign. There is no unit associated with integers. > **Note:** There is no official range of valid `<integer>` values, and the specifications do not specify a range. ## Interpolation When animated, values of the `<integer>` data type are {{Glossary("interpolation", "interpolated")}} using discrete, whole steps. The calculation is done as if they were real, floating-point numbers; the discrete value is obtained using the [floor function](https://en.wikipedia.org/wiki/Floor_function). The speed of the interpolation is determined by the [easing function](/en-US/docs/Web/CSS/easing-function) associated with the animation. ## Examples ### Valid integers ```plain example-good 12 Positive integer (without a leading + sign) +123 Positive integer (with a leading + sign) -456 Negative integer 0 Zero +0 Zero, with a leading + -0 Zero, with a leading - ``` ### Invalid integers ```plain example-bad 12.0 This is a <number>, not an <integer>, though it represents an integer. 12. Decimal points are not allowed. +---12 Only one leading +/- is allowed. ten Letters are not allowed. _5 Special characters are not allowed. \35 Escaped Unicode characters are not allowed, even if they are an integer (here: 5). \4E94 Non-arabic numerals are not allowed, even when escaped (here: the Japanese 5, 五). 3e4 Scientific notation is not allowed. ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("&lt;number&gt;")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/margin-left/index.md
--- title: margin-left slug: Web/CSS/margin-left page-type: css-property browser-compat: css.properties.margin-left --- {{CSSRef}} The **`margin-left`** [CSS](/en-US/docs/Web/CSS) property sets the [margin area](/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model#margin_area) on the left side of an element. A positive value places it farther from its neighbors, while a negative value places it closer. {{EmbedInteractiveExample("pages/css/margin-left.html")}} The vertical margins of two adjacent boxes may fuse. This is called [_margin collapsing_](/en-US/docs/Web/CSS/CSS_box_model/Mastering_margin_collapsing). In the rare cases where width is overconstrained (i.e., when all of `width`, `margin-left`, `border`, `padding`, the content area, and `margin-right` are defined), `margin-left` is ignored, and will have the same calculated value as if the `auto` value was specified. ## Syntax ```css /* <length> values */ margin-left: 10px; /* An absolute length */ margin-left: 1em; /* relative to the text size */ margin-left: 5%; /* relative to the nearest block container's width */ /* Keyword values */ margin-left: auto; /* Global values */ margin-left: inherit; margin-left: initial; margin-left: revert; margin-left: revert-layer; margin-left: unset; ``` The `margin-left` property is specified as the keyword `auto`, or a `<length>`, or a `<percentage>`. Its value can be positive, zero, or negative. ### Values - {{cssxref("&lt;length&gt;")}} - : The size of the margin as a fixed value. - {{cssxref("&lt;percentage&gt;")}} - : The size of the margin as a percentage, relative to the inline size (_width_ in a horizontal language, defined by {{cssxref("writing-mode")}}) of the [containing block](/en-US/docs/Web/CSS/Containing_block). - `auto` - : The left margin receives a share of the unused horizontal space, as determined mainly by the layout mode that is used. If the values of `margin-left` and `margin-right` are both `auto`, the calculated space is evenly distributed. This table summarizes the different cases: <table class="standard-table"> <thead> <tr> <th scope="col">Value of {{cssxref("display")}}</th> <th scope="col">Value of {{cssxref("float")}}</th> <th scope="col">Value of {{cssxref("position")}}</th> <th scope="col">Computed value of <code>auto</code></th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <th> <code>inline</code>, <code>inline-block</code>, <code>inline-table</code> </th> <th><em>any</em></th> <th><code>static</code> or <code>relative</code></th> <td><code>0</code></td> <td>Inline layout mode</td> </tr> <tr> <th> <code>block</code>, <code>inline</code>, <code>inline-block</code>, <code>block</code>, <code>table</code>, <code>inline-table</code>, <code>list-item</code>, <code>table-caption</code> </th> <th><em>any</em></th> <th><code>static</code> or <code>relative</code></th> <td> <code>0</code>, except if both <code>margin-left</code> and <code>margin-right</code> are set to <code>auto</code>. In this case, it is set to the value centering the element inside its parent. </td> <td>Block layout mode</td> </tr> <tr> <th> <code>block</code>, <code>inline</code>, <code>inline-block</code>, <code>block</code>, <code>table</code>, <code>inline-table</code>, <code>list-item</code>, <code>table-caption</code> </th> <th><code>left</code> or <code>right</code></th> <th><code>static</code> or <code>relative</code></th> <td><code>0</code></td> <td>Block layout mode (floating element)</td> </tr> <tr> <th> <em>any </em><code>table-*</code><em>, except </em ><code>table-caption</code> </th> <th><em>any</em></th> <th><em>any</em></th> <td><code>0</code></td> <td> Internal <code>table-*</code> elements don't have margins, use {{ cssxref("border-spacing") }} instead </td> </tr> <tr> <th> <em>any, except <code>flex</code>,</em> <code>inline-flex</code ><em>, or </em><code>table-*</code> </th> <th><em>any</em></th> <th> <em><code>fixed</code></em> or <code>absolute</code> </th> <td> <code>0</code>, except if both <code>margin-left</code> and <code>margin-right</code> are set to <code>auto</code>. In this case, it is set to the value centering the border area inside the available <code>width</code>, if fixed. </td> <td>Absolutely positioned layout mode</td> </tr> <tr> <th><code>flex</code>, <code>inline-flex</code></th> <th><em>any</em></th> <th><em>any</em></th> <td> <code>0</code>, except if there is any positive horizontal free space. In this case, it is evenly distributed to all horizontal <code>auto</code> margins. </td> <td>Flexbox layout mode</td> </tr> </tbody> </table> ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting margin-left as a percentage Percentage values for `margin-left` are relative to the container's inline size. #### HTML ```html <p> A large rose-tree stood near the entrance of the garden: the roses growing on it were white, but there were three gardeners at it, busily painting them red. </p> <p class="example"> Alice thought this a very curious thing, and she went nearer to watch them, and just as she came up to them she heard one of them say, "Look out now, Five! Don't go splashing paint over me like that!" </p> <p> "I couldn't help it," said Five, in a sulky tone; "Seven jogged my elbow." </p> ``` #### CSS ```css .example { margin-left: 50%; } ``` #### Result {{EmbedLiveSample("Setting margin-left as a percentage","","250")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("margin-top")}}, {{cssxref("margin-right")}}, and {{cssxref("margin-bottom")}} and the {{cssxref("margin")}} shorthand - The mapped logical properties: {{cssxref("margin-block-start")}}, {{cssxref("margin-block-end")}}, {{cssxref("margin-inline-start")}}, and {{cssxref("margin-inline-end")}} and the shorthands {{cssxref("margin-block")}} and {{cssxref("margin-inline")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/hyphens/index.md
--- title: hyphens slug: Web/CSS/hyphens page-type: css-property browser-compat: css.properties.hyphens --- {{CSSRef}} The **`hyphens`** [CSS](/en-US/docs/Web/CSS) property specifies how words should be hyphenated when text wraps across multiple lines. It can prevent hyphenation entirely, hyphenate at manually-specified points within the text, or let the browser automatically insert hyphens where appropriate. {{EmbedInteractiveExample("pages/css/hyphens.html")}} > **Note:** In the above demo, the string "An extraordinarily long English word!" contains the hidden `&shy;` (soft hyphen) character: `An extra&shy;ordinarily long English word!`. This character is used to indicate a potential place to insert a hyphen when `hyphens: manual;` is specified. Hyphenation rules are language-specific. In HTML, the language is determined by the [`lang`](/en-US/docs/Web/HTML/Global_attributes/lang) attribute, and browsers will hyphenate only if this attribute is present and the appropriate hyphenation dictionary is available. In XML, the [`xml:lang`](/en-US/docs/Web/SVG/Attribute/xml:lang) attribute must be used. > **Note:** The rules defining how hyphenation is performed are not explicitly defined by the specification, so the exact hyphenation may vary from browser to browser. If supported, {{cssxref("hyphenate-character")}} may be used to specify an alternative hyphenation character to use at the end of the line being broken. ## Syntax ```css /* Keyword values */ hyphens: none; hyphens: manual; hyphens: auto; /* Global values */ hyphens: inherit; hyphens: initial; hyphens: revert; hyphens: revert-layer; hyphens: unset; ``` The `hyphens` property is specified as a single keyword value chosen from the list below. ### Values - `none` - : Words are not broken at line breaks, even if characters inside the words suggest line break points. Lines will only wrap at whitespace. - `manual` - : Default value. Words are broken for line-wrapping only where characters inside the word suggest line break opportunities. See [Suggesting line break opportunities](#suggesting_line_break_opportunities) below for details. - `auto` - : The browser is free to automatically break words at appropriate hyphenation points, following whatever rules it chooses. However, suggested line break opportunities (see [Suggesting line break opportunities](#suggesting_line_break_opportunities) below) will override automatic break point selection when present. > **Note:** The `auto` setting's behavior depends on the language being properly tagged to select the appropriate hyphenation rules. You must specify a language using the `lang` HTML attribute to guarantee that automatic hyphenation is applied in that language. > **Note:** If you apply [`word-break: break-all`](/en-US/docs/Web/CSS/word-break#break-all) then no hyphens are shown, even if the word breaks at a hyphenation point. ## Suggesting line break opportunities There are two Unicode characters used to manually specify potential line break points within text: - U+2010 (HYPHEN) - : The "hard" hyphen character indicates a visible line break opportunity. Even if the line is not actually broken at that point, the hyphen is still rendered. - U+00AD (SHY) - : An invisible, "**s**oft" **hy**phen. This character is not rendered visibly; instead, it marks a place where the browser should break the word if hyphenation is necessary. In HTML, use `&shy;` to insert a soft hyphen. > **Note:** When the HTML [`<wbr>`](/en-US/docs/Web/HTML/Element/wbr) element leads to a line break, no hyphen is added. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Specifying text hyphenation This example uses three classes, one for each possible configuration of the `hyphens` property. #### HTML ```html <dl> <dt><code>none</code>: no hyphen; overflow if needed</dt> <dd lang="en" class="none">An extreme&shy;ly long English word</dd> <dt> <code>manual</code>: hyphen only at &amp;hyphen; or &amp;shy; (if needed) </dt> <dd lang="en" class="manual">An extreme&shy;ly long English word</dd> <dt><code>auto</code>: hyphens where the algorithm decides (if needed)</dt> <dd lang="en" class="auto">An extreme&shy;ly long English word</dd> </dl> ``` #### CSS ```css dd { width: 55px; border: 1px solid black; } dd.none { hyphens: none; } dd.manual { hyphens: manual; } dd.auto { hyphens: auto; } ``` #### Result {{EmbedLiveSample("Specifying_text_hyphenation", "100%", 490)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("content")}} - {{cssxref("overflow-wrap")}} (formerly `word-wrap`) - {{cssxref("word-break")}} - [Guide to wrapping and breaking text](/en-US/docs/Web/CSS/CSS_text/Wrapping_breaking_text)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/overscroll-behavior-x/index.md
--- title: overscroll-behavior-x slug: Web/CSS/overscroll-behavior-x page-type: css-property browser-compat: css.properties.overscroll-behavior-x --- {{CSSRef}} The **`overscroll-behavior-x`** CSS property sets the browser's behavior when the horizontal boundary of a scrolling area is reached. See {{cssxref("overscroll-behavior")}} for a full explanation. ## Syntax ```css /* Keyword values */ overscroll-behavior-x: auto; /* default */ overscroll-behavior-x: contain; overscroll-behavior-x: none; /* Global values */ overscroll-behavior-x: inherit; overscroll-behavior-x: initial; overscroll-behavior-x: revert; overscroll-behavior-x: revert-layer; overscroll-behavior-x: unset; ``` The `overscroll-behavior-x` property is specified as a keyword chosen from the list of values below. ### Values - `auto` - : The default scroll overflow behavior occurs as normal. - `contain` - : Default scroll overflow behavior (e.g., "bounce" effects) is observed inside the element where this value is set. However, no {{Glossary("Scroll_chaining", "scroll chaining")}} occurs on neighboring scrolling areas; the underlying elements will not scroll. The `contain` value disables native browser navigation, including the vertical pull-to-refresh gesture and horizontal swipe navigation. - `none` - : No scroll chaining occurs to neighboring scrolling areas, and default scroll overflow behavior is prevented. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Preventing an underlying element from scrolling horizontally In our simple [overscroll-behavior-x example](https://mdn.github.io/css-examples/overscroll-behavior/overscroll-behavior-x) (see [source code](https://github.com/mdn/css-examples/blob/main/overscroll-behavior/overscroll-behavior-x.html) also), we have two block-level boxes, one inside the other. The outer box has a large {{cssxref("width")}} set on it so the page will scroll horizontally. The inner box has a small width (and {{cssxref("height")}}) set on it so it sits comfortably inside the viewport, but its content is given a large `width` so it will scroll horizontally. By default, when the inner box is scrolled and a scroll boundary is reached, the whole page will begin to scroll, which is probably not what we want. To avoid this, you can set `overscroll-behavior-x: contain` on the inner box: ```css main > div { height: 300px; width: 500px; overflow: auto; position: relative; top: 100px; left: 100px; overscroll-behavior-x: contain; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Take control of your scroll: customizing pull-to-refresh and overflow effects](https://developer.chrome.com/blog/overscroll-behavior/#full-demo) - The mapped logical properties: {{cssxref("overscroll-behavior-inline")}}, {{cssxref("overscroll-behavior-block")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/counter-increment/index.md
--- title: counter-increment slug: Web/CSS/counter-increment page-type: css-property browser-compat: css.properties.counter-increment --- {{CSSRef}} The **`counter-increment`** [CSS](/en-US/docs/Web/CSS) property can be used to increase or decrease the value of the named [CSS counters](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) by the specified values, or to prevent all counters or an individual counter's value from being changed. If a named counter in the list of space-separated counters and values doesn't exist, it will be created. If no value is provided for a counter in the list of counters, the counter will be increased by `1`. The counter's value can be reset to any integer value with the {{cssxref("counter-reset")}} CSS property. {{EmbedInteractiveExample("pages/css/counter-increment.html")}} ## Syntax ```css /* Increases "my-counter" by 1 */ counter-increment: my-counter; /* Decreases "my-counter" by 1 */ counter-increment: my-counter -1; /* Increases "counter1" by 1 and decreases "counter2" by 4 */ counter-increment: counter1 counter2 -4; /* Increases "page" by 1, "section" by 2, while "chapter" doesn't change */ counter-increment: chapter 0 section 2 page; /* Do not increment/decrement anything: used to override less specific rules */ counter-increment: none; /* Global values */ counter-increment: inherit; counter-increment: initial; counter-increment: revert; counter-increment: revert-layer; counter-increment: unset; ``` ### Values The `counter-increment` property takes as its value either a list of space-separated counter names specified as `<custom-ident>` with an optional `<integer>` value or the keyword `none`. You may specify as many counters to increment as you want, with each name or name-number pair separated by a space. - {{cssxref("&lt;custom-ident&gt;")}} - : Specifies the name of the counter to increase or decrease. - {{cssxref("&lt;integer&gt;")}} - : Specifies the value to add to the counter. If the integer is preceded by a `-` sign, the value will be subtracted from the counter. Defaults to `1` if no value is specified. - `none` - : Indicates that no counter must be increased or decreased. This value can also be used to cancel all counters from being increased or decreased in more specific rules. This is the default value of the property. > **Note:** Using the `none` value prevents all counters from being increased or decreased for the selected elements where this rule applies. To prevent only specific counters from being increased or decreased, set the `integer` value as `0` on the relevant counter(s). ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Decreasing the counter value In this example, we display a sequence of numbers counting backward. To do this, we use a counter to display numbers starting from 100 and decreasing by 7 each time. #### HTML ```html <div> <i></i><i></i><i></i><i></i><i></i><i></i><i></i> <i></i><i></i><i></i><i></i ><i></i><i></i><i></i> <i></i><i></i><i></i><i></i><i></i><i></i><i></i> <i></i><i></i><i></i><i></i><i></i><i></i><i></i> </div> ``` #### CSS We set the initial value of the counter named `sevens` to `100` by using {{cssxref("counter-reset")}}. Then, for each {{HTMLElement("i")}}, we decrease the counter by `7`. To set the first count at `100`, we target the first `<i>` element by using the {{cssxref(":first-of-type")}} pseudo-class and setting `counter-increment: none;`. Additionally, the {{cssxref("content")}} property is used in the {{cssxref("::before")}} pseudo-element to display the value of the counter using the [`counter()`](/en-US/docs/Web/CSS/counter) function. ```css div { counter-reset: sevens 100; } i { counter-increment: sevens -7; } i:first-of-type { counter-increment: none; } i::before { content: counter(sevens); } ``` ```css hidden div { display: flex; flex-direction: column; flex-wrap: wrap; height: 300px; width: 200px; } i { flex: 0 0 2em; } ``` #### Result {{EmbedLiveSample("Decreasing the counter value", 140, 300)}} Had we not used `counter-reset` (or {{cssxref("counter-set")}}) to create the counter and set the value to `100`, the `sevens` counter would still have been created, but with an initial value `0`. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - Counter properties: {{cssxref("counter-set")}}, {{cssxref("counter-reset")}} - Counter at-rule: {{cssxref("@counter-style")}} - Counter functions: {{cssxref("counter", "counter()")}}, {{cssxref("counters", "counters()")}} - [Using CSS counters](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) guide - [CSS lists and counters](/en-US/docs/Web/CSS/CSS_lists) module - [CSS counter styles](/en-US/docs/Web/CSS/CSS_counter_styles) module
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/grid-area/index.md
--- title: grid-area slug: Web/CSS/grid-area page-type: css-shorthand-property browser-compat: css.properties.grid-area --- {{CSSRef}} The **`grid-area`** CSS [shorthand property](/en-US/docs/Web/CSS/Shorthand_properties) specifies a grid item's size and location within a {{glossary("grid", "grid")}} by contributing a line, a span, or nothing (automatic) to its grid placement, thereby specifying the edges of its {{glossary("grid areas", "grid area")}}. {{EmbedInteractiveExample("pages/css/grid-area.html")}} If four `<grid-line>` values are specified, `grid-row-start` is set to the first value, `grid-column-start` is set to the second value, `grid-row-end` is set to the third value, and `grid-column-end` is set to the fourth value. When `grid-column-end` is omitted, if `grid-column-start` is a {{cssxref("&lt;custom-ident&gt;")}}, `grid-column-end` is set to that `<custom-ident>`; otherwise, it is set to `auto`. When `grid-row-end` is omitted, if `grid-row-start` is a `<custom-ident>`, `grid-row-end` is set to that `<custom-ident>`; otherwise, it is set to `auto`. When `grid-column-start` is omitted, if `grid-row-start` is a `<custom-ident>`, all four longhands are set to that value. Otherwise, it is set to `auto`. The grid-area property can also be set to a {{cssxref("&lt;custom-ident&gt;")}} which acts as a name for the area, which can then be placed using {{cssxref("grid-template-areas")}}. ## Constituent properties This property is a shorthand for the following CSS properties: - [`grid-row-start`](/en-US/docs/Web/CSS/grid-row-start) - [`grid-column-start`](/en-US/docs/Web/CSS/grid-column-start) - [`grid-row-end`](/en-US/docs/Web/CSS/grid-row-end) - [`grid-column-end`](/en-US/docs/Web/CSS/grid-column-end) ## Syntax ```css /* Keyword values */ grid-area: auto; grid-area: auto / auto; grid-area: auto / auto / auto; grid-area: auto / auto / auto / auto; /* <custom-ident> values */ grid-area: some-grid-area; grid-area: some-grid-area / another-grid-area; /* <integer> && <custom-ident>? values */ grid-area: 4 some-grid-area; grid-area: 4 some-grid-area / 2 another-grid-area; /* span && [ <integer> || <custom-ident> ] values */ grid-area: span 3; grid-area: span 3 / span some-grid-area; grid-area: 2 span / another-grid-area span; /* Global values */ grid-area: inherit; grid-area: initial; grid-area: revert; grid-area: revert-layer; grid-area: unset; ``` ### Values - `auto` - : Is a keyword indicating that the property contributes nothing to the grid item's placement, indicating auto-placement or a default span of `1`. - `<custom-ident>` - : If there is a named line with the name '`<custom-ident>-start`'/'`<custom-ident>-end`', it contributes the first such line to the grid item's placement. > **Note:** Named grid areas automatically generate implicit named lines of this form, so specifying `grid-area: foo;` will choose the start/end edge of that named grid area (unless another line named `foo-start`/`foo-end` was explicitly specified before it). Otherwise, this is treated as if the integer `1` had been specified along with the `<custom-ident>`. - `<integer> && <custom-ident>?` - : Contributes the n-th grid line to the grid item's placement. If a negative integer is given, it instead counts in reverse, starting from the end edge of the explicit grid. If a name is given as a {{cssxref("&lt;custom-ident&gt;")}}, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines are assumed to have that name for the purpose of finding this position. An {{cssxref("&lt;integer&gt;")}} value of `0` is invalid. - `span && [ <integer> || <custom-ident> ]` - : Contributes a grid span to the grid item's placement such that the corresponding edge of the grid item's grid area is _n_ lines from the opposite edge. If a name is given as a {{cssxref("&lt;custom-ident&gt;")}}, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines on the side of the explicit grid corresponding to the search direction are assumed to have that name for the purpose of counting this span. If the {{cssxref("&lt;integer&gt;")}} is omitted, it defaults to `1`. Negative integers or 0 are invalid. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting grid areas #### HTML ```html <div id="grid"> <div id="item1"></div> <div id="item2"></div> <div id="item3"></div> </div> ``` #### CSS ```css #grid { display: grid; height: 100px; grid-template: repeat(4, 1fr) / 50px 100px; } #item1 { background-color: lime; grid-area: 2 / 2 / auto / span 3; } #item2 { background-color: yellow; } #item3 { background-color: blue; } ``` #### Result {{EmbedLiveSample("Setting_grid_areas", "100%", "150px")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - Related CSS properties: {{cssxref("grid-row")}}, {{cssxref("grid-row-start")}}, {{cssxref("grid-row-end")}}, {{cssxref("grid-column")}}, {{cssxref("grid-column-start")}}, {{cssxref("grid-column-end")}}, {{cssxref("grid-template-areas")}} - Grid Layout Guide: _[Grid template areas](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_template_areas)_ - Video tutorial: _[Grid Template Areas](https://gridbyexample.com/video/grid-template-areas/)_
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/actual_value/index.md
--- title: Actual value slug: Web/CSS/actual_value page-type: guide spec-urls: https://www.w3.org/TR/CSS22/cascade.html#actual-value --- {{CSSRef}} The **actual value** of a [CSS](/en-US/docs/Web/CSS) property is the [used value](/en-US/docs/Web/CSS/used_value) of that property after any necessary approximations have been applied. For example, a {{glossary("user agent")}} that can only render borders with a whole-number pixel width may round the thickness of the border to the nearest integer. ## Calculating a property's actual value The {{glossary("user agent")}} performs four steps to calculate a property's actual (final) value: 1. First, the [specified value](/en-US/docs/Web/CSS/specified_value) is determined based on the result of [cascading](/en-US/docs/Web/CSS/Cascade), [inheritance](/en-US/docs/Web/CSS/Inheritance), or using the [initial value](/en-US/docs/Web/CSS/initial_value). 2. Next, the [computed value](/en-US/docs/Web/CSS/computed_value) is calculated according to the specification (for example, a `span` with `position: absolute` will have its computed `display` changed to `block`). 3. Then, layout is calculated, resulting in the [used value](/en-US/docs/Web/CSS/used_value). 4. Finally, the used value is transformed according to the limitations of the local environment, resulting in the actual value. ## Specifications {{Specifications}} ## See also - CSS key concepts: - [CSS syntax](/en-US/docs/Web/CSS/Syntax) - [At-rules](/en-US/docs/Web/CSS/At-rule) - [Comments](/en-US/docs/Web/CSS/Comments) - [Specificity](/en-US/docs/Web/CSS/Specificity) - [Inheritance](/en-US/docs/Web/CSS/Inheritance) - [Box model](/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model) - [Layout modes](/en-US/docs/Web/CSS/Layout_mode) - [Visual formatting models](/en-US/docs/Web/CSS/Visual_formatting_model) - [Margin collapsing](/en-US/docs/Web/CSS/CSS_box_model/Mastering_margin_collapsing) - Values - [Initial values](/en-US/docs/Web/CSS/initial_value) - [Computed values](/en-US/docs/Web/CSS/computed_value) - [Used values](/en-US/docs/Web/CSS/used_value) - [Value definition syntax](/en-US/docs/Web/CSS/Value_definition_syntax) - [Shorthand properties](/en-US/docs/Web/CSS/Shorthand_properties) - [Replaced elements](/en-US/docs/Web/CSS/Replaced_element)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/length/index.md
--- title: <length> slug: Web/CSS/length page-type: css-type browser-compat: css.types.length --- {{CSSRef}} The **`<length>`** [CSS](/en-US/docs/Web/CSS) [data type](/en-US/docs/Web/CSS/CSS_Types) represents a distance value. Lengths can be used in numerous CSS properties, such as {{Cssxref("width")}}, {{Cssxref("height")}}, {{Cssxref("margin")}}, {{Cssxref("padding")}}, {{Cssxref("border-width")}}, {{Cssxref("font-size")}}, and {{Cssxref("text-shadow")}}. > **Note:** Although {{cssxref("&lt;percentage&gt;")}} values are usable in some of the same properties that accept `<length>` values, they are not themselves `<length>` values. See {{cssxref("&lt;length-percentage&gt;")}}. ## Syntax The `<length>` data type consists of a {{cssxref("&lt;number&gt;")}} followed by one of the units listed below. As with all CSS dimensions, there is no space between the number and the unit literal. Specifying the length unit is optional if the number is `0`. > **Note:** Some properties allow negative `<length>` values, while others do not. The [specified value](/en-US/docs/Web/CSS/specified_value) of a length (_specified length_) is represented by its quantity and unit. The [computed value](/en-US/docs/Web/CSS/computed_value) of a length (_computed length_) is the specified length resolved to an absolute length, and its unit is not distinguished. The `<length>` units can be relative or absolute. Relative lengths represent a measurement in terms of some other distance. Depending on the unit, this distance can be the size of a specific character, the [line height](/en-US/docs/Web/CSS/line-height), or the size of the {{Glossary("viewport")}}. Style sheets that use relative length units can more easily scale from one output environment to another. > **Note:** Child elements do not inherit the relative values as specified for their parent; they inherit the computed values. The relative length units listed here are based on font and viewport. ### Relative length units based on font Font lengths define the `<length>` value in terms of the size of a particular character or font attribute in the font currently in effect in an element or its parent. > **Note:** These units, especially `em` and `rem`, are often used to create scalable layouts, which maintain the vertical rhythm of the page even when the user changes the font size. - `cap` - : Represents the "cap height" (nominal height of capital letters) of the element's {{Cssxref("font")}}. - `ch` - : Represents the width or more precisely the {{Glossary("advance measure")}} of the glyph `0` (zero, the Unicode character U+0030) in the element's {{Cssxref("font")}}. In cases where it is impossible or impractical to determine the measure of the `0` glyph, it must be assumed to be `0.5em` wide by `1em` tall. - `em` - : Represents the calculated {{Cssxref("font-size")}} of the element. If used on the {{Cssxref("font-size")}} property itself, it represents the _inherited_ font-size of the element. - `ex` - : Represents the [x-height](https://en.wikipedia.org/wiki/X-height) of the element's {{Cssxref("font")}}. In fonts with the `x` letter, this is generally the height of lowercase letters in the font; `1ex ≈ 0.5em` in many fonts. - `ic` - : Equal to the used {{Glossary("advance measure")}} of the "水" glyph (CJK water ideograph, U+6C34), found in the font used to render it. - `lh` - : Equal to the computed value of the {{Cssxref("line-height")}} property of the element on which it is used, converted to an absolute length. This unit enables length calculations based on the theoretical size of an ideal empty line. However, the size of actual line boxes may differ based on their content. ### Relative length units based on root element's font - `rcap` - : Equal to the "cap height" (nominal height of capital letters) of the root element's {{Cssxref("font")}}. - `rch` - : Equal to the width or the {{Glossary("advance measure")}} of the glyph `0` (zero, the Unicode character U+0030) in the root element's {{Cssxref("font")}}. - `rem` - : Represents the {{Cssxref("font-size")}} of the root element (typically {{HTMLElement("html")}}). When used within the root element {{Cssxref("font-size")}}, it represents its initial value. A common browser default is `16px`, but user-defined preferences may modify this. - `rex` - : Represents the x-height of the root element's {{Cssxref("font")}}. - `ric` - : Equal to the value of [`ic`](#ic) unit on the root element's font. - `rlh` - : Equal to the value of [`lh`](#lh) unit on the root element's font. This unit enables length calculations based on the theoretical size of an ideal empty line. However, the size of actual line boxes may differ based on their content. ### Relative length units based on viewport The viewport-percentage length units are based on four different viewport sizes: small, large, dynamic, and default. The allowance for the different viewport sizes is in response to browser interfaces expanding and retracting dynamically and hiding and showing the content underneath. - **Small** - : When you want the smallest possible viewport in response to browser interfaces expanding dynamically, you should use the small viewport size. The small viewport size allows the content you design to fill the entire viewport when browser interfaces are expanded. Choosing this size might also possibly leave empty spaces when browser interfaces retract. For example, an element that is sized using viewport-percentage units based on the small viewport size, the element will fill the screen perfectly without any of its content being obscured when all the dynamic browser interfaces are shown. When those browser interfaces are hidden, however, there might be extra space visible around the element. Therefore, the small viewport-percentage units are "safer" to use in general, but might not produce the most attractive layout after a user starts interacting with the page. The small viewport size is represented by the `sv` prefix and results in the `sv*` viewport-percentage length units. The sizes of the small viewport-percentage units are fixed, and therefore stable, unless the viewport itself is resized. - **Large** - : When you want the largest possible viewport in response to browser interfaces retracting dynamically, you should use the large viewport size. The large viewport size allows the content you design to fill the entire viewport when browser interfaces are retracting. You need to be aware though that the content might get hidden when browser interfaces expand. For example, on mobile phones where the screen real-estate is at a premium, browsers often hide part or all of the title and address bar after a user starts scrolling the page. When an element is sized using a viewport-percentage unit based on the large viewport size, the content of the element will fill the entire visible page when these browser interfaces are hidden. However, when these retractable browser interfaces are shown, they can hide the content that is sized or positioned using the _large_ viewport-percentage units. The large viewport unit is represented by the `lv` prefix and results in the `lv*` viewport-percentage units. The sizes of the large viewport-percentage units are fixed, and therefore stable, unless the viewport itself is resized. - **Dynamic** - : When you want the viewport to be automatically sized in response to browser interfaces dynamically expanding or retracting, you can use the dynamic viewport size. The dynamic viewport size allows the content you design to fit exactly within the viewport, irrespective of the presence of dynamic browser interfaces. The dynamic viewport unit is represented by the `dv` prefix and results in the `dv*` viewport-percentage units. The sizes of the dynamic viewport-percentage units are not stable, even when the viewport itself is unchanged. > **Note:** While the dynamic viewport size can give you more control and flexibility, using viewport-percentage units based on the dynamic viewport size can cause the content to resize while a user is scrolling a page. This can lead to degradation of the user interface and cause a performance hit. - **Default** - : The default viewport size is defined by the browser. The behavior of the resulting viewport-percentage unit could be equivalent to the viewport-percentage unit based on the small viewport size, the large viewport size, an intermediate size between the two, or the dynamic viewport size. > **Note:** For example, a browser might implement the default viewport-percentage unit for height (`vh`) that is equivalent to the large viewport-percentage height unit (`lvh`). If so, this could obscure content on a full-page display while the browser interface is expanded. Viewport-percentage lengths define `<length>` values in percentage relative to the size of the initial [containing block](/en-US/docs/Web/CSS/Containing_block), which in turn is based on either the size of the {{Glossary("viewport")}} or the page area, i.e., the visible portion of the document. When the height or width of the initial containing block is changed, the elements that are sized based on them are scaled accordingly. There is a viewport-percentage length unit variant corresponding to each of the viewport sizes, as described below. > **Note:** Viewport lengths are invalid in {{cssxref("@page")}} declaration blocks. - `vh` - : Represents a percentage of the height of the viewport's initial [containing block](/en-US/docs/Web/CSS/Containing_block). `1vh` is 1% of the viewport height. For example, if the viewport height is `300px`, then a value of `70vh` on a property will be `210px`. For small, large, and dynamic viewport sizes, the respective viewport-percentage units are `svh`, `lvh`, and `dvh`. `vh` represents the viewport-percentage length unit based on the browser default viewport size. - `vw` - : Represents a percentage of the width of the viewport's initial [containing block](/en-US/docs/Web/CSS/Containing_block). `1vw` is 1% of the viewport width. For example, if the viewport width is `800px`, then a value of `50vw` on a property will be `400px`. For small, large, and dynamic viewport sizes, the respective viewport-percentage units are `svw`, `lvw`, and `dvw`. `vw` represents the viewport-percentage length unit based on the browser default viewport size. - `vmax` - : Represents in percentage the largest of `vw` and `vh`. For small, large, and dynamic viewport sizes, the respective viewport-percentage units are `svmax`, `lvmax`, and `dvmax`. `vmax` represents the viewport-percentage length unit based on the browser default viewport size. - `vmin` - : Represents in percentage the smallest of `vw` and `vh`. For small, large, and dynamic viewport sizes, the respective viewport-percentage units are `svmin`, `lvmin`, and `dvmin`. `vmin` represents the viewport-percentage length unit based on the browser default viewport size. - `vb` - : Represents percentage of the size of the initial [containing block](/en-US/docs/Web/CSS/Containing_block), in the direction of the root element's [block axis](/en-US/docs/Web/CSS/CSS_logical_properties_and_values#block_vs._inline). For small, large, and dynamic viewport sizes, the respective viewport-percentage units are `svb`, `lvb`, and `dvb`, respectively. `vb` represents the viewport-percentage length unit based on the browser default viewport size. - `vi` - : Represents a percentage of the size of the initial [containing block](/en-US/docs/Web/CSS/Containing_block), in the direction of the root element's [inline axis](/en-US/docs/Web/CSS/CSS_logical_properties_and_values#block_vs._inline). For small, large, and dynamic viewport sizes, the respective viewport-percentage units are `svi`, `lvi`, and `dvi`. `vi` represents the viewport-percentage length unit based on the browser default viewport size. ### Container query length units When applying styles to a container using container queries, you can use container query length units. These units specify a length relative to the dimensions of a query container. Components that use units of length relative to their container are more flexible to use in different containers without having to recalculate concrete length values. For more information, see [Container queries](/en-US/docs/Web/CSS/CSS_containment/Container_queries). - `cqw` - : Represents a percentage of the width of the query container. `1cqw` is 1% of the query container's width. For example, if the query container's width is `800px`, then a value of `50cqw` on a property will be `400px`. - `cqh` - : Represents a percentage of the height of the query container. `1cqh` is 1% of the query container's height. For example, if the query container's height is `300px`, then a value of `10cqh` on a property will be `30px`. - `cqi` - : Represents a percentage of the inline size of the query container. `1cqi` is 1% of the query container's inline size. For example, if the query container's inline size is `800px`, then a value of `50cqi` on a property will be `400px`. - `cqb` - : Represents a percentage of the block size of the query container. `1cqb` is 1% of the query container's block size. For example, if the query container's block size is `300px`, then a value of `10cqb` on a property will be `30px`. - `cqmin` - : Represents a percentage of the smaller value of either the query container's inline size or block size. `1cqmin` is 1% of the smaller value of either the query container's inline size or block size. For example, if the query container's inline size is `800px` and its block size is `300px`, then a value of `50cqmin` on a property will be `150px`. - `cqmax` - : Represents a percentage of the larger value of either the query container's inline size or block size. `1cqmax` is 1% of the larger value of either the query container's inline size or block size. For example, if the query container's inline size is `800px` and its block size is `300px`, then a value of `50cqmax` on a property will be `400px`. ### Absolute length units Absolute length units represent a physical measurement when the physical properties of the output medium are known, such as for print layout. This is done by anchoring one of the units to a physical unit and then defining the others relative to it. The anchoring is done differently for low-resolution devices, such as screens, versus high-resolution devices, such as printers. For low-dpi devices, the unit `px` represents the physical _reference pixel_; other units are defined relative to it. Thus, `1in` is defined as `96px`, which equals `72pt`. The consequence of this definition is that on such devices, dimensions described in inches (`in`), centimeters (`cm`), or millimeters (`mm`) don't necessarily match the size of the physical unit with the same name. For high-dpi devices, inches (`in`), centimeters (`cm`), and millimeters (`mm`) are the same as their physical counterparts. Therefore, the `px` unit is defined relative to them (1/96 of `1in`). > **Note:** Many users increase their {{Glossary("user agent")}}'s default font size to make text more legible. Absolute lengths can cause accessibility problems because they are fixed and do not scale according to user settings. For this reason, prefer relative lengths (such as `em` or `rem`) when setting `font-size`. - `px` - : One pixel. For screen displays, it traditionally represents one device pixel (dot). However, for _printers_ and _high-resolution screens_, one CSS pixel implies multiple device pixels. `1px` = `1in / 96`. - `cm` - : One centimeter. `1cm` = `96px / 2.54`. - `mm` - : One millimeter. `1mm` = `1cm / 10`. - `Q` - : One quarter of a millimeter. `1Q` = `1cm / 40`. - `in` - : One inch. `1in` = `2.54cm` = `96px`. - `pc` - : One pica. `1pc` = `12pt` = `1in / 6`. - `pt` - : One point. `1pt` = `1in / 72`. ## Interpolation When animated, values of the `<length>` data type are interpolated as real, floating-point numbers. The interpolation happens on the calculated value. The speed of the interpolation is determined by the [easing function](/en-US/docs/Web/CSS/easing-function) associated with the animation. ## Examples ### Comparing different length units The following example provides you with an input field in which you can enter a `<length>` value (e.g. `300px`, `50%`, `30vw`) to set the width of a result bar that will appear below it once you've pressed the <kbd>Enter</kbd> or the <kbd>Return</kbd> key. This allows you to compare and contrast the effect of different length units. #### HTML ```html <div class="outer"> <div class="input-container"> <label for="length">Enter width:</label> <input type="text" id="length" /> </div> <div class="inner"></div> </div> <div class="results"></div> ``` #### CSS ```css html { font-family: sans-serif; font-weight: bold; box-sizing: border-box; } .outer { width: 100%; height: 50px; background-color: #eee; position: relative; } .inner { height: 50px; background-color: #999; box-shadow: inset 3px 3px 5px rgb(255 255 255 / 50%), inset -3px -3px 5px rgb(0 0 0 / 50%); } .result { height: 20px; box-shadow: inset 3px 3px 5px rgb(255 255 255 / 50%), inset -3px -3px 5px rgb(0 0 0 / 50%); background-color: orange; display: flex; align-items: center; margin-top: 10px; } .result code { position: absolute; margin-left: 20px; } .results { margin-top: 10px; } .input-container { position: absolute; display: flex; justify-content: flex-start; align-items: center; height: 50px; } label { margin: 0 10px 0 20px; } ``` #### JavaScript ```js const inputDiv = document.querySelector(".inner"); const inputElem = document.querySelector("input"); const resultsDiv = document.querySelector(".results"); inputElem.addEventListener("change", () => { inputDiv.style.width = inputElem.value; const result = document.createElement("div"); result.className = "result"; result.style.width = inputElem.value; result.innerHTML = `<code>width: ${inputElem.value}</code>`; resultsDiv.appendChild(result); inputElem.value = ""; inputElem.focus(); }); ``` #### Result {{EmbedLiveSample('Comparing different length units', '100%', 700)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS values & units tutorial](/en-US/docs/Learn/CSS/Building_blocks/Values_and_units) - [CSS values & units reference](/en-US/docs/Web/CSS/CSS_Values_and_Units) - [Box Model](/en-US/docs/Web/CSS/CSS_box_model)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/css_containment/index.md
--- title: CSS containment slug: Web/CSS/CSS_containment page-type: css-module spec-urls: - https://drafts.csswg.org/css-contain-2/ - https://drafts.csswg.org/css-contain-3/ --- {{CSSRef}} The **CSS containment** module defines containment and container queries. Containment enables the isolation of page subtrees from the rest of the DOM. The browser can then improve performance by optimizing the rendering of these independent parts. Container queries are similar to dimension [media queries](/en-US/docs/Web/CSS/CSS_media_queries), except that the queries are based on the dimensions of a specific container element defined as a _containment context_, rather than on the dimensions of the viewport. Container queries enable querying a container's size, properties, and property values to conditionally apply CSS styles. When applying these conditional styles, you can use container query length units, which specify lengths relative to the dimensions of the query container. Additional properties are defined to enable establishing a specific element as a query container and giving it a specific name. ## Reference ### Properties - {{cssxref("contain")}} - {{cssxref("container")}} shorthand - {{cssxref("container-name")}} - {{cssxref("container-type")}} - {{cssxref("content-visibility")}} ### At-rules and descriptors - {{cssxref("@container")}} - [`@container` descriptors](/en-US/docs/Web/CSS/@container#descriptors): - `aspect-ratio` - `block-size` - `height` - `inline-size` - `orientation` - `width` ### Functions - [`style()`](/en-US/docs/Web/CSS/@container#container_style_queries) ### Data types - [`<container-name>`](/en-US/docs/Web/CSS/@container#values) - [`<style-feature>`](/en-US/docs/Web/CSS/@container#container_style_queries) - [Container relative `<length>` units](/en-US/docs/Web/CSS/length#container_query_length_units) ### Events - {{domxref("Element.contentvisibilityautostatechange_event", "contentvisibilityautostatechange")}} ### Interfaces - {{domxref("ContentVisibilityAutoStateChangeEvent")}} - {{domxref("ContentVisibilityAutoStateChangeEvent.skipped", "skipped")}} property - {{domxref("CSSContainerRule")}} - {{domxref("CSSContainerRule.containerName")}} - {{domxref("CSSContainerRule.containerQuery")}} ## Guides - [CSS container queries](/en-US/docs/Web/CSS/CSS_containment/Container_queries) - : A guide to using container queries with `@container`, including naming containment contexts. - [Using CSS containment](/en-US/docs/Web/CSS/CSS_containment/Using_CSS_containment) - : Describes the basic aims of CSS containment and how to leverage `contain` and `content-visibility` for a better user experience. - [Using container size and style queries](/en-US/docs/Web/CSS/CSS_containment/Container_size_and_style_queries) - : A guide to writing container size and style queries with `@container`, including style queries for custom properties, query syntax and names, and nesting container queries. ## Related concepts - [Layout and the containing block](/en-US/docs/Web/CSS/Containing_block) - [Block formatting context](/en-US/docs/Web/CSS/CSS_display/Block_formatting_context) - [CSS media queries](/en-US/docs/Web/CSS/CSS_media_queries) module - {{cssxref("@media")}} at-rule - [CSS logical operators](/en-US/docs/Web/CSS/@media#logical_operators) (`not`, `or`, and `and`) - [CSS transitions](/en-US/docs/Web/CSS/CSS_transitions) module - {{cssxref("@starting-style")}} at-rule - {{cssxref("transition-behavior")}} property - CSS box sizing module - {{cssxref("contain-intrinsic-size")}} shorthand property - {{CSSxRef("contain-intrinsic-inline-size")}} property - {{CSSxRef("contain-intrinsic-size")}} property - {{CSSxRef("contain-intrinsic-width")}} property - {{CSSxRef("contain-intrinsic-height")}} property - [CSS counter styles](/en-US/docs/Web/CSS/CSS_counter_styles) module - [Using CSS counters](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) guide - [CSS nesting](/en-US/docs/Web/CSS/CSS_nesting) module - [CSS nesting at-rules](/en-US/docs/Web/CSS/CSS_nesting/Nesting_at-rules) guide ## Specifications {{Specifications}} ## See also - [Using feature queries](/en-US/docs/Web/CSS/CSS_conditional_rules/Using_feature_queries) - [Using CSS media queries](/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries) - {{cssxref("@supports")}} at-rule
0
data/mdn-content/files/en-us/web/css/css_containment
data/mdn-content/files/en-us/web/css/css_containment/using_css_containment/index.md
--- title: Using CSS containment slug: Web/CSS/CSS_containment/Using_CSS_containment page-type: guide --- {{CSSRef}} CSS containment improves the performance of web pages by allowing the browser to isolate a subtree of the page from the rest of the page. If the browser knows that a part of the page is independent from the rest of the content, rendering can be optimized and performance improved. The {{cssxref("contain")}} and {{cssxref("content-visibility")}} properties enable developers to inform user agents whether or not an element should render its contents at all, and whether it should render its contents when it is offscreen. The user agent then applies containment to elements when appropriate, potentially deferring layout and rendering until needed. This guide describes the basic aims of CSS containment and how to leverage `contain` and `content-visibility` for a better user experience. ## Basic example Web pages often contain multiple sections which are, logically, independent of each other. CSS containment enables them to be treated truly independently from each other when it comes to rendering. For example, blogs usually contain several articles, each containing a headline and content, as in the markup below. ```html <h1>My blog</h1> <article> <h2>Heading of a nice article</h2> <p>Content here.</p> </article> <article> <h2>Another heading of another article</h2> <p>More content here.</p> </article> ``` With CSS, we apply the {{cssxref("contain")}} property with a value of `content` to each article. The `content` value is shorthand for `contain: layout paint style`: ```css article { contain: content; } ``` Logically, each article is independent of the other articles on the page. This information is something that is usually known, and likely quite obvious, to the web developer creating the page. However, browsers don't know the intent of your content and cannot assume that an article or other section of content will be entirely self-contained. This property provides a way of explaining this to the browser and giving it explicit permission to make performance optimizations. It tells the browser that the internal layout of the element is completely separate from the rest of the page, and that everything about the element is painted inside its bounds. Nothing can visibly overflow. By setting `contain: content` on each `<article>` we have indicated this; we have told the browser that each article is independent. The browser can then use this information to make decisions about how to render each `<article>` of content. For example, it might not render articles that are outside the viewable area. When additional articles are appended at the end of the page, the browser does not need to recalculate layout or repaint the preceding content; it also doesn't need to touch any area outside of the containing element's subtree. If box model properties are dependent, however, the browser will need to recalculate layout and repaint. For example, if the `<article>` is styled such that its size depends on its contents (e.g. with `height: auto`), then the browser will need to account for its size changing. ## Key concepts and terminology ### `contain` values There are four types of containment: layout, paint, size, and style. Use the {{cssxref("contain")}} property to specify the type or types you want to apply to an element by including any combination of these types. #### Layout containment ```css article { contain: layout; } ``` Layout is normally scoped to the entire document, which means that if you move one element the entire document needs to be treated as if things could have moved anywhere. By using `contain: layout` you can tell the browser it only needs to check this element — everything inside the element is scoped to that element and does not affect the rest of the page, with the containing box establishing an independent [formatting context](/en-US/docs/Web/CSS/CSS_flow_layout/Introduction_to_formatting_contexts). In addition: - {{cssxref("float")}} layout will be performed independently inside the specified element. - Margins won't collapse across a layout containment boundary. - The layout container is a [containing block](/en-US/docs/Web/CSS/Containing_block) for `absolute`- and `fixed`-positioned descendants. - The containing box creates a [stacking context](/en-US/docs/Web/CSS/CSS_positioned_layout/Understanding_z-index/Stacking_context), therefore {{cssxref("z-index")}} can be used. > **Note:** The `style` and `layout` values of `contain` are automatically applied when using the {{cssxref("container-type")}} and {{cssxref("container-name")}} properties. #### Paint containment ```css article { contain: paint; } ``` Paint containment essentially clips the box to the padding edge of the [principal box](/en-US/docs/Web/CSS/Visual_formatting_model#the_principal_box). There can be no visible overflow. The same additional notes are true for `paint` containment as `layout` containment (see above). Another advantage is that if the element with containment applied is offscreen, the browser does not need to paint its child elements — these are also offscreen as they are contained completely by that box. #### Size containment ```css article { contain: size; } ``` Size containment does not offer much in the way of performance optimizations when used on its own. However, size containment means that the size of the size-contained element's children cannot affect the size of the element itself — its size is computed as if it had no children. If you set `contain: size` on an element, you need to specify the size of the element using {{cssxref("contain-intrinsic-size")}}, or the longhand properties {{cssxref("contain-intrinsic-width")}} and {{cssxref("contain-intrinsic-height")}}, in that order. If no size is set, the element risks being zero-sized in most cases. ```css article { contain: size; contain-intrinsic-size: 100vw auto; } ``` #### Style containment ```css article { contain: style; } ``` Despite the name, style containment does not provide scoped styles such as you would get with the [Shadow DOM](/en-US/docs/Web/API/Web_components/Using_shadow_DOM) or {{cssxref("@scope")}}. The main use case for the `style` value is to prevent situations where a [CSS counter](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) could be changed in an element, which could then affect the rest of the tree. Using `contain: style` ensures the {{cssxref("counter-increment")}} and {{cssxref("counter-set")}} properties create new counters scoped to that subtree only. You can include more than one containment type by including multiple space-separated values, such as `contain: layout paint` or by using one of the two [special values](#special-values). #### Special values There are two special values of `contain` that are shorthand for the first three or all four of the containment types: - `content` - `strict` We encountered the first in the example above. Using `contain: content` turns on `layout`, `paint`, and `style` containment. As it omits `size`, it is a safe value to apply widely. The `contain: strict` declaration, which behaves the same as the declaration `contain: size layout paint style` (which includes four space-separated values), provides the most containment. It is riskier to use as it applies `size` containment; the risk exists that a box could end up zero-sized due to a reliance on the size of its children. To remove this risk, always set a size when using `strict`: ```css article { contain: strict; contain-intrinsic-size: 80vw auto; } ``` The above is the same as: ```css article { contain: size layout paint style; contain-intrinsic-size: 80vw auto; } ``` ### `content-visibility` When you have a lot of content that would benefit from heavy containment that will often be offscreen — for example if all your blog posts are viewable on the blog home pages as an infinitely scrollable blog — `content-visibility: auto` can be used to apply all of the containments at once. The {{cssxref("content-visibility")}} property controls whether or not an element renders its contents at all, along with forcing a strong set of containments, allowing user agents to potentially omit large swathes of layout and rendering work until it becomes needed. It enables the user agent to skip an element's rendering work (including layout and painting) until it is needed — which makes the initial page load much faster. Its possible values are: - `visible`: The default behavior — an element's contents are laid out and rendered as normal. - `hidden`: The element [skips its contents](#skips-its-contents). The skipped contents will not be accessible to user agent features such as find-in-page, tab-order navigation, etc., nor be selectable or focusable. - `auto`: The element turns on layout containment, style containment, and paint containment, as if `contain: content` was set. If the element is not [relevant to the user](#relevant_to_the_user), it also skips its contents. Unlike `hidden`, the skipped content is still available for user interactions, remaining focusable, selectable, in regular tab order, and available to in-content search. ### Relevant to the user User-agents have a concept of content being [relevant to the user](https://drafts.csswg.org/css-contain/#relevant-to-the-user). An element becomes "relevant to the user" if any of the following are true: - The element appears in the viewport, or a user-agent-defined margin around the viewport (50% of the viewport dimensions, to give the app time to prepare for when the element visibility changes). - The element or its contents receive focus. - The element or its contents are selected, for example by dragging over the text with the mouse cursor or by some other highlight operation. - The element or its contents are placed in the {{glossary("top layer")}}. When `content-visibility: auto` is set, and the browser determines that content is relevant to the user, the browser will render that content. ### Skips its contents When you set `content-visibility: hidden` on an element, you are telling the browser that it is not relevant to the user, and therefore its [contents should be skipped](https://drafts.csswg.org/css-contain/#skips-its-contents) and not rendered. This helps to improve performance. The browser will also skip an element's contents when `content-visibility: auto` is set on it, and the browser determines that its content is _not_ relevant to the user. When an element skips its contents: - It has layout, style, paint, and size containment turned on. - Its contents are not painted, as if {{cssxref("visibility", "visibility: hidden")}} was set on it. - Its contents do not receive pointer events, as if {{cssxref("pointer-events", "pointer-events: none")}} was set on it. This happens in both the cases mentioned above, but with `content-visibility: auto` the content can be searched, receive focus, and otherwise move from not relevant to relevant. This is not the case for `content-visibility: hidden`. > **Note:** To animate the transition from `content-visibility: hidden` to a visible value, you will need to set {{cssxref("transition-behavior", "transition-behavior:&nbsp;allow-discrete")}} and {{cssxref("@starting-style")}} styles. See [transitioning `display` and `content-visibility`](/en-US/docs/Web/CSS/CSS_transitions/Using_CSS_transitions#transitioning_display_and_content-visibility) to learn more. ## See also - [CSS containment module](/en-US/docs/Web/CSS/CSS_containment) - [Learn: CSS performance optimization](/en-US/docs/Learn/Performance/CSS) - [CSS container queries](/en-US/docs/Web/CSS/CSS_containment/Container_queries) - [An Introduction to CSS Containment](https://blogs.igalia.com/mrego/2019/01/11/an-introduction-to-css-containment/) via Igalia.com (2019) - The {{domxref("element/contentvisibilityautostatechange_event", "contentvisibilityautostatechange")}} event
0
data/mdn-content/files/en-us/web/css/css_containment
data/mdn-content/files/en-us/web/css/css_containment/container_size_and_style_queries/index.md
--- title: Using container size and style queries slug: Web/CSS/CSS_containment/Container_size_and_style_queries page-type: guide browser-compat: css.at-rules.container --- {{CSSRef}}{{SeeCompatTable}} [Container queries](/en-US/docs/Web/CSS/CSS_containment/Container_queries) enable you to apply styles to elements nested within a specific container based on the features of that container. The query returns true or false depending on whether the query condition is true for the container. Container queries are similar to [media queries](/en-US/docs/Web/CSS/CSS_media_queries). The {{cssxref("@media")}} at-rule enables applying styles to elements based on viewport size or other device characteristics. Similarly, the {{cssxref("@container")}} at-rule enables applying styles to elements based on a containing element's size or other style features, rather than the viewport's. Container queries have the same syntax rules and logical operators as media queries. ```css @container <container-condition> { /* <stylesheet> */ } ``` There are two types of container queries: _container size queries_ and _container style queries_: - **Container size queries** - : Size queries enable applying styles to elements based on the current [size](/en-US/docs/Web/CSS/@container#descriptors) of a containing element, including the orientation and aspect ratio. The containing elements need to be explicitly declared as _size query containers_. - **Container style queries** - : Style queries enable applying styles to elements based on a containing element's style features. Any non-empty element can be a style query container. Currently, the only style feature supported by style queries is CSS [custom properties](/en-US/docs/Web/CSS/Using_CSS_custom_properties). In this case, the query returns true or false depending on the computed value of the containing element's custom properties. When container style queries are fully supported, they will enable you to apply styles to any element's descendants based on any property, declaration, or computed value — for example if the container is `display: inline flex` or has a non-transparent background color. In this guide, we learn the basics of container queries by looking at: 1. [container size queries](#container-size-queries), 2. [naming containers](#naming_containers) to limit their scope, and 3. using the `style()` functional notation within the {{cssxref("@container")}} at rule's `<container-condition>` to create [style queries with custom properties](#style_queries_for_custom_properties). ## Container size queries Container size queries are filtered by a size condition. The associated styles are applied to contained elements if the container element has been declared to be a container and the container condition is true for that element. An element's size container is the nearest ancestor with containment. Elements are declared as _size query containers_ by setting their {{cssxref("container-type")}} property (or the {{cssxref("container")}} shorthand) to `size` or `inline-size`. ```css @container (orientation: landscape) { /* styles applied to descendants of this size container */ } .sizeContainer { container-type: size; } ``` Declaring size query containers adds [containment](/en-US/docs/Web/CSS/CSS_containment/Using_CSS_containment) to them. This is a performance necessity — querying the size of every element in the DOM, all the time, would be bad for performance and user experience. Additionally, if a descendant style changed the size of the container element, an infinite loop could occur. In a container size query, the `<container-condition>` includes one or more `<size-query>`s. Each size query includes a size feature name, a comparison operator, and a value. The size features that can be queried are limited to `width`, `height`, `inline-size`, `block-size`, `aspect-ratio`, and `orientation`. The boolean syntax and logic combining one or more `<size-query>`s is the same as for [`@media`](/en-US/docs/Web/CSS/@media) size feature queries. ```css form { container-type: inline-size; } @container (10em <= width <= 20em) { /* styles */ } ``` The `<container-condition>` in this example contains a single `<size-query>` — `(10em <= width <= 20em)`. In this case, all {{htmlelement("form")}} elements are potential matches for any unnamed container query. The styles declared within our container query apply to the descendants of all forms between `10em` and `30em` wide, inclusive. ## Naming containers A `<container-condition>` can include an optional case-sensitive {{cssxref("container-name")}}. In the form example, we could have limited the elements matched by the query by adding a name to the `<container-condition>` and then setting the {{cssxref("container-name")}} property with its value equal to the same name on the form elements we want to match. The optional `<container-name>` set within the query condition filters the set of query containers considered to just those with a matching query container name. The {{cssxref("container-name")}} property specifies a list of query container names that can be used by `@container` rules to filter which query containers are targeted. Names enable querying aspects of a specific container, even if the container is not a direct parent. ```css @container <container-name> <container-query> { /* <stylesheet> */ } ``` Once you've added names to your `@container` at rules, you can use the {{cssxref("container-name")}} property or the {{cssxref("container")}} shorthand to apply a space-separated list of names to container elements. Styles contained inside the named `@container` at rules will only be applied to matching elements inside size query containers with the same names set on them. The `<container-name>` is a case-sensitive {{cssxref("ident")}}. ```css @container card (orientation: landscape) { /* styles */ } .cards li { container-type: inline-size; container-name: card; } ``` This example size query is limited to elements with a `container-name` of `card` applied to them. In this example, the styles within the container query style block will apply to the descendants of all {{htmlelement("li")}} elements nested within an element with a class of `cards` with a width that is greater than their height. Note that if there are other elements with `container-name: card` applied to them that match the size query, the styles will also be applied to those elements' descendants. ```css @container wide (orientation: landscape) and (min-width: 20em) { /* styles applied to descendants of .sizeContainer if size features match */ } @container narrow (orientation: portrait) or (max-width: 20em) { /* styles applied to descendants of .sizeContainer if size features match */ } .sizeContainer { container-type: size; container-name: wide narrow; } ``` In this container size query example, the element has two container names. The descendants of any elements with `class="sizeContainer"` will get the styles from the `wide` or `narrow` query applied (or both if that element is exactly a 20em square). Container names also enable querying styles from elements that aren't a direct parent. When a containment context is given a name, it can be specifically targeted using the `@container` at-rule instead of the nearest ancestor with containment. Set `container-name: none` to prevent the container from matching any named container queries. That removes all associated container query names, but does not prevent the element from matching unnamed queries. To prevent an element from being a size container, set `container-type: normal`. This removes containment, meaning the element isn't a size container (it can still be a [style container](#container_style_queries). To prevent an element from being matched by any container queries, provide it with an unused `container-name`. ```css article { container-name: none; container-type: size; } main { container-name: neverUsedName; container-type: normal; } ``` In the above example, the {{htmlelement("article")}} element can match any unnamed container query. In other words, it will be tested by each `@container` query that doesn't include a name in the `<container-condition>`. On the other hand, assuming the `neverUsedName` is never used as a container query name, the {{htmlelement("main")}} element will never be queried. Even if that name was removed, it would still not be tested against any size queries as the `container-type` value of `normal` means it is not a size query container. With container queries, we are not limited to size queries! You can also query a container's style features. ## Container style queries A _container style query_ is a `@container` query that evaluates computed styles of the container element as defined in one or more `style()` functional notations. The boolean syntax and logic used to combine style features into a style query are the same as in [CSS feature queries](/en-US/docs/Web/CSS/CSS_conditional_rules/Using_feature_queries). The only difference is the function name — `style()` within a `<style-feature>` as opposed to `supports()` within a `<support-condition>`: ```css @container style(<style-feature>), not style(<style-feature>), style(<style-feature>) and style(<style-feature>), style(<style-feature>) or style(<style-feature>) { /* <stylesheet> */ } ``` The parameter of each `style()` function is a single **`<style-feature>`**. Per the CSS containment specification, a `<style-feature>` can be a valid CSS [declaration](/en-US/docs/Web/CSS/syntax#css_declarations), a CSS property, or a [`<custom-property-name>`](/en-US/docs/Web/CSS/var#values). The only style feature currently supported is custom properties, with or without a value. See the [browser compatibility table](#browser_compatibility). If the `<style-feature>` includes a value, the style query evaluates to true if the computed value of the custom property (or, in the future, the CSS declaration) passed as the `style()` argument is true for the container being queried. Otherwise, it resolves to false. A style feature without a value evaluates to true if the computed value is different from the [initial value](#registered_properties) for the given property. In the future, we'll be able to write style queries like so: ```css @container style(color: green) and style(background-color: transparent), not style(background-color: red), style(--themeBackground), style(--themeColor: blue) or style(--themeColor: purple), (max-width: 100vw) and style(max-width: 600px) { /* <stylesheet> */ } ``` The `style()` functional notation is used to differentiate style queries from size queries. While not yet supported, we will eventually be able to query regular CSS declarations such as `max-width: 100vw`. Querying `@containers (max-width: 100vw)` is a size query; containment with {{cssxref("container-type")}}, or the {{cssxref("container")}} shorthand, is needed. That query will return true if the container is 100vw or less. That is different from querying `@containers style(max-width: 100vw)`, which is a style query; when supported, this query will return true if the container has a {{cssxref("max-width")}} value of `100vw`. Until style queries for regular CSS declarations and properties are supported, we are limited to including only custom properties as the `style()` parameter, with or without a value: ```css @container style(--themeBackground), style(--themeColor: blue) or style(--themeColor: purple) { /* <stylesheet> */ } ``` A few things to note that have already been mentioned but are important to remember: - All elements can be style query containers; setting a `container-type` is not required. When descendant styles don't impact the computed styles of an ancestor, containment is not needed. - A `<container-condition>` can include both style and size features. If including size features in your query, make sure your container elements have a `container-type` of `size` or `inline-size` set. - If you don't want an element to be considered as a container, ever, give it a `container-name` that will not be used. Setting `container-name: none` removes any query names associated with a container; it does not prevent the element from being a style container. - At the time of this writing (February 2024), container style queries only work with CSS custom property values in the `style()` query. Now, let's dive in and take a look at the different `<style-feature>` types. ### Style queries for custom properties Style queries for custom properties allow you to query the [custom properties](/en-US/docs/Web/CSS/Using_CSS_custom_properties), also called "CSS variables", of a parent element. They are included within a `<style-query>` just as you would include any regular CSS property within a feature query: either with or without a value. #### Standalone custom property queries The `<style-query>` parameter of the `style()` functional notation can include just a CSS variable name; a custom property with no value. When no value is included, the query will return false if the value is the same as the value of the `initial-value` descriptor within the `@property` at-rule, if there is one. The style query will return true and match all elements that have a custom property value that differs from the `initial-value` or for all elements that have a custom property of any value if the custom property was declared without being registered. ##### Unregistered custom properties When CSS variables are introduced via a simple CSS custom property value assignment, valueless custom property queries always return true. ```css :root { --theme-color: rebeccapurple; } @container style(--theme-color) { /* <stylesheet> */ } ``` In this example, the container query matches the element on which the `--theme-color` property was declared and all of its descendants. As the CSS variable `--theme-color` was declared on the {{cssxref(":root")}}, the style query `style(--theme-color)` will be true for every element within that {{glossary("DOM")}} node. ##### Registered properties The behavior of registered custom properties is different. When explicitly defined with the {{cssxref("@property")}} CSS at-rule or via JavaScript with {{domxref('CSS/registerProperty_static', 'CSS.registerProperty()')}}, the style query `style(--theme-color)` only returns true for elements if the element's computed value for `--theme-color` is different from the [`initial-value`](/en-US/docs/Web/CSS/@property/initial-value) set in the original definition of that custom property. ```css @property --theme-color { initial-value: rebeccapurple; inherited: true; } :root { --theme-color: rebeccapurple; } main { --theme-color: blue; } @container style(--theme-color) { /* <stylesheet> */ } ``` In this example, the `:root` element does NOT match the style query because the value of the custom property is the same as the `initial-value` value. The custom property value for the element (and all the elements inheriting the value) is still `rebeccapurple`. Only elements that differ from the initial value, in this case, the {{htmlelement("main")}} and its descendants that inherit that changed value, are a match. #### Custom property with a value If a style query includes a value for the custom property, the element's computed value for that property must be an exact match, with equivalent values only being a match if the custom property was defined with a {{cssxref("@property")}} at rule (or a {{domxref('CSS/registerProperty_static', 'CSS.registerProperty()')}} method call) containing a `syntax` descriptor. ```css @container style(--accent-color: blue) { /* <stylesheet> */ } ``` This container style query matches any element that has `blue` as the {{cssxref("computed_value")}} of the `--accent-color` custom property. In this case, other color values equivalent to sRGB `blue` (such as the hexadecimal code `#0000ff`) will match only if the `--accent-color` property was defined as a color with `@property` or `CSS.registerProperty()`, for example: ```css @property --accent-color { syntax: "<color>"; inherits: true; initial-value: #00f; } ``` In this case, if the value of `--accent-color` were set to `blue`, `#00f`, `#0000ff`, `rgb(0 0 255 / 1)`, or `rgb(0% 0% 100%)` it would return true for `@container style(--accent-color: blue)`. ##### Example In this example, we have a {{htmlelement("fieldset")}} with four radio buttons. The fourth option includes a text {{htmlelement("input")}} for entering a custom color. ```html <fieldset> <legend>Change the value of <code>--theme</code></legend> <ol> <li> <input type="radio" name="selection" value="red" id="red" /> <label for="red">--theme: red;</label> </li> <li> <input type="radio" name="selection" value="green" id="green" /> <label for="green">--theme: green</label> </li> <li> <input type="radio" name="selection" value="blue" id="blue" /> <label for="blue">--theme: blue</label> </li> <li> <input type="radio" name="selection" value="currentcolor" id="other" /> <label for="other">Other</label> <label for="color">color:</label> <input text="checkbox" name="selection" value="currentcolor" id="color" /> </li> </ol> </fieldset> <output>I change colors</output> ``` JavaScript updates the value of the CSS `--theme` variable on the {{htmlelement("body")}} element, which is an ancestor of the {{htmlelement("fieldset")}} and {{htmlelement("output")}} elements, whenever a radio button is selected. When the text `<input>` is updated, the {{domxref("HTMLInputElement.value", "value")}} of the `other` radio button is updated only if the `other` radio button is {{domxref("HTMLInputElement.checked", "checked")}}, which in turn updates the value of `--theme`. ```js const radios = document.querySelectorAll('input[name="selection"]'); const body = document.querySelector("body"); const other = document.getElementById("other"); const color = document.getElementById("color"); for (let i = 0; i < radios.length; i++) { radios[i].addEventListener("change", (e) => { body.style.setProperty("--theme", e.target.value); }); } color.addEventListener("input", (e) => { other.style.setProperty("value", e.target.value); if (other.checked) { body.style.setProperty("--theme", e.target.value); } }); ``` We use the `@property` at-rule to define a CSS variable `--theme` to be a {{cssxref("color_value", "&lt;color&gt;")}} value and set the `initial-value` to `#00F`, ensuring equivalent colors are a match regardless of what syntax is used (for example, `#F00` is equal to `rgb(255 0 0)`, `#ff0000`, and `red`). ```css @property --theme { syntax: "<color>"; inherits: true; initial-value: #f00; } ``` ```css hidden output { padding: 3px 5px; margin-top: 5px; } ``` The first style feature query is a custom property with no value. This query type returns true when the computed value for the custom property value is different from the `initial-value` for that property. In this case, it will be true when the value of `--theme` is any value other than any syntax equivalent value of`#f00` ( such as `red`). When true, the {{htmlelement("output")}} will have a 5px dotted outline. The outline color is the current value of `--theme`. The default text {{cssxref("color")}} is grey. ```css @container style(--theme) { output { outline: 5px dotted var(--theme); color: #777; } } ``` The second and third style queries include values for the custom property. These will match if the container's `--theme` value is an equivalent color to the value listed, even if that value is the same as the `initial-value`. The first query matches elements whose `--theme` value is equivalent to `red`, `blue`, or `green`. When it is, the {{cssxref("color")}} will be the color current value of `--theme` (in the case of `blue` and `green`, overriding the grey set in the first style query). The second style query states that when `--theme` is equivalent to `red`, the `<output>`'s contents will also be bold. We did this to better demonstrate that the container query is a match. ```css @container style(--theme: green) or style(--theme: blue) or style(--theme: red) { output { color: var(--theme); } } @container style(--theme: red) { output { font-weight: bold; } } ``` {{EmbedLiveSample('example','100%','200')}} Try entering different color values into the text box. You may notice that values that are sRGB equivalents of `red` will make the `<output>` red — as it matches `style(--theme: red)` — while removing the outline, because `style(--theme)` returns false if the element's value for `--theme` is the same as the initial value for `--theme` defined by the `@property` at-rule. Any non-red sRGB valid color value, including `currentcolor` or `hsl(180 100% 50%)`, etc., makes the first style query return true; they are values that are different from the `initial-value`. Because we set `syntax: "<color>";`, the CSS variable can only be assigned valid `<color>` values. Valid values for the {{cssxref("color")}} property that aren't value `<color>` values, such as `unset` or `inherit`, are [invalid](/en-US/docs/Web/CSS/CSS_syntax/Error_handling) for this custom property, and will be ignored. If you enter `unset` or `gibberish`, the JavaScript updates the `style` on the {{HTMLElement("body")}} to `--theme: unset` or `--theme: gibberish`. Neither of these are colors. Both are invalid and ignored. This means the initial value is inherited and unchanged, with `style(--theme)` returning false and `style(--theme: red)` returning true. > **Note:** When declaring custom properties, consider using `@property` with the {{cssxref("@property/syntax","syntax")}} descriptor so the browser can properly compare computed values. ### Nested queries Container queries can be nested within other container queries. The styles defined inside multiple nested container queries are applied when all of the wrapping container queries are true. ```css @container style(--theme: red) { output { outline: 1px dotted; } @container style(--theme: purple) { output { outline: 5px dotted; } } } ``` In this case, the `<output>` will have a 5px dotted border if it's nested in a container where `--theme: purple` is set, and that container is nested within a container whose `--theme` value is `red`. ### Style query CSS declarations and properties Not yet supported in any browser, the `style()` functional notation can include regular CSS declarations including CSS properties and property value pairs. ```css @container style(font-weight: bold) { b, strong { background: yellow; } } ``` When supported, this basic example will make the background color of any {{htmlelement("b")}} and {{htmlelement("strong")}} elements yellow when the parent is already `bold`. The matching is done against the computed value of the parent container; if the parent's computed {{cssxref("font-weight")}} is `bold` (not `bolder` or `900`), there is a match. Just as with custom property container style queries, we did not have to define any elements as style containers as all elements are style containers by default. As long as an element doesn't have a `container-name` set, if it has `font-weight: bold` set or inherited, it will match. Style features that query a shorthand property will be true if the computed values match for each of its longhand properties, and false otherwise. For example, `@container style({{cssxref("border")}}: 2px solid red)` will resolve to true if all 12 longhand properties ({{cssxref("border-bottom-style")}}, etc.) that make up that shorthand are set to the same equivalent values. The global CSS values `revert` and `revert-layer` are invalid as values in a `<style-feature>` and cause the container style query to be false. Do not apply the styles you are querying in the style query to the element you are styling with that query as this may cause an infinite loop. It is expected that style queries will also accept properties in a boolean context. The style query will return false if the value of the property is the initial value for that property (if it has not been changed), and true otherwise. ```css @container style(font-weight) { } ``` The above example will return true for any element that has a value for `font-weight` that differs from its initial value. User-agent stylesheets set `font-weight: bold` for {{htmlelement("heading_elements", "heading")}} and {{htmlelement("th")}} elements, for example. Some browsers set {{htmlelement("strong")}} and {{htmlelement("b")}} to `bold`, others to `bolder`. {{htmlelement("optgroup")}} also sometimes has a `font-weight` other than `normal` set by the user agent. As long as the element's `font-weight` is not the default value for that user-agent, the style query will return true. These features are not yet supported in any browser. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Media queries](/en-US/docs/Web/CSS/CSS_media_queries) - CSS {{Cssxref("@container")}} at-rule - CSS {{Cssxref("contain")}} property - CSS {{Cssxref("container")}} shorthand property - CSS {{Cssxref("container-name")}} property - [Getting Started with Style Queries](https://developer.chrome.com/docs/css-ui/style-queries) (2022) - [Style queries](https://una.im/style-queries/) via una.im (2022)
0
data/mdn-content/files/en-us/web/css/css_containment
data/mdn-content/files/en-us/web/css/css_containment/container_queries/index.md
--- title: CSS container queries slug: Web/CSS/CSS_containment/Container_queries page-type: guide --- {{CSSRef}} Container queries enable you to apply styles to an element based on the size of the element's container. If, for example, a container has less space available in the surrounding context, you can hide certain elements or use smaller fonts. Container queries are an alternative to [media queries](/en-US/docs/Web/CSS/CSS_media_queries), which apply styles to elements based on viewport size or other device characteristics. ![Two different query types. First, a media query based on the viewport's width, which is the full width of the browser. Second, a container query based on the width of a container element.](container-query.svg) ## Using container queries To use container queries, you need to declare a **containment context** on an element so that the browser knows you might want to query the dimensions of this container later. To do this, use the {{cssxref("container-type")}} property with 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](/en-US/docs/Web/CSS/CSS_logical_properties_and_values/Basic_concepts_of_logical_properties_and_values#block_and_inline_dimensions) dimensions of the container. Applies layout, style, and size [containment](/en-US/docs/Web/CSS/CSS_containment/Using_CSS_containment) to the container. - `inline-size` - : The query will be based on the [inline](/en-US/docs/Web/CSS/CSS_logical_properties_and_values/Basic_concepts_of_logical_properties_and_values#block_and_inline_dimensions) dimensions of the container. Applies layout, style, and inline-size containment to the element. - `normal` - : The element is not a query container for any container size queries, but remains a query container for container style queries. Consider the following example of a card component for a blog post with a title and some text: ```html <div class="post"> <div class="card"> <h2>Card title</h2> <p>Card content</p> </div> </div> ``` You can create a containment context using the `container-type` property: ```css .post { container-type: inline-size; } ``` Next, use the {{cssxref("@container")}} at-rule to define a container query. The query in the following example will apply styles to elements based on the size of the nearest ancestor with a containment context. Specifically, this query will apply a larger font size for the card title if the container is wider than `700px`: ```css /* Default heading styles for the card title */ .card h2 { font-size: 1em; } /* If the container is larger than 700px */ @container (min-width: 700px) { .card h2 { font-size: 2em; } } ``` Using container queries, the card can be reused in multiple areas of a page without needing to know specifically where it will be placed each time. If the container with the card is narrower than `700px`, the font of the card title will be small, and if the card is in a container that's wider than `700px`, the font of the card title will be bigger. For more information on the syntax of container queries, see the {{cssxref("@container")}} page. ### Naming containment contexts In the previous section, a container query applied styles based on the nearest ancestor with a containment context. It's possible to give a containment context a name using the {{Cssxref("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`: ```css .post { container-type: inline-size; container-name: sidebar; } ``` You can then target this containment context using the `@container` at-rule: ```css @container sidebar (min-width: 700px) { .card { font-size: 2em; } } ``` More information on naming containment contexts is available on the {{cssxref("container-name")}} page. ### Shorthand container syntax The shorthand way of declaring a containment context is to use the `container` property: ```css .post { container: sidebar / inline-size; } ``` For more information on this property, see the {{Cssxref("container")}} reference. ### Container query length units When applying styles to a container using container queries, you can use container query length units. These units specify a length relative to the dimensions of a query container. Components that use units of length relative to their container are more flexible to use in different containers without having to recalculate concrete length values. The container query length units are: - `cqw`: 1% of a query container's width - `cqh`: 1% of a query container's height - `cqi`: 1% of a query container's inline size - `cqb`: 1% of a query container's block size - `cqmin`: The smaller value of either `cqi` or `cqb` - `cqmax`: The larger value of either `cqi` or `cqb` The following example uses the `cqi` unit to set the font size of a heading based on the inline size of the container: ```css @container (min-width: 700px) { .card h2 { font-size: max(1.5em, 1.23em + 2cqi); } } ``` For more information on these units, see the [Container query length units](/en-US/docs/Web/CSS/length#container_query_length_units) reference. ## Fallbacks for container queries For browsers that don't yet support container queries, {{cssxref("grid")}} and {{cssxref("flex")}} can be used to create a similar effect for the card component used on this page. The following example uses a {{cssxref("grid-template-columns")}} declaration to create a two-column layout for the card component. ```css .card { display: grid; grid-template-columns: 2fr 1fr; } ``` If you want to use a single-column layout for devices with a smaller viewport, you can use a media query to change the grid template: ```css @media (max-width: 700px) { .card { grid-template-columns: 1fr; } } ``` ## See also - [Media queries](/en-US/docs/Web/CSS/CSS_media_queries) - CSS {{Cssxref("@container")}} at-rule - CSS {{Cssxref("contain")}} property - CSS {{Cssxref("container")}} shorthand property - CSS {{Cssxref("container-name")}} property - CSS {{cssxref("content-visibility")}} property - [Using container size and style queries](/en-US/docs/Web/CSS/CSS_containment/Container_size_and_style_queries) - [Say Hello to CSS Container Queries](https://ishadeed.com/article/say-hello-to-css-container-queries/) by Ahmad Shadeed - [Container Queries: a Quick Start Guide](https://www.oddbird.net/2021/04/05/containerqueries/) - [Collection of Container Queries articles](https://github.com/sturobson/Awesome-Container-Queries)
0
data/mdn-content/files/en-us/web/css/css_containment
data/mdn-content/files/en-us/web/css/css_containment/container_queries/container-query.svg
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" clip-rule="evenodd" viewBox="0 0 1125.49 595.35"> <clipPath id="a"> <path d="M0 0h1125.49v595.35H0z"/> </clipPath> <g clip-path="url(#a)"> <path fill="#fff" d="M22.788 14.175H1097.04c12.58 0 22.79 7.1 22.79 15.857v492.761c0 8.758-10.21 15.857-22.79 15.857H22.789C10.203 538.65 0 531.551 0 522.793V30.033c0-8.759 10.203-15.858 22.788-15.858Z"/> </g> <path fill="#dbdbdb" d="M623.7 108.06v325.607c0 6.494-5.166 11.758-11.539 11.758H39.889c-6.373 0-11.539-5.264-11.539-11.758V108.06"/> <path fill="#ddd" d="M39.89 76.875h572.27c6.373 0 11.539 6.75 11.539 15.075V509.4c0 8.326-5.166 15.075-11.539 15.075H39.889c-6.373 0-11.539-6.749-11.539-15.075V91.95c0-8.326 5.166-15.075 11.54-15.075Z"/> <path fill="#f5f5f5" d="M609.525 510.3H42.518V105.225h567.007"/> <path fill="#fee1e1" d="M650.197 99.225h443.133c6.8 0 12.32 5.517 12.32 12.322v102.931c0 6.805-5.52 12.322-12.32 12.322H650.197c-6.805 0-12.322-5.517-12.322-12.322V111.547c0-6.805 5.517-12.322 12.322-12.322Z"/> <path fill="#e94420" d="M42.525 92.468a7.088 7.088 0 1 1 14.175 0 7.088 7.088 0 0 1-14.175 0Z"/> <path fill="#ef9f46" d="M68.04 92.468a7.088 7.088 0 1 1 14.175 0 7.088 7.088 0 0 1-14.175 0Z"/> <path fill="#68c40c" d="M93.555 92.468a7.087 7.087 0 1 1 14.175 0 7.087 7.087 0 0 1-14.175 0Z"/> <path fill="#dafcef" stroke="#000" stroke-linecap="butt" d="M170.1 269.325H567V390.9H170.1V269.325Z"/> <path fill="none" stroke="#000" stroke-dasharray="2" stroke-linecap="butt" stroke-width="2" d="M28.35 34.35V62.7"/> <path fill="none" stroke="#000" stroke-dasharray="4" stroke-linecap="butt" stroke-width="2" d="M28.35 48.525H623.7m0-14.175V62.7"/> <path d="M655.983 36.68h4.592l6.8 20.012 6.752-20.012h4.544v23.67h-3.046V46.38c0-.484.011-1.284.032-2.401.022-1.117.033-2.315.033-3.593l-6.752 19.964h-3.174l-6.8-19.964v.725c0 .58.013 1.463.04 2.65.027 1.187.041 2.06.041 2.619v13.97h-3.062V36.68Zm34.37 6.026c1.224 0 2.411.287 3.561.862 1.149.575 2.025 1.319 2.626 2.232.58.87.967 1.885 1.16 3.045.172.795.258 2.063.258 3.803h-12.649c.054 1.75.468 3.155 1.241 4.213.773 1.059 1.971 1.588 3.593 1.588 1.515 0 2.723-.5 3.626-1.499a5.048 5.048 0 0 0 1.095-2.014h2.853c-.076.634-.325 1.34-.75 2.119-.424.779-.899 1.415-1.426 1.91-.881.859-1.971 1.439-3.271 1.74-.698.171-1.488.257-2.368.257-2.149 0-3.97-.781-5.463-2.344-1.493-1.563-2.24-3.752-2.24-6.566 0-2.772.752-5.022 2.256-6.752 1.504-1.73 3.47-2.594 5.898-2.594Zm4.624 7.638c-.118-1.257-.392-2.262-.822-3.014-.794-1.396-2.121-2.094-3.98-2.094-1.332 0-2.449.48-3.351 1.442-.902.961-1.38 2.183-1.434 3.666h9.587Zm8.379 1.579c0 1.847.392 3.394 1.176 4.64.785 1.246 2.041 1.87 3.771 1.87 1.343 0 2.446-.578 3.311-1.733.865-1.154 1.297-2.811 1.297-4.97 0-2.181-.445-3.795-1.337-4.843-.892-1.047-1.993-1.57-3.303-1.57-1.461 0-2.646.558-3.553 1.675-.908 1.117-1.362 2.76-1.362 4.93Zm4.367-9.137c1.321 0 2.428.28 3.319.838.516.323 1.101.887 1.757 1.692V36.6h2.787v23.75h-2.61v-2.4c-.677 1.063-1.477 1.83-2.401 2.303-.924.473-1.982.71-3.174.71-1.923 0-3.588-.809-4.996-2.426-1.407-1.616-2.11-3.768-2.11-6.453 0-2.514.641-4.692 1.925-6.534 1.284-1.842 3.118-2.764 5.503-2.764Zm12.149.387h2.949V60.35h-2.949V43.173Zm0-6.493h2.949v3.287h-2.949V36.68Zm9.555 19.078c0 .838.307 1.498.919 1.982.612.483 1.337.725 2.175.725a6.613 6.613 0 0 0 2.965-.71c1.611-.783 2.417-2.067 2.417-3.85v-2.337c-.354.226-.811.414-1.37.564a11.79 11.79 0 0 1-1.643.323l-1.757.225c-1.052.14-1.842.36-2.368.66-.892.506-1.338 1.311-1.338 2.418Zm7.026-5.866c.666-.086 1.112-.365 1.337-.837.129-.258.194-.629.194-1.112 0-.988-.352-1.706-1.056-2.151-.703-.446-1.711-.67-3.021-.67-1.515 0-2.589.41-3.223 1.226-.354.45-.585 1.122-.693 2.014h-2.707c.054-2.127.744-3.607 2.071-4.44 1.327-.832 2.865-1.248 4.616-1.248 2.031 0 3.68.386 4.947 1.16 1.257.773 1.885 1.976 1.885 3.61v9.941c0 .3.062.543.186.725.123.183.384.274.781.274.129 0 .274-.008.435-.024a7.02 7.02 0 0 0 .516-.072v2.143a7.998 7.998 0 0 1-1.031.241 7.555 7.555 0 0 1-.967.049c-.999 0-1.724-.355-2.175-1.064-.237-.376-.403-.908-.5-1.595-.591.773-1.439 1.445-2.546 2.014-1.106.57-2.326.854-3.658.854-1.6 0-2.908-.486-3.923-1.458-1.015-.972-1.523-2.189-1.523-3.65 0-1.6.5-2.841 1.499-3.722.999-.88 2.309-1.423 3.931-1.627l4.625-.58Zm20.141 2.063c0 1.493.21 2.745.629 3.754.741 1.794 2.068 2.691 3.98 2.691 2.019 0 3.41-.945 4.173-2.836.419-1.042.629-2.374.629-3.996 0-1.493-.231-2.739-.693-3.738-.784-1.708-2.165-2.562-4.141-2.562-1.257 0-2.334.545-3.231 1.636-.897 1.09-1.346 2.774-1.346 5.051Zm4.287-9.249c1.428 0 2.631.36 3.609 1.08.537.386 1.053.956 1.547 1.708v-2.401h2.755V67.23h-2.916v-8.862c-.484.773-1.152 1.389-2.006 1.845-.854.457-1.921.685-3.199.685-1.837 0-3.48-.72-4.931-2.16-1.45-1.439-2.175-3.63-2.175-6.574 0-2.76.68-5.027 2.038-6.8 1.359-1.772 3.118-2.658 5.278-2.658Zm15.098.387v11.456c0 .881.139 1.6.419 2.16.515 1.03 1.477 1.546 2.884 1.546 2.019 0 3.394-.902 4.125-2.707.397-.967.596-2.293.596-3.98v-8.475h2.901V60.35h-2.74l.032-2.546a5.842 5.842 0 0 1-1.401 1.66c-1.107.902-2.45 1.353-4.029 1.353-2.46 0-4.135-.822-5.027-2.465-.483-.88-.725-2.057-.725-3.529v-11.73h2.965Zm22.639-.387c1.224 0 2.411.287 3.561.862 1.149.575 2.025 1.319 2.626 2.232.58.87.967 1.885 1.161 3.045.171.795.257 2.063.257 3.803h-12.649c.054 1.75.468 3.155 1.241 4.213.774 1.059 1.971 1.588 3.593 1.588 1.515 0 2.724-.5 3.626-1.499a5.05 5.05 0 0 0 1.096-2.014h2.852c-.076.634-.325 1.34-.75 2.119-.424.779-.899 1.415-1.426 1.91-.881.859-1.971 1.439-3.271 1.74-.698.171-1.487.257-2.368.257-2.149 0-3.97-.781-5.463-2.344-1.493-1.563-2.239-3.752-2.239-6.566 0-2.772.752-5.022 2.255-6.752 1.504-1.73 3.47-2.594 5.898-2.594Zm4.624 7.638c-.118-1.257-.392-2.262-.821-3.014-.795-1.396-2.122-2.094-3.98-2.094-1.332 0-2.45.48-3.352 1.442-.902.961-1.38 2.183-1.434 3.666h9.587Zm6.623-7.251h2.755v2.98c.226-.58.779-1.286 1.66-2.118.881-.833 1.896-1.249 3.045-1.249.054 0 .145.005.274.016.129.01.349.032.661.064v3.062a4.327 4.327 0 0 0-.475-.064 6.505 6.505 0 0 0-.476-.017c-1.461 0-2.583.47-3.367 1.41a4.918 4.918 0 0 0-1.177 3.247v9.926h-2.9V43.093Zm21.688 0h3.207a1002.27 1002.27 0 0 0-2.723 7.573 376.57 376.57 0 0 1-2.643 7.25c-1.676 4.405-2.857 7.09-3.545 8.058-.687.966-1.869 1.45-3.545 1.45a6.95 6.95 0 0 1-.942-.049 7.428 7.428 0 0 1-.814-.177v-2.642c.505.14.87.225 1.096.257.225.033.424.049.596.049.537 0 .932-.089 1.184-.266.253-.177.465-.395.637-.653.053-.086.247-.526.58-1.321.333-.795.575-1.386.725-1.773l-6.381-17.756h3.287l4.625 14.05 4.656-14.05Zm22.527-6.8c-1.687 3.276-2.782 5.688-3.287 7.235-.763 2.352-1.144 5.07-1.144 8.153 0 3.115.435 5.962 1.305 8.54.537 1.59 1.595 3.878 3.174 6.864h-1.95c-1.568-2.449-2.54-4.012-2.916-4.689-.376-.676-.784-1.595-1.225-2.755a22.714 22.714 0 0 1-1.257-5.092 21.38 21.38 0 0 1-.177-2.675c0-3.18.5-6.01 1.499-8.491.634-1.58 1.955-3.943 3.964-7.09h2.014Zm4.769 6.8 4.609 14.05 4.818-14.05h3.174l-6.51 17.257h-3.094l-6.364-17.257h3.367Zm15.082.08h2.949V60.35h-2.949V43.173Zm0-6.493h2.949v3.287h-2.949V36.68Zm14.518 6.026c1.225 0 2.412.287 3.561.862 1.15.575 2.025 1.319 2.627 2.232.58.87.967 1.885 1.16 3.045.172.795.258 2.063.258 3.803h-12.649c.054 1.75.467 3.155 1.241 4.213.773 1.059 1.971 1.588 3.593 1.588 1.515 0 2.723-.5 3.625-1.499a5.05 5.05 0 0 0 1.096-2.014h2.852c-.075.634-.325 1.34-.749 2.119-.424.779-.9 1.415-1.426 1.91-.881.859-1.971 1.439-3.271 1.74-.698.171-1.488.257-2.369.257-2.148 0-3.969-.781-5.462-2.344-1.493-1.563-2.24-3.752-2.24-6.566 0-2.772.752-5.022 2.256-6.752 1.504-1.73 3.47-2.594 5.897-2.594Zm4.625 7.638c-.118-1.257-.392-2.262-.822-3.014-.795-1.396-2.121-2.094-3.98-2.094-1.332 0-2.449.48-3.351 1.442-.903.961-1.381 2.183-1.434 3.666h9.587Zm7.879-7.251 3.32 13.6 3.367-13.6h3.255l3.384 13.519 3.529-13.52h2.9l-5.011 17.258h-3.013l-3.513-13.358-3.4 13.358h-3.013l-4.979-17.257h3.174ZM930.731 58.4c1.353 0 2.478-.566 3.375-1.7.897-1.133 1.346-2.828 1.346-5.083 0-1.375-.199-2.557-.596-3.545-.752-1.902-2.127-2.852-4.125-2.852-2.009 0-3.384 1.004-4.125 3.013-.398 1.074-.597 2.438-.597 4.093 0 1.332.199 2.465.597 3.4.752 1.783 2.127 2.674 4.125 2.674Zm-7.509-15.227h2.82v2.288c.58-.784 1.214-1.39 1.901-1.82.978-.645 2.127-.967 3.448-.967 1.955 0 3.615.749 4.979 2.248 1.365 1.498 2.047 3.638 2.047 6.42 0 3.76-.983 6.446-2.949 8.057-1.246 1.02-2.696 1.531-4.351 1.531-1.299 0-2.39-.285-3.271-.854-.515-.322-1.09-.875-1.724-1.66v8.814h-2.9V43.173Zm25.427 15.307c1.922 0 3.241-.727 3.955-2.182.715-1.456 1.072-3.075 1.072-4.859 0-1.611-.258-2.922-.773-3.931-.817-1.59-2.224-2.385-4.222-2.385-1.773 0-3.062.677-3.867 2.03-.806 1.354-1.209 2.987-1.209 4.899 0 1.837.403 3.367 1.209 4.592.805 1.225 2.084 1.837 3.835 1.837Zm.112-15.887c2.224 0 4.104.741 5.64 2.224 1.536 1.482 2.304 3.663 2.304 6.542 0 2.782-.676 5.08-2.03 6.896-1.354 1.816-3.454 2.723-6.3 2.723-2.374 0-4.26-.803-5.656-2.409-1.397-1.605-2.095-3.762-2.095-6.469 0-2.9.736-5.21 2.208-6.929 1.471-1.718 3.448-2.578 5.929-2.578Zm11.473.5h2.755v2.98c.226-.58.779-1.286 1.66-2.118.881-.833 1.896-1.249 3.046-1.249.053 0 .145.005.273.016.129.01.35.032.661.064v3.062a4.327 4.327 0 0 0-.475-.064 6.505 6.505 0 0 0-.476-.017c-1.461 0-2.583.47-3.367 1.41a4.923 4.923 0 0 0-1.177 3.247v9.926h-2.9V43.093Zm11.489-4.818h2.932v4.818h2.756v2.368h-2.756v11.264c0 .601.205 1.004.613 1.208.225.118.601.177 1.128.177a8.684 8.684 0 0 0 1.015-.048v2.288a6.57 6.57 0 0 1-1.039.21c-.36.042-.75.064-1.169.064-1.353 0-2.272-.346-2.755-1.04-.483-.692-.725-1.592-.725-2.698V45.46h-2.337v-2.368h2.337v-4.818Zm19.481 16.661c.086.967.327 1.708.725 2.224.73.934 1.998 1.401 3.803 1.401 1.074 0 2.019-.233 2.836-.7.816-.468 1.224-1.19 1.224-2.168 0-.74-.327-1.305-.983-1.692-.419-.236-1.246-.51-2.481-.821l-2.304-.58c-1.472-.366-2.557-.774-3.255-1.225-1.246-.784-1.869-1.87-1.869-3.255 0-1.633.588-2.954 1.764-3.964 1.176-1.01 2.758-1.514 4.745-1.514 2.6 0 4.474.762 5.621 2.288.72.966 1.07 2.008 1.05 3.126h-2.739a3.36 3.36 0 0 0-.693-1.789c-.666-.763-1.821-1.144-3.464-1.144-1.096 0-1.926.21-2.49.628-.564.42-.846.973-.846 1.66 0 .752.371 1.354 1.112 1.805.43.268 1.064.505 1.902.709l1.917.467c2.084.505 3.481.994 4.191 1.466 1.13.742 1.69 1.907 1.69 3.497 0 1.536-.58 2.863-1.75 3.98-1.163 1.117-2.939 1.676-5.323 1.676-2.568 0-4.386-.583-5.455-1.749-1.069-1.165-1.641-2.607-1.716-4.326h2.788Zm14.776-11.763h2.95V60.35h-2.95V43.173Zm0-6.493h2.95v3.287h-2.95V36.68Zm6.04 21.382 10.23-12.375h-9.47v-2.594h13.37v2.368l-10.16 12.295h10.47v2.594h-14.44v-2.288ZM1037 42.706c1.22 0 2.41.287 3.56.862s2.02 1.319 2.63 2.232c.58.87.96 1.885 1.16 3.045.17.795.25 2.063.25 3.803h-12.65c.06 1.75.47 3.155 1.25 4.213.77 1.059 1.97 1.588 3.59 1.588 1.51 0 2.72-.5 3.62-1.499a5.07 5.07 0 0 0 1.1-2.014h2.85c-.07.634-.32 1.34-.75 2.119-.42.779-.9 1.415-1.42 1.91-.88.859-1.97 1.439-3.27 1.74-.7.171-1.49.257-2.37.257-2.15 0-3.97-.781-5.47-2.344-1.49-1.563-2.24-3.752-2.24-6.566 0-2.772.76-5.022 2.26-6.752 1.5-1.73 3.47-2.594 5.9-2.594Zm4.62 7.638c-.12-1.257-.39-2.262-.82-3.014-.79-1.396-2.12-2.094-3.98-2.094-1.33 0-2.45.48-3.35 1.442-.9.961-1.38 2.183-1.44 3.666h9.59Zm5.56 16.741c1.71-3.33 2.81-5.757 3.3-7.283.76-2.31 1.13-5.017 1.13-8.12 0-3.105-.43-5.947-1.3-8.525-.54-1.59-1.6-3.878-3.18-6.864h1.95c1.66 2.642 2.66 4.273 3.01 4.89.35.618.73 1.47 1.13 2.554.52 1.343.89 2.67 1.11 3.98.22 1.31.33 2.573.33 3.787 0 3.18-.51 6.015-1.52 8.508-.63 1.6-1.95 3.958-3.94 7.073h-2.02Zm-395.317 252.45c2.997 0 5.323.79 6.977 2.369 1.654 1.579 2.573 3.373 2.755 5.382h-3.126c-.354-1.526-1.061-2.734-2.119-3.626-1.058-.892-2.543-1.337-4.455-1.337-2.331 0-4.214.819-5.648 2.457-1.434 1.638-2.151 4.149-2.151 7.533 0 2.771.647 5.019 1.942 6.743 1.294 1.724 3.225 2.586 5.793 2.586 2.363 0 4.162-.907 5.398-2.723.655-.956 1.144-2.213 1.466-3.77h3.126c-.279 2.492-1.203 4.581-2.772 6.268-1.88 2.03-4.415 3.045-7.605 3.045-2.75 0-5.06-.832-6.929-2.497-2.46-2.202-3.69-5.602-3.69-10.2 0-3.491.924-6.354 2.772-8.588 1.998-2.428 4.753-3.642 8.266-3.642Zm20.319 22.446c1.922 0 3.241-.728 3.955-2.183.715-1.456 1.072-3.075 1.072-4.859 0-1.611-.258-2.922-.773-3.931-.817-1.59-2.224-2.385-4.222-2.385-1.773 0-3.062.677-3.867 2.03-.806 1.354-1.209 2.987-1.209 4.899 0 1.837.403 3.367 1.209 4.592.805 1.225 2.084 1.837 3.835 1.837Zm.112-15.888c2.224 0 4.104.741 5.64 2.224 1.536 1.482 2.304 3.663 2.304 6.542 0 2.782-.676 5.081-2.03 6.896-1.354 1.816-3.454 2.723-6.3 2.723-2.374 0-4.26-.803-5.656-2.409-1.397-1.605-2.095-3.762-2.095-6.469 0-2.9.736-5.21 2.208-6.929 1.471-1.718 3.448-2.578 5.929-2.578Zm11.393.5h2.755v2.449c.816-1.01 1.681-1.735 2.594-2.175.913-.441 1.928-.661 3.046-.661 2.449 0 4.103.854 4.962 2.562.473.935.709 2.272.709 4.012v11.07h-2.948v-10.876c0-1.053-.156-1.902-.468-2.546-.515-1.075-1.45-1.612-2.803-1.612-.688 0-1.252.07-1.692.21-.795.236-1.493.709-2.095 1.418-.483.569-.798 1.157-.943 1.764-.145.607-.217 1.475-.217 2.602v9.04h-2.9v-17.257Zm18.933-4.818h2.932v4.818h2.756v2.368h-2.756v11.264c0 .601.204 1.004.613 1.208.225.118.601.177 1.127.177a8.734 8.734 0 0 0 1.016-.048v2.288c-.333.097-.68.167-1.04.209-.36.043-.749.065-1.168.065-1.353 0-2.272-.347-2.755-1.039-.484-.693-.725-1.593-.725-2.699v-11.425h-2.337v-2.368h2.337v-4.818Zm10.812 17.483c0 .838.306 1.498.918 1.982.612.483 1.337.725 2.175.725a6.621 6.621 0 0 0 2.965-.709c1.612-.784 2.417-2.068 2.417-3.851v-2.337c-.354.226-.811.414-1.369.564a11.54 11.54 0 0 1-1.644.322l-1.756.226c-1.053.14-1.843.36-2.369.661-.892.505-1.337 1.31-1.337 2.417Zm7.025-5.866c.666-.085 1.112-.365 1.337-.837.129-.258.194-.629.194-1.112 0-.989-.352-1.706-1.056-2.151-.703-.446-1.71-.669-3.021-.669-1.515 0-2.589.408-3.223 1.225-.354.451-.585 1.122-.692 2.014h-2.707c.053-2.127.743-3.607 2.07-4.439 1.327-.833 2.866-1.249 4.617-1.249 2.03 0 3.679.386 4.946 1.16 1.257.773 1.886 1.976 1.886 3.609v9.942c0 .301.061.543.185.725.123.183.384.274.781.274.129 0 .274-.008.435-.024.162-.016.333-.04.516-.072v2.143a8.173 8.173 0 0 1-1.031.241 7.56 7.56 0 0 1-.967.049c-.999 0-1.724-.355-2.175-1.064-.237-.376-.403-.908-.5-1.595-.591.773-1.439 1.445-2.546 2.014-1.106.569-2.325.854-3.657.854-1.601 0-2.909-.486-3.924-1.458-1.015-.972-1.523-2.189-1.523-3.65 0-1.6.5-2.841 1.499-3.722.999-.881 2.309-1.423 3.932-1.627l4.624-.581Zm9.104-6.719h2.949v17.177h-2.949v-17.177Zm0-6.493h2.949v3.287h-2.949v-3.287Zm7.332 6.413h2.755v2.449c.816-1.01 1.681-1.735 2.594-2.175.913-.441 1.928-.661 3.046-.661 2.449 0 4.103.854 4.962 2.562.473.935.709 2.272.709 4.012v11.07h-2.948v-10.876c0-1.053-.156-1.902-.468-2.546-.515-1.075-1.45-1.612-2.803-1.612-.688 0-1.252.07-1.692.21-.795.236-1.493.709-2.095 1.418-.483.569-.797 1.157-.943 1.764-.145.607-.217 1.475-.217 2.602v9.04h-2.9v-17.257Zm25.539-.387c1.225 0 2.412.287 3.561.862 1.15.575 2.025 1.319 2.627 2.232.58.87.966 1.885 1.16 3.045.172.795.258 2.063.258 3.803h-12.649c.053 1.751.467 3.155 1.24 4.213.774 1.059 1.972 1.588 3.594 1.588 1.514 0 2.723-.5 3.625-1.499a5.046 5.046 0 0 0 1.096-2.014h2.852c-.075.634-.325 1.34-.749 2.119-.425.779-.9 1.415-1.426 1.909-.881.86-1.972 1.44-3.271 1.74-.699.172-1.488.258-2.369.258-2.148 0-3.969-.781-5.462-2.344-1.494-1.563-2.24-3.752-2.24-6.566 0-2.772.752-5.022 2.256-6.752 1.504-1.729 3.469-2.594 5.897-2.594Zm4.625 7.638c-.119-1.257-.392-2.262-.822-3.014-.795-1.396-2.122-2.094-3.98-2.094-1.332 0-2.449.48-3.352 1.442-.902.961-1.38 2.183-1.434 3.666h9.588Zm6.622-7.251h2.756v2.981c.225-.58.778-1.287 1.659-2.119.881-.833 1.896-1.249 3.046-1.249.053 0 .145.005.274.016.128.011.349.032.66.065v3.061a4.202 4.202 0 0 0-.475-.064 6.308 6.308 0 0 0-.475-.017c-1.461 0-2.584.47-3.368 1.41a4.924 4.924 0 0 0-1.176 3.247v9.926h-2.901v-17.257Zm21.946 8.862c0 1.493.21 2.745.629 3.754.741 1.794 2.068 2.691 3.98 2.691 2.019 0 3.411-.945 4.173-2.836.419-1.042.629-2.374.629-3.996 0-1.493-.231-2.739-.693-3.738-.784-1.708-2.165-2.562-4.141-2.562-1.257 0-2.334.545-3.231 1.635-.897 1.091-1.346 2.775-1.346 5.052Zm4.287-9.249c1.428 0 2.631.36 3.609 1.08.537.386 1.053.956 1.547 1.708v-2.401h2.755v24.137h-2.916v-8.862c-.484.774-1.152 1.388-2.006 1.845-.854.457-1.921.685-3.199.685-1.837 0-3.48-.72-4.931-2.159-1.45-1.44-2.175-3.631-2.175-6.575 0-2.76.68-5.027 2.039-6.799 1.358-1.773 3.117-2.659 5.277-2.659Zm15.098.387v11.456c0 .881.139 1.601.419 2.159.515 1.032 1.477 1.547 2.884 1.547 2.02 0 3.395-.902 4.125-2.707.397-.967.596-2.293.596-3.98v-8.475h2.901v17.257h-2.74l.033-2.546a5.864 5.864 0 0 1-1.402 1.66c-1.107.902-2.45 1.353-4.029 1.353-2.46 0-4.135-.821-5.027-2.465-.483-.881-.725-2.057-.725-3.529v-11.73h2.965Zm22.639-.387c1.225 0 2.412.287 3.561.862s2.025 1.319 2.626 2.232c.581.87.967 1.885 1.161 3.045.171.795.257 2.063.257 3.803h-12.649c.054 1.751.468 3.155 1.241 4.213.774 1.059 1.971 1.588 3.593 1.588 1.515 0 2.724-.5 3.626-1.499a5.046 5.046 0 0 0 1.096-2.014h2.852c-.076.634-.325 1.34-.75 2.119-.424.779-.899 1.415-1.426 1.909-.88.86-1.971 1.44-3.271 1.74-.698.172-1.487.258-2.368.258-2.149 0-3.969-.781-5.463-2.344-1.493-1.563-2.239-3.752-2.239-6.566 0-2.772.752-5.022 2.255-6.752 1.504-1.729 3.47-2.594 5.898-2.594Zm4.624 7.638c-.118-1.257-.392-2.262-.821-3.014-.795-1.396-2.122-2.094-3.98-2.094-1.332 0-2.45.48-3.352 1.442-.902.961-1.38 2.183-1.434 3.666h9.587Zm6.623-7.251h2.755v2.981c.226-.58.779-1.287 1.66-2.119.881-.833 1.896-1.249 3.045-1.249.054 0 .145.005.274.016.129.011.349.032.661.065v3.061a4.202 4.202 0 0 0-.475-.064 6.316 6.316 0 0 0-.476-.017c-1.461 0-2.583.47-3.367 1.41a4.92 4.92 0 0 0-1.177 3.247v9.926h-2.9v-17.257Zm21.688 0h3.207c-.408 1.106-1.316 3.631-2.723 7.573a376.074 376.074 0 0 1-2.643 7.251c-1.675 4.404-2.857 7.09-3.545 8.057-.687.966-1.869 1.45-3.545 1.45-.408 0-.722-.016-.942-.049a7.37 7.37 0 0 1-.814-.177v-2.642c.505.139.87.225 1.096.257.225.033.424.049.596.049.537 0 .932-.089 1.184-.266.253-.177.465-.395.637-.653.054-.086.247-.526.58-1.321.333-.795.575-1.386.725-1.772l-6.381-17.757h3.287l4.625 14.05 4.656-14.05Zm22.527-6.8c-1.687 3.276-2.782 5.688-3.287 7.235-.763 2.352-1.144 5.07-1.144 8.153 0 3.115.435 5.962 1.305 8.54.537 1.59 1.595 3.878 3.174 6.864h-1.95c-1.568-2.449-2.54-4.012-2.916-4.689-.376-.676-.784-1.595-1.225-2.755a22.66 22.66 0 0 1-1.434-7.767c0-3.179.5-6.01 1.499-8.491.634-1.579 1.955-3.943 3.964-7.09h2.014Zm10.006 6.3c1.945 0 3.526.473 4.746 1.418 1.219.945 1.952 2.573 2.199 4.882h-2.82c-.172-1.063-.564-1.947-1.176-2.65-.612-.704-1.595-1.056-2.949-1.056-1.847 0-3.169.903-3.964 2.707-.515 1.171-.773 2.616-.773 4.335 0 1.729.365 3.185 1.096 4.367.73 1.181 1.879 1.772 3.448 1.772 1.203 0 2.156-.368 2.86-1.104.704-.736 1.19-1.743 1.458-3.021h2.82c-.322 2.288-1.128 3.961-2.417 5.019-1.289 1.058-2.938 1.588-4.947 1.588-2.256 0-4.055-.825-5.398-2.474-1.342-1.649-2.014-3.709-2.014-6.179 0-3.03.736-5.388 2.208-7.074 1.471-1.687 3.346-2.53 5.623-2.53Zm16.694 15.888c1.922 0 3.241-.728 3.955-2.183.715-1.456 1.072-3.075 1.072-4.859 0-1.611-.258-2.922-.774-3.931-.816-1.59-2.223-2.385-4.221-2.385-1.773 0-3.062.677-3.867 2.03-.806 1.354-1.209 2.987-1.209 4.899 0 1.837.403 3.367 1.209 4.592.805 1.225 2.084 1.837 3.835 1.837Zm.112-15.888c2.224 0 4.104.741 5.64 2.224 1.536 1.482 2.304 3.663 2.304 6.542 0 2.782-.677 5.081-2.03 6.896-1.354 1.816-3.454 2.723-6.3 2.723-2.374 0-4.26-.803-5.656-2.409-1.397-1.605-2.095-3.762-2.095-6.469 0-2.9.736-5.21 2.208-6.929 1.471-1.718 3.448-2.578 5.929-2.578Zm11.392.5h2.756v2.449c.816-1.01 1.681-1.735 2.594-2.175.913-.441 1.928-.661 3.045-.661 2.45 0 4.104.854 4.963 2.562.473.935.709 2.272.709 4.012v11.07h-2.948v-10.876c0-1.053-.156-1.902-.468-2.546-.515-1.075-1.45-1.612-2.803-1.612-.688 0-1.252.07-1.692.21-.795.236-1.493.709-2.095 1.418-.483.569-.798 1.157-.943 1.764-.145.607-.217 1.475-.217 2.602v9.04h-2.901v-17.257Zm18.934-4.818h2.932v4.818h2.756v2.368h-2.756v11.264c0 .601.204 1.004.612 1.208.226.118.602.177 1.128.177a8.734 8.734 0 0 0 1.016-.048v2.288c-.333.097-.68.167-1.04.209-.36.043-.749.065-1.168.065-1.354 0-2.272-.347-2.755-1.039-.484-.693-.725-1.593-.725-2.699v-11.425h-2.337v-2.368h2.337v-4.818Zm10.812 17.483c0 .838.306 1.498.918 1.982.612.483 1.337.725 2.175.725a6.621 6.621 0 0 0 2.965-.709c1.611-.784 2.417-2.068 2.417-3.851v-2.337c-.354.226-.811.414-1.369.564a11.54 11.54 0 0 1-1.644.322l-1.756.226c-1.053.14-1.843.36-2.369.661-.892.505-1.337 1.31-1.337 2.417Zm7.025-5.866c.666-.085 1.112-.365 1.337-.837.129-.258.194-.629.194-1.112 0-.989-.352-1.706-1.056-2.151-.703-.446-1.71-.669-3.021-.669-1.515 0-2.589.408-3.223 1.225-.354.451-.585 1.122-.692 2.014h-2.708c.054-2.127.744-3.607 2.071-4.439 1.327-.833 2.866-1.249 4.616-1.249 2.031 0 3.68.386 4.947 1.16 1.257.773 1.886 1.976 1.886 3.609v9.942c0 .301.061.543.185.725.123.183.384.274.781.274.129 0 .274-.008.435-.024.162-.016.333-.04.516-.072v2.143a8.173 8.173 0 0 1-1.031.241 7.57 7.57 0 0 1-.967.049c-.999 0-1.724-.355-2.175-1.064-.237-.376-.403-.908-.5-1.595-.591.773-1.439 1.445-2.546 2.014-1.106.569-2.325.854-3.657.854-1.601 0-2.909-.486-3.924-1.458-1.015-.972-1.523-2.189-1.523-3.65 0-1.6.5-2.841 1.499-3.722.999-.881 2.309-1.423 3.931-1.627l4.625-.581Zm9.104-6.719h2.949v17.177h-2.949v-17.177Zm0-6.493h2.949v3.287h-2.949v-3.287Zm7.331 6.413h2.756v2.449c.816-1.01 1.681-1.735 2.594-2.175.913-.441 1.928-.661 3.046-.661 2.449 0 4.103.854 4.962 2.562.473.935.709 2.272.709 4.012v11.07h-2.948v-10.876c0-1.053-.156-1.902-.468-2.546-.515-1.075-1.45-1.612-2.803-1.612-.688 0-1.252.07-1.692.21-.795.236-1.493.709-2.095 1.418-.483.569-.798 1.157-.943 1.764-.145.607-.217 1.475-.217 2.602v9.04h-2.901v-17.257Zm25.539-.387c1.23 0 2.41.287 3.56.862s2.03 1.319 2.63 2.232c.58.87.97 1.885 1.16 3.045.17.795.26 2.063.26 3.803h-12.65c.05 1.751.46 3.155 1.24 4.213.77 1.059 1.97 1.588 3.59 1.588 1.52 0 2.72-.5 3.63-1.499.51-.58.88-1.251 1.09-2.014h2.85c-.07.634-.32 1.34-.74 2.119-.43.779-.9 1.415-1.43 1.909-.88.86-1.97 1.44-3.27 1.74-.7.172-1.49.258-2.37.258-2.15 0-3.97-.781-5.46-2.344-1.5-1.563-2.24-3.752-2.24-6.566 0-2.772.75-5.022 2.25-6.752 1.51-1.729 3.47-2.594 5.9-2.594Zm4.63 7.638c-.12-1.257-.4-2.262-.83-3.014-.79-1.396-2.12-2.094-3.98-2.094-1.33 0-2.45.48-3.35 1.442-.9.961-1.38 2.183-1.43 3.666h9.59Zm6.62-7.251h2.75v2.981c.23-.58.78-1.287 1.66-2.119.88-.833 1.9-1.249 3.05-1.249.05 0 .14.005.27.016s.35.032.66.065v3.061a4.217 4.217 0 0 0-.47-.064 6.568 6.568 0 0 0-.48-.017c-1.46 0-2.58.47-3.37 1.41a4.939 4.939 0 0 0-1.17 3.247v9.926h-2.9v-17.257Zm21.8 11.843c.09.967.33 1.708.72 2.224.73.934 2 1.401 3.81 1.401 1.07 0 2.02-.233 2.83-.701.82-.467 1.23-1.189 1.23-2.167 0-.741-.33-1.305-.99-1.692-.41-.236-1.24-.51-2.48-.821l-2.3-.58c-1.47-.366-2.56-.774-3.26-1.225-1.24-.784-1.86-1.869-1.86-3.255 0-1.633.58-2.954 1.76-3.964 1.18-1.01 2.76-1.514 4.74-1.514 2.6 0 4.48.762 5.63 2.288.72.966 1.07 2.008 1.05 3.126h-2.74a3.4 3.4 0 0 0-.7-1.789c-.66-.763-1.82-1.144-3.46-1.144-1.1 0-1.93.209-2.49.628-.56.419-.85.973-.85 1.66 0 .752.37 1.354 1.12 1.805.43.268 1.06.505 1.9.709l1.91.467c2.09.505 3.49.994 4.19 1.466 1.13.742 1.7 1.907 1.7 3.497 0 1.536-.59 2.863-1.75 3.98-1.17 1.117-2.94 1.676-5.33 1.676-2.57 0-4.38-.583-5.45-1.749-1.07-1.165-1.64-2.607-1.72-4.326h2.79Zm14.78-11.763h2.94v17.177h-2.94v-17.177Zm0-6.493h2.94v3.287h-2.94v-3.287Zm6.04 21.382 10.23-12.375h-9.47v-2.594h13.37v2.368l-10.17 12.295h10.48v2.594h-14.44v-2.288Zm24.97-15.356c1.23 0 2.41.287 3.56.862s2.03 1.319 2.63 2.232c.58.87.97 1.885 1.16 3.045.17.795.26 2.063.26 3.803h-12.65c.05 1.751.47 3.155 1.24 4.213.77 1.059 1.97 1.588 3.59 1.588 1.52 0 2.73-.5 3.63-1.499.51-.58.88-1.251 1.1-2.014h2.85c-.08.634-.33 1.34-.75 2.119-.43.779-.9 1.415-1.43 1.909-.88.86-1.97 1.44-3.27 1.74-.7.172-1.49.258-2.37.258-2.15 0-3.97-.781-5.46-2.344-1.49-1.563-2.24-3.752-2.24-6.566 0-2.772.75-5.022 2.26-6.752 1.5-1.729 3.47-2.594 5.89-2.594Zm4.63 7.638c-.12-1.257-.39-2.262-.82-3.014-.8-1.396-2.13-2.094-3.98-2.094-1.34 0-2.45.48-3.36 1.442-.9.961-1.38 2.183-1.43 3.666h9.59Zm5.56 16.741c1.7-3.33 2.81-5.757 3.3-7.283.75-2.309 1.13-5.016 1.13-8.121 0-3.104-.44-5.946-1.31-8.524-.53-1.59-1.59-3.878-3.17-6.864h1.95c1.65 2.642 2.65 4.273 3 4.89.35.618.73 1.469 1.14 2.554.52 1.343.88 2.67 1.1 3.98.22 1.311.33 2.573.33 3.787 0 3.179-.5 6.015-1.51 8.508-.63 1.6-1.95 3.958-3.95 7.073h-2.01Z"/> <path fill="none" stroke="#000" stroke-dasharray="4" stroke-linecap="butt" stroke-width="2" d="M170.1 396.9v28.35m0-14.175H567m0-14.175v28.35"/> <path d="M665.903 133.481c0-.956-.216-1.705-.648-2.247-.432-.542-1.024-.813-1.777-.813-.754 0-1.344.271-1.771.813-.428.542-.641 1.291-.641 2.247 0 .965.213 1.718.641 2.26.427.541 1.017.812 1.771.812.753 0 1.345-.271 1.777-.812.432-.542.648-1.295.648-2.26Zm2.361 5.205h-2.488v-1.041c-.322.449-.701.777-1.137.984-.436.207-.958.311-1.568.311-1.388 0-2.507-.499-3.357-1.498-.851-.999-1.276-2.319-1.276-3.961 0-1.633.423-2.947 1.269-3.942.847-.994 1.968-1.492 3.364-1.492.601 0 1.13.104 1.587.311.457.208.83.51 1.118.908v-.52c0-1.151-.316-2.04-.946-2.666-.631-.627-1.53-.94-2.698-.94-1.76 0-3.197.775-4.31 2.323-1.113 1.549-1.669 3.564-1.669 6.043 0 2.599.643 4.666 1.929 6.202 1.287 1.536 3.001 2.304 5.142 2.304.677 0 1.333-.099 1.968-.298a6.916 6.916 0 0 0 1.828-.895l1.168 2.234a7.814 7.814 0 0 1-2.241 1.048 9.371 9.371 0 0 1-2.596.349c-2.962 0-5.324-.986-7.084-2.958-1.761-1.972-2.641-4.634-2.641-7.986 0-3.275.792-5.897 2.374-7.864 1.583-1.968 3.678-2.952 6.284-2.952 1.845 0 3.303.557 4.374 1.669 1.071 1.113 1.606 2.631 1.606 4.552v9.775Zm9.699-11.083c.279-.601.635-1.045 1.066-1.333.432-.288.961-.432 1.587-.432 1.228 0 2.076.424 2.546 1.27.47.846.704 2.607.704 5.281v8.011h-3.046v-9.128c0-1.058-.081-1.765-.242-2.12-.16-.356-.452-.533-.876-.533-.423 0-.719.182-.888.546-.17.364-.254 1.066-.254 2.107v9.128h-3.009v-9.128c0-1.041-.085-1.743-.254-2.107-.169-.364-.465-.546-.889-.546-.423 0-.715.177-.876.533-.16.355-.241 1.062-.241 2.12v9.128h-3.047v-14.219h2.704v1.473a2.782 2.782 0 0 1 1.016-1.327 2.66 2.66 0 0 1 1.562-.489c.575 0 1.1.172 1.574.515.474.342.762.759.863 1.25Zm20.884 12.099c-.863.355-1.744.622-2.641.8-.897.177-1.845.266-2.843.266-2.379 0-4.196-.637-5.453-1.91-1.257-1.274-1.885-3.109-1.885-5.504 0-2.319.605-4.151 1.815-5.497s2.861-2.019 4.951-2.019c2.108 0 3.743.625 4.907 1.873 1.164 1.248 1.746 3.007 1.746 5.275v1.511h-9.636c.008 1.117.338 1.951.99 2.501.652.55 1.625.825 2.92.825.855 0 1.697-.123 2.526-.368.83-.246 1.697-.635 2.603-1.168v3.415Zm-3.149-7.998c-.016-.982-.268-1.725-.755-2.228-.487-.504-1.204-.756-2.152-.756-.855 0-1.536.261-2.044.781-.508.521-.808 1.259-.901 2.215l5.852-.012Zm14.854-3.504v-7.554h3.707V140.4h-3.707v-2.107c-.398.821-.912 1.438-1.543 1.853-.63.415-1.373.622-2.228.622-1.625 0-2.886-.652-3.783-1.955-.897-1.303-1.345-3.14-1.345-5.51 0-2.403.454-4.248 1.364-5.535.91-1.286 2.207-1.93 3.891-1.93.762 0 1.446.197 2.051.591.605.393 1.136.984 1.593 1.771Zm-5.18 5.129c0 1.371.229 2.446.686 3.224.457.779 1.087 1.168 1.891 1.168s1.439-.389 1.905-1.168c.465-.778.698-1.853.698-3.224 0-1.371-.233-2.446-.698-3.225-.466-.779-1.101-1.168-1.905-1.168s-1.434.389-1.891 1.168c-.457.779-.686 1.854-.686 3.225Zm13.597-7.148h7.363v11.363h4.621v2.856h-12.974v-2.856h4.634v-8.506h-3.644v-2.857Zm3.644-6.919h3.719v4.342h-3.719v-4.342Zm18.09 14.46c-1.388 0-2.357.178-2.907.533-.55.356-.825.965-.825 1.829 0 .643.19 1.155.571 1.536.381.381.897.571 1.549.571.982 0 1.744-.37 2.285-1.111.542-.74.813-1.775.813-3.104v-.254h-1.486Zm5.18-1.434v8.112h-3.694v-1.587a4.48 4.48 0 0 1-1.727 1.447c-.702.339-1.477.508-2.323.508-1.617 0-2.876-.427-3.777-1.282-.901-.855-1.352-2.048-1.352-3.58 0-1.659.537-2.884 1.612-3.675 1.075-.792 2.738-1.187 4.99-1.187h2.577v-.623c0-.6-.218-1.055-.654-1.364-.436-.309-1.077-.464-1.923-.464-.889 0-1.75.113-2.584.337-.834.224-1.703.582-2.609 1.073v-3.174a12.75 12.75 0 0 1 2.501-.749 14.391 14.391 0 0 1 2.692-.242c2.31 0 3.933.47 4.868 1.41.935.939 1.403 2.619 1.403 5.04Zm28.971-11.642c-1.117 2.023-1.947 3.984-2.489 5.884a20.709 20.709 0 0 0-.812 5.694c0 1.888.271 3.788.812 5.7.542 1.913 1.372 3.885 2.489 5.916h-2.895c-1.346-1.955-2.344-3.884-2.996-5.789a17.867 17.867 0 0 1-.977-5.827c0-1.963.328-3.901.983-5.814.656-1.913 1.653-3.834 2.99-5.764h2.895Zm12.682 6.957c.28-.601.635-1.045 1.067-1.333.431-.288.96-.432 1.587-.432 1.227 0 2.075.424 2.545 1.27.47.846.705 2.607.705 5.281v8.011h-3.047v-9.128c0-1.058-.081-1.765-.241-2.12-.161-.356-.453-.533-.876-.533-.424 0-.72.182-.889.546-.169.364-.254 1.066-.254 2.107v9.128h-3.009v-9.128c0-1.041-.084-1.743-.254-2.107-.169-.364-.465-.546-.888-.546-.424 0-.716.177-.876.533-.161.355-.242 1.062-.242 2.12v9.128h-3.046v-14.219h2.704v1.473a2.774 2.774 0 0 1 1.015-1.327 2.66 2.66 0 0 1 1.562-.489c.575 0 1.1.172 1.574.515.474.342.762.759.863 1.25Zm15.781 6.119c-1.388 0-2.357.178-2.908.533-.55.356-.825.965-.825 1.829 0 .643.191 1.155.571 1.536.381.381.898.571 1.549.571.982 0 1.744-.37 2.285-1.111.542-.74.813-1.775.813-3.104v-.254h-1.485Zm5.179-1.434v8.112h-3.694v-1.587a4.48 4.48 0 0 1-1.727 1.447c-.702.339-1.476.508-2.323.508-1.616 0-2.875-.427-3.777-1.282-.901-.855-1.352-2.048-1.352-3.58 0-1.659.538-2.884 1.613-3.675 1.074-.792 2.738-1.187 4.989-1.187h2.577v-.623c0-.6-.218-1.055-.654-1.364-.436-.309-1.077-.464-1.923-.464-.889 0-1.75.113-2.584.337-.833.224-1.703.582-2.609 1.073v-3.174a12.75 12.75 0 0 1 2.501-.749 14.399 14.399 0 0 1 2.692-.242c2.31 0 3.933.47 4.868 1.41.936.939 1.403 2.619 1.403 5.04Zm16.123-6.107-4.786 6.805 5.205 7.414h-4.342l-2.792-4.786-2.781 4.786h-4.342l5.256-7.414-4.837-6.805h4.342l2.362 4.24 2.374-4.24h4.341Zm2.412 5.028h13.457v3.415h-13.457v-3.415Zm14.359-5.028h3.098l1.688 10.893 1.536-7.173h3.009l1.511 7.173 1.714-10.893h3.097l-2.577 14.219h-3.529l-1.727-7.389-1.714 7.389h-3.529l-2.577-14.219Zm18.459 0h7.363v11.363h4.621v2.856h-12.974v-2.856h4.633v-8.506h-3.643v-2.857Zm3.643-6.919h3.72v4.342h-3.72v-4.342Zm19.246 8.938v-7.554h3.708V140.4h-3.708v-2.107c-.397.821-.911 1.438-1.542 1.853-.631.415-1.373.622-2.228.622-1.625 0-2.886-.652-3.783-1.955-.897-1.303-1.346-3.14-1.346-5.51 0-2.403.455-4.248 1.365-5.535.91-1.286 2.207-1.93 3.891-1.93.762 0 1.445.197 2.05.591.605.393 1.136.984 1.593 1.771Zm-5.179 5.129c0 1.371.228 2.446.685 3.224.457.779 1.088 1.168 1.892 1.168.804 0 1.439-.389 1.904-1.168.466-.778.698-1.853.698-3.224 0-1.371-.232-2.446-.698-3.225-.465-.779-1.1-1.168-1.904-1.168-.804 0-1.435.389-1.892 1.168-.457.779-.685 1.854-.685 3.225Zm19.551-11.185v4.037h4.862v2.857h-4.862v6.728c0 .635.154 1.09.463 1.365.309.275.823.413 1.542.413h2.857v2.856h-3.123c-2.133 0-3.576-.341-4.329-1.022-.754-.681-1.13-1.944-1.13-3.79v-6.55h-3.631v-2.857h3.631v-4.037h3.72Zm20.49 9.027v9.229h-3.695v-8.658c0-1.024-.146-1.756-.438-2.196-.292-.441-.768-.661-1.428-.661-.677 0-1.206.299-1.587.895-.381.597-.571 1.424-.571 2.482v8.138h-3.694v-19.754h3.694v7.668c.262-.787.711-1.396 1.346-1.828.635-.432 1.4-.648 2.298-.648 1.345 0 2.361.447 3.046 1.34.686.893 1.029 2.224 1.029 3.993Zm7.757-4.266h4.24v4.634h-4.24v-4.634Zm0 8.836h4.24v4.659h-4.24v-4.659Zm32.576-3.821h-2.006v-3.301h2.006c.931 0 1.652-.184 2.164-.552.512-.369.768-.887.768-1.556 0-.702-.256-1.25-.768-1.644-.512-.393-1.233-.59-2.164-.59-.711 0-1.473.089-2.285.267a16.93 16.93 0 0 0-2.527.774v-3.402a20.353 20.353 0 0 1 2.577-.603 14.95 14.95 0 0 1 2.438-.21c1.997 0 3.555.436 4.672 1.308 1.117.871 1.676 2.073 1.676 3.605 0 1.126-.322 2.046-.965 2.761-.643.716-1.562 1.183-2.755 1.403 1.354.237 2.387.777 3.098 1.619.71.842 1.066 1.949 1.066 3.32 0 1.836-.588 3.237-1.765 4.202-1.176.965-2.886 1.447-5.129 1.447-.956 0-1.898-.08-2.824-.241a15.895 15.895 0 0 1-2.66-.698v-3.479c.796.398 1.655.701 2.577.908a13.25 13.25 0 0 0 2.907.311c1.016 0 1.82-.235 2.412-.705.593-.469.889-1.102.889-1.898 0-.973-.296-1.724-.889-2.253-.592-.529-1.43-.793-2.513-.793Zm10.245-.978c0-3.284.52-5.745 1.561-7.382 1.041-1.638 2.607-2.457 4.698-2.457 2.099 0 3.669.817 4.71 2.45 1.041 1.634 1.561 4.097 1.561 7.389 0 3.284-.52 5.743-1.561 7.376s-2.611 2.45-4.71 2.45c-2.091 0-3.657-.819-4.698-2.456-1.041-1.638-1.561-4.095-1.561-7.37Zm4.367 5.065c.398 1.058 1.028 1.587 1.892 1.587.871 0 1.506-.529 1.904-1.587.398-1.058.597-2.746.597-5.065 0-.88-.03-1.667-.089-2.361l-4.431 7.058c.042.136.085.258.127.368Zm1.892-11.73c-.864 0-1.494.529-1.892 1.587-.398 1.058-.597 2.751-.597 5.078 0 .719.017 1.375.051 1.968l4.367-6.97-.025-.076c-.398-1.058-1.033-1.587-1.904-1.587Zm21.823 15.425c-.863.355-1.744.622-2.641.8-.897.177-1.845.266-2.843.266-2.379 0-4.196-.637-5.453-1.91-1.257-1.274-1.885-3.109-1.885-5.504 0-2.319.605-4.151 1.815-5.497s2.861-2.019 4.951-2.019c2.108 0 3.743.625 4.907 1.873 1.164 1.248 1.746 3.007 1.746 5.275v1.511h-9.636c.008 1.117.338 1.951.99 2.501.652.55 1.625.825 2.92.825.855 0 1.697-.123 2.526-.368.83-.246 1.697-.635 2.603-1.168v3.415Zm-3.148-7.998c-.017-.982-.269-1.725-.756-2.228-.487-.504-1.204-.756-2.152-.756-.855 0-1.536.261-2.044.781-.507.521-.808 1.259-.901 2.215l5.853-.012Zm13.568-4.101c.28-.601.64-1.045 1.07-1.333.43-.288.96-.432 1.59-.432 1.22 0 2.07.424 2.54 1.27.47.846.71 2.607.71 5.281v8.011h-3.05v-9.128c0-1.058-.08-1.765-.24-2.12-.16-.356-.45-.533-.88-.533-.42 0-.72.182-.89.546-.17.364-.25 1.066-.25 2.107v9.128h-3.01v-9.128c0-1.041-.08-1.743-.25-2.107-.17-.364-.47-.546-.89-.546-.43 0-.72.177-.88.533-.16.355-.24 1.062-.24 2.12v9.128h-3.046v-14.219h2.706v1.473a2.79 2.79 0 0 1 1.01-1.327c.47-.326.99-.489 1.57-.489.57 0 1.1.172 1.57.515.47.342.76.759.86 1.25Zm10.82-6.957h2.89c1.34 1.93 2.34 3.851 2.99 5.764.66 1.913.99 3.851.99 5.814 0 1.981-.33 3.923-.98 5.827-.65 1.905-1.65 3.834-3 5.789h-2.89c1.12-2.031 1.95-4.003 2.49-5.916.54-1.912.81-3.812.81-5.7 0-1.896-.27-3.794-.81-5.694s-1.37-3.861-2.49-5.884Zm25.1 21.569v2.425h-1.91c-2.1 0-3.52-.313-4.23-.939-.72-.627-1.07-1.875-1.07-3.745v-2.73c0-1.278-.23-2.16-.68-2.647-.46-.486-1.27-.73-2.46-.73h-.79v-2.425h.79c1.19 0 2-.241 2.46-.723.45-.483.68-1.367.68-2.654v-2.729c0-1.87.35-3.117 1.07-3.739.71-.622 2.13-.933 4.23-.933h1.91v2.412h-1.56c-.88 0-1.47.178-1.77.533-.29.356-.44 1.139-.44 2.349v2.653c0 1.397-.21 2.379-.62 2.946-.42.567-1.16.931-2.23 1.091 1.07.178 1.81.555 2.23 1.13.41.576.62 1.558.62 2.946v2.602c0 1.219.15 2.008.44 2.368.3.36.89.539 1.77.539h1.56Zm-346.33 15.231h2.805l-9.978 21.366h-2.806l9.979-21.366Zm19.157 4.519-5.243 2.831 5.256 2.857-1.118 1.93-4.963-2.996v5.547h-2.247v-5.535c-.805.483-1.634.982-2.489 1.498-.855.517-1.68 1.012-2.475 1.486l-1.105-1.93 5.269-2.857-5.256-2.831 1.092-1.942 4.964 2.983v-5.535h2.247v5.535c.812-.482 1.644-.981 2.494-1.498.851-.516 1.674-1.011 2.469-1.485l1.105 1.942Zm28.971.661v3.25a8.487 8.487 0 0 0-2.14-.997 7.656 7.656 0 0 0-2.228-.336c-.829 0-1.455.12-1.878.361-.424.242-.635.599-.635 1.073 0 .677.905 1.227 2.717 1.651l.139.038 1.067.254c1.354.321 2.346.84 2.977 1.555.63.715.946 1.682.946 2.901 0 1.464-.481 2.562-1.441 3.294-.961.732-2.406 1.098-4.336 1.098-.855 0-1.731-.074-2.628-.222a18.974 18.974 0 0 1-2.729-.667v-3.25a11.59 11.59 0 0 0 2.507 1.067 8.911 8.911 0 0 0 2.469.368c.872 0 1.532-.127 1.981-.381.448-.254.673-.622.673-1.104 0-.474-.159-.838-.476-1.092-.318-.254-1.073-.525-2.267-.813l-1.028-.228c-1.422-.322-2.459-.825-3.11-1.511-.652-.685-.978-1.604-.978-2.755 0-1.371.495-2.437 1.486-3.199.99-.762 2.382-1.143 4.176-1.143.804 0 1.606.066 2.406.197s1.576.328 2.33.591Zm11.832-4.482v4.037h4.862v2.857h-4.862v6.728c0 .635.154 1.09.463 1.365.309.275.823.413 1.543.413h2.856v2.856h-3.123c-2.133 0-3.576-.341-4.329-1.022-.753-.681-1.13-1.944-1.13-3.79v-6.55h-3.631v-2.857h3.631v-4.037h3.72Zm15.92 19.792c-.5 1.337-1.099 2.313-1.797 2.926-.698.614-1.546.921-2.545.921h-3.072v-2.831h1.51c.762 0 1.321-.129 1.676-.387.356-.259.715-.857 1.079-1.797l.279-.762-5.408-13.825h3.91l3.25 9.23 3.111-9.23h3.91l-5.903 15.755Zm11.54-7.465v-10.968h-3.771v-2.857h7.49v13.825c0 1.117.174 1.909.521 2.374.347.466.935.699 1.765.699h2.97v2.856h-4.011c-1.769 0-3.039-.457-3.809-1.371-.77-.914-1.155-2.433-1.155-4.558Zm24.73 5.231a14.17 14.17 0 0 1-2.641.8c-.897.177-1.845.266-2.843.266-2.379 0-4.196-.637-5.453-1.91-1.257-1.274-1.885-3.109-1.885-5.504 0-2.319.605-4.151 1.815-5.497 1.211-1.346 2.861-2.019 4.951-2.019 2.108 0 3.743.625 4.907 1.873 1.164 1.248 1.746 3.007 1.746 5.275v1.511h-9.636c.009 1.117.339 1.951.99 2.501.652.55 1.625.825 2.92.825a8.88 8.88 0 0 0 2.527-.368c.829-.246 1.697-.635 2.602-1.168v3.415Zm-3.148-7.998c-.017-.982-.269-1.725-.756-2.228-.486-.504-1.204-.756-2.152-.756-.854 0-1.536.261-2.043.781-.508.521-.809 1.259-.902 2.215l5.853-.012Zm17.392-5.078v3.25a8.465 8.465 0 0 0-2.139-.997 7.656 7.656 0 0 0-2.228-.336c-.829 0-1.456.12-1.879.361-.423.242-.635.599-.635 1.073 0 .677.906 1.227 2.717 1.651l.14.038 1.066.254c1.354.321 2.347.84 2.977 1.555.631.715.946 1.682.946 2.901 0 1.464-.48 2.562-1.441 3.294-.96.732-2.406 1.098-4.335 1.098-.855 0-1.731-.074-2.628-.222a19.02 19.02 0 0 1-2.73-.667v-3.25c.821.466 1.657.821 2.508 1.067a8.9 8.9 0 0 0 2.469.368c.872 0 1.532-.127 1.98-.381.449-.254.673-.622.673-1.104 0-.474-.158-.838-.476-1.092-.317-.254-1.073-.525-2.266-.813l-1.028-.228c-1.422-.322-2.459-.825-3.111-1.511-.651-.685-.977-1.604-.977-2.755 0-1.371.495-2.437 1.485-3.199.99-.762 2.383-1.143 4.177-1.143.804 0 1.606.066 2.406.197.799.131 1.576.328 2.329.591Zm16.669 4.545v9.229h-3.694v-8.658c0-1.024-.146-1.756-.438-2.196-.292-.441-.768-.661-1.428-.661-.677 0-1.206.299-1.587.895-.381.597-.572 1.424-.572 2.482v8.138h-3.694v-19.754h3.694v7.668c.263-.787.711-1.396 1.346-1.828.635-.432 1.401-.648 2.298-.648 1.346 0 2.361.447 3.047 1.34.685.893 1.028 2.224 1.028 3.993Zm16.047 8.531a14.17 14.17 0 0 1-2.641.8c-.897.177-1.845.266-2.843.266-2.379 0-4.196-.637-5.453-1.91-1.257-1.274-1.885-3.109-1.885-5.504 0-2.319.605-4.151 1.815-5.497 1.211-1.346 2.861-2.019 4.951-2.019 2.108 0 3.743.625 4.907 1.873 1.164 1.248 1.746 3.007 1.746 5.275v1.511h-9.636c.008 1.117.339 1.951.99 2.501.652.55 1.625.825 2.92.825a8.88 8.88 0 0 0 2.527-.368c.829-.246 1.696-.635 2.602-1.168v3.415Zm-3.148-7.998c-.017-.982-.269-1.725-.756-2.228-.486-.504-1.204-.756-2.152-.756-.854 0-1.536.261-2.044.781-.507.521-.808 1.259-.901 2.215l5.853-.012Zm18.801 7.998c-.863.355-1.743.622-2.64.8a14.67 14.67 0 0 1-2.844.266c-2.378 0-4.196-.637-5.453-1.91-1.256-1.274-1.885-3.109-1.885-5.504 0-2.319.605-4.151 1.816-5.497 1.21-1.346 2.86-2.019 4.951-2.019 2.107 0 3.743.625 4.906 1.873 1.164 1.248 1.746 3.007 1.746 5.275v1.511h-9.636c.009 1.117.339 1.951.991 2.501.651.55 1.625.825 2.919.825a8.88 8.88 0 0 0 2.527-.368c.829-.246 1.697-.635 2.602-1.168v3.415Zm-3.148-7.998c-.017-.982-.269-1.725-.755-2.228-.487-.504-1.204-.756-2.152-.756-.855 0-1.536.261-2.044.781-.508.521-.808 1.259-.902 2.215l5.853-.012Zm13.571-9.56v4.037h4.863v2.857h-4.863v6.728c0 .635.155 1.09.464 1.365.309.275.823.413 1.542.413h2.857v2.856h-3.123c-2.133 0-3.576-.341-4.33-1.022-.753-.681-1.129-1.944-1.129-3.79v-6.55h-3.631v-2.857h3.631v-4.037h3.719Zm37.464 3.821-5.243 2.831 5.256 2.857-1.117 1.93-4.964-2.996v5.547h-2.247v-5.535c-.804.483-1.634.982-2.489 1.498a427.53 427.53 0 0 1-2.475 1.486l-1.105-1.93 5.269-2.857-5.256-2.831 1.092-1.942 4.964 2.983v-5.535h2.247v5.535c.812-.482 1.644-.981 2.494-1.498.851-.516 1.674-1.011 2.47-1.485l1.104 1.942Zm12.15-4.519h2.805l-9.978 21.366h-2.806l9.979-21.366Zm-275.235 56.769h1.536c.88 0 1.473-.182 1.778-.545.304-.364.457-1.152.457-2.362v-2.602c0-1.388.207-2.37.622-2.946.414-.575 1.151-.952 2.209-1.13-1.067-.16-1.805-.524-2.216-1.091-.41-.567-.615-1.549-.615-2.946v-2.653c0-1.193-.151-1.972-.451-2.336-.3-.364-.895-.546-1.784-.546h-1.536v-2.412h1.904c2.099 0 3.502.311 4.209.933.707.622 1.06 1.869 1.06 3.739v2.729c0 1.278.231 2.161.692 2.647.461.487 1.288.73 2.482.73h.787v2.425h-.787c-1.194 0-2.021.246-2.482.737-.461.49-.692 1.371-.692 2.64v2.73c0 1.87-.353 3.118-1.06 3.745-.707.626-2.11.939-4.209.939h-1.904v-2.425Z"/> <path fill="#dafcef" d="M649.694 354.375h444.136c6.53 0 11.82 5.292 11.82 11.819v103.937c0 6.527-5.29 11.819-11.82 11.819H649.694c-6.527 0-11.819-5.292-11.819-11.819V366.194c0-6.527 5.292-11.819 11.819-11.819Z"/> <path d="M670.833 392.989c0-.956-.216-1.705-.647-2.247-.432-.541-1.025-.812-1.778-.812-.753 0-1.343.271-1.771.812-.427.542-.641 1.291-.641 2.247 0 .965.214 1.718.641 2.26.428.542 1.018.813 1.771.813.753 0 1.346-.271 1.778-.813.431-.542.647-1.295.647-2.26Zm2.361 5.205h-2.488v-1.041c-.322.449-.7.777-1.136.984-.436.208-.959.311-1.568.311-1.388 0-2.507-.499-3.358-1.498s-1.276-2.319-1.276-3.961c0-1.633.423-2.947 1.27-3.942.846-.994 1.967-1.491 3.364-1.491.601 0 1.13.103 1.587.311.457.207.829.51 1.117.907v-.52c0-1.151-.315-2.04-.946-2.666-.63-.626-1.53-.94-2.698-.94-1.76 0-3.197.775-4.31 2.324-1.113 1.548-1.669 3.563-1.669 6.043 0 2.598.643 4.665 1.93 6.201 1.286 1.536 3 2.304 5.141 2.304.677 0 1.333-.099 1.968-.298a6.9 6.9 0 0 0 1.828-.895l1.168 2.235a7.813 7.813 0 0 1-2.241 1.047 9.329 9.329 0 0 1-2.596.349c-2.962 0-5.323-.986-7.084-2.958-1.76-1.972-2.64-4.634-2.64-7.985 0-3.276.791-5.897 2.374-7.865 1.582-1.968 3.677-2.952 6.284-2.952 1.845 0 3.303.557 4.373 1.67 1.071 1.113 1.606 2.63 1.606 4.551v9.775Zm14.409.991a7.836 7.836 0 0 1-2.018.818 9.449 9.449 0 0 1-2.323.273c-2.192 0-3.906-.66-5.142-1.98-1.236-1.32-1.853-3.149-1.853-5.484 0-2.345.622-4.179 1.866-5.504 1.244-1.324 2.962-1.987 5.154-1.987.762 0 1.496.089 2.203.267a9.445 9.445 0 0 1 2.113.825v3.402a6.063 6.063 0 0 0-1.783-1.072 5.54 5.54 0 0 0-2.012-.375c-1.211 0-2.142.385-2.793 1.155-.652.771-.978 1.867-.978 3.289 0 1.421.326 2.513.978 3.275.651.762 1.582 1.142 2.793 1.142a5.57 5.57 0 0 0 2.018-.361 5.937 5.937 0 0 0 1.777-1.098v3.415Zm10.004-10.817c-.888 0-1.587.392-2.094 1.174-.508.783-.762 1.873-.762 3.27 0 1.396.254 2.486.762 3.269.507.782 1.206 1.174 2.094 1.174.898 0 1.6-.392 2.108-1.174.508-.783.762-1.873.762-3.269 0-1.397-.254-2.487-.762-3.27-.508-.782-1.21-1.174-2.108-1.174Zm-6.576 4.444c0-2.294.595-4.112 1.784-5.453 1.189-1.342 2.787-2.012 4.792-2.012 2.015 0 3.616.67 4.806 2.012 1.189 1.341 1.783 3.159 1.783 5.453 0 2.293-.594 4.111-1.783 5.452-1.19 1.342-2.791 2.012-4.806 2.012-2.005 0-3.603-.67-4.792-2.012-1.189-1.341-1.784-3.159-1.784-5.452Zm28.006-2.133v9.229h-3.694v-8.658c0-1.033-.146-1.771-.438-2.215-.292-.445-.768-.667-1.428-.667-.669 0-1.196.301-1.581.902-.385.6-.578 1.434-.578 2.5v8.138h-3.694v-14.219h3.694v2.133c.263-.787.711-1.396 1.346-1.828.635-.432 1.401-.647 2.298-.647 1.346 0 2.361.446 3.047 1.339.685.893 1.028 2.224 1.028 3.993Zm10.816-9.027v4.037h4.863v2.857h-4.863v6.728c0 .635.155 1.09.464 1.365.309.275.823.413 1.542.413h2.857v2.856h-3.123c-2.133 0-3.576-.34-4.329-1.022-.754-.681-1.13-1.944-1.13-3.789v-6.551h-3.631v-2.857h3.631v-4.037h3.719Zm15.781 11.578c-1.388 0-2.357.178-2.907.534-.551.355-.826.964-.826 1.828 0 .643.191 1.155.572 1.536.38.381.897.571 1.548.571.982 0 1.744-.37 2.286-1.111.541-.74.812-1.775.812-3.104v-.254h-1.485Zm5.179-1.434v8.112h-3.694v-1.587a4.48 4.48 0 0 1-1.726 1.448 5.29 5.29 0 0 1-2.324.507c-1.616 0-2.875-.427-3.777-1.282-.901-.855-1.352-2.048-1.352-3.58 0-1.659.538-2.884 1.613-3.675 1.075-.792 2.738-1.187 4.989-1.187h2.577v-.622c0-.601-.218-1.056-.654-1.365-.436-.309-1.077-.463-1.923-.463-.889 0-1.75.112-2.584.336-.833.224-1.703.582-2.608 1.073v-3.174a12.798 12.798 0 0 1 2.501-.749 14.382 14.382 0 0 1 2.691-.241c2.311 0 3.933.469 4.869 1.409.935.939 1.402 2.619 1.402 5.04Zm4.393-6.107h7.363v11.363h4.621v2.856h-12.974v-2.856h4.634v-8.506h-3.644v-2.857Zm3.644-6.919h3.719v4.342h-3.719v-4.342Zm22.8 11.909v9.229h-3.694v-8.658c0-1.033-.146-1.771-.438-2.215-.292-.445-.768-.667-1.428-.667-.669 0-1.196.301-1.581.902-.385.6-.577 1.434-.577 2.5v8.138h-3.695v-14.219h3.695v2.133c.262-.787.711-1.396 1.345-1.828.635-.432 1.401-.647 2.298-.647 1.346 0 2.361.446 3.047 1.339.686.893 1.028 2.224 1.028 3.993Zm16.047 8.531c-.863.355-1.743.622-2.64.8-.898.177-1.845.266-2.844.266-2.378 0-4.196-.637-5.453-1.91-1.257-1.274-1.885-3.109-1.885-5.504 0-2.319.605-4.151 1.815-5.497 1.211-1.345 2.861-2.018 4.952-2.018 2.107 0 3.743.624 4.906 1.872 1.164 1.249 1.746 3.007 1.746 5.275v1.511h-9.636c.009 1.117.339 1.951.99 2.501.652.55 1.625.825 2.92.825a8.88 8.88 0 0 0 2.527-.368c.829-.246 1.697-.635 2.602-1.168v3.415Zm-3.148-7.998c-.017-.982-.269-1.725-.756-2.228-.486-.504-1.204-.756-2.151-.756-.855 0-1.537.261-2.044.781-.508.521-.809 1.259-.902 2.216l5.853-.013Zm19.424-1.6a4.022 4.022 0 0 0-1.429-.838 5.532 5.532 0 0 0-1.796-.279c-.787 0-1.475.138-2.063.413a3.102 3.102 0 0 0-1.365 1.199 3.206 3.206 0 0 0-.425 1.168c-.08.457-.121 1.151-.121 2.082v6.551h-3.719v-14.219h3.719v2.209a4.067 4.067 0 0 1 1.676-1.885c.753-.444 1.634-.666 2.641-.666a6.053 6.053 0 0 1 2.882.723v3.542Zm28.424-9.458c-1.117 2.023-1.946 3.984-2.488 5.885a20.67 20.67 0 0 0-.812 5.693c0 1.888.27 3.788.812 5.701.542 1.912 1.371 3.884 2.488 5.916h-2.894c-1.346-1.955-2.345-3.885-2.996-5.789a17.879 17.879 0 0 1-.978-5.828c0-1.963.328-3.901.984-5.814.656-1.913 1.653-3.834 2.99-5.764h2.894Zm3.923 5.535h3.098l1.688 10.893 1.537-7.173h3.008l1.511 7.173 1.714-10.893h3.098l-2.578 14.219h-3.529l-1.726-7.388-1.714 7.388h-3.53l-2.577-14.219Zm18.459 0h7.364v11.363h4.621v2.856h-12.975v-2.856h4.634v-8.506h-3.644v-2.857Zm3.644-6.919h3.72v4.342h-3.72v-4.342Zm19.246 8.938v-7.554h3.707v19.754h-3.707v-2.107c-.398.821-.912 1.439-1.543 1.853-.63.415-1.373.622-2.228.622-1.625 0-2.886-.651-3.783-1.955-.897-1.303-1.346-3.14-1.346-5.509 0-2.404.455-4.249 1.365-5.536.91-1.286 2.207-1.929 3.891-1.929.762 0 1.446.196 2.051.59.605.394 1.136.984 1.593 1.771Zm-5.18 5.129c0 1.371.229 2.446.686 3.225.457.778 1.087 1.167 1.891 1.167s1.439-.389 1.905-1.167c.465-.779.698-1.854.698-3.225s-.233-2.446-.698-3.225c-.466-.778-1.101-1.168-1.905-1.168s-1.434.39-1.891 1.168c-.457.779-.686 1.854-.686 3.225Zm19.551-11.185v4.037h4.862v2.857h-4.862v6.728c0 .635.154 1.09.463 1.365.309.275.823.413 1.543.413h2.856v2.856h-3.123c-2.133 0-3.576-.34-4.329-1.022-.753-.681-1.13-1.944-1.13-3.789v-6.551h-3.631v-2.857h3.631v-4.037h3.72Zm20.49 9.027v9.229h-3.694v-8.658c0-1.024-.146-1.756-.438-2.196-.292-.44-.768-.66-1.428-.66-.677 0-1.206.298-1.587.895-.381.596-.572 1.424-.572 2.481v8.138h-3.694v-19.754h3.694v7.668c.263-.787.711-1.396 1.346-1.828.635-.432 1.401-.647 2.298-.647 1.346 0 2.361.446 3.047 1.339.685.893 1.028 2.224 1.028 3.993Zm18.827-2.527v-3.174l13.419 5.282v2.996l-13.419 5.268v-3.161l10.093-3.593-10.093-3.618Zm32.577 8.455h4.189v-12.301l-3.897.964v-3.376l3.922-.94h3.619v15.653h4.189v3.301h-12.022v-3.301Zm15.716-15.653h10.296v3.301h-7.261v3.516c.304-.118.63-.205.977-.26a7.058 7.058 0 0 1 1.105-.082c1.878 0 3.402.592 4.57 1.777 1.163 1.185 1.753 2.725 1.753 4.621 0 1.989-.63 3.561-1.899 4.716-1.265 1.156-2.99 1.733-5.173 1.733-.813 0-1.634-.067-2.463-.203a18.262 18.262 0 0 1-2.539-.609v-3.377c.702.347 1.424.607 2.164.781.741.173 1.483.26 2.228.26 1.32 0 2.323-.282 3.009-.844.686-.563 1.028-1.382 1.028-2.457 0-.99-.34-1.784-1.022-2.38-.681-.597-1.589-.895-2.723-.895-.668 0-1.339.082-2.012.247a10.44 10.44 0 0 0-2.038.743v-10.588Zm27.19 18.256c-.86.355-1.74.622-2.64.8-.89.177-1.84.266-2.84.266-2.38 0-4.2-.637-5.45-1.91-1.26-1.274-1.89-3.109-1.89-5.504 0-2.319.61-4.151 1.82-5.497 1.21-1.345 2.86-2.018 4.95-2.018 2.11 0 3.74.624 4.9 1.872 1.17 1.249 1.75 3.007 1.75 5.275v1.511h-9.64c.01 1.117.34 1.951.99 2.501.66.55 1.63.825 2.92.825.86 0 1.7-.123 2.53-.368.83-.246 1.7-.635 2.6-1.168v3.415Zm-3.14-7.998c-.02-.982-.27-1.725-.76-2.228-.49-.504-1.2-.756-2.15-.756-.86 0-1.54.261-2.05.781-.5.521-.8 1.259-.9 2.216l5.86-.013Zm13.57-4.101c.28-.601.63-1.045 1.06-1.333.43-.287.96-.431 1.59-.431 1.23 0 2.08.423 2.55 1.269.47.847.7 2.607.7 5.281v8.011h-3.05v-9.128c0-1.058-.08-1.764-.24-2.12-.16-.355-.45-.533-.87-.533-.43 0-.72.182-.89.546-.17.364-.26 1.066-.26 2.107v9.128h-3.01v-9.128c0-1.041-.08-1.743-.25-2.107-.17-.364-.46-.546-.89-.546-.42 0-.71.178-.87.533-.16.356-.25 1.062-.25 2.12v9.128h-3.04v-14.219h2.7v1.473c.21-.559.55-1.001 1.02-1.327.46-.325.98-.488 1.56-.488.57 0 1.1.171 1.57.514.48.343.77.759.87 1.25Zm10.81-6.957h2.9c1.33 1.93 2.33 3.851 2.99 5.764.65 1.913.98 3.851.98 5.814 0 1.981-.32 3.923-.98 5.828-.65 1.904-1.65 3.834-2.99 5.789h-2.9c1.12-2.032 1.95-4.004 2.49-5.916.54-1.913.81-3.813.81-5.701 0-1.895-.27-3.793-.81-5.693-.54-1.901-1.37-3.862-2.49-5.885Zm25.1 21.57v2.424h-1.9c-2.11 0-3.52-.313-4.24-.939-.71-.626-1.07-1.875-1.07-3.745v-2.73c0-1.278-.23-2.16-.68-2.647-.45-.486-1.27-.73-2.46-.73h-.78v-2.424h.78c1.19 0 2.01-.242 2.46-.724.45-.482.68-1.367.68-2.653v-2.73c0-1.87.36-3.117 1.07-3.739.72-.622 2.13-.933 4.24-.933h1.9v2.412h-1.56c-.88 0-1.47.178-1.76.534-.3.355-.45 1.138-.45 2.348v2.654c0 1.396-.21 2.378-.62 2.945-.42.567-1.16.931-2.22 1.092 1.06.177 1.8.554 2.22 1.13.41.575.62 1.557.62 2.945v2.602c0 1.219.15 2.008.45 2.368.29.36.88.54 1.76.54h1.56Zm-361.98 15.23h2.806l-9.979 21.366h-2.805l9.978-21.366Zm19.157 4.52-5.243 2.831 5.256 2.856-1.117 1.93-4.964-2.996v5.548h-2.247v-5.536c-.804.483-1.634.982-2.488 1.498-.855.517-1.68 1.012-2.476 1.486l-1.104-1.93 5.268-2.856-5.256-2.831 1.092-1.943 4.964 2.984v-5.536h2.247v5.536a323.25 323.25 0 0 0 2.495-1.498 307.61 307.61 0 0 1 2.469-1.486l1.104 1.943Zm28.971.66v3.25a8.516 8.516 0 0 0-2.139-.997 7.66 7.66 0 0 0-2.228-.336c-.83 0-1.456.12-1.879.362-.423.241-.635.598-.635 1.072 0 .677.906 1.228 2.717 1.651l.14.038 1.066.254c1.354.321 2.347.84 2.977 1.555.631.715.946 1.682.946 2.901 0 1.464-.48 2.562-1.441 3.294-.961.732-2.406 1.098-4.335 1.098-.855 0-1.731-.074-2.628-.222a19.147 19.147 0 0 1-2.73-.666v-3.25c.821.465 1.657.821 2.507 1.066a8.876 8.876 0 0 0 2.47.368c.871 0 1.532-.127 1.98-.381.449-.254.673-.622.673-1.104 0-.474-.159-.838-.476-1.092-.317-.254-1.073-.525-2.266-.812l-1.028-.229c-1.422-.322-2.459-.825-3.111-1.511-.652-.685-.977-1.603-.977-2.755 0-1.371.495-2.437 1.485-3.199.99-.761 2.382-1.142 4.177-1.142.804 0 1.606.065 2.406.196.799.132 1.576.328 2.329.591Zm11.832-4.482v4.037h4.862v2.857h-4.862v6.728c0 .635.155 1.09.464 1.365.308.275.823.413 1.542.413h2.856v2.856h-3.123c-2.132 0-3.575-.34-4.329-1.022-.753-.681-1.13-1.944-1.13-3.789v-6.551h-3.63v-2.857h3.63v-4.037h3.72Zm15.92 19.792c-.499 1.338-1.098 2.313-1.796 2.927-.699.613-1.547.92-2.546.92h-3.072v-2.831h1.511c.761 0 1.32-.129 1.676-.387.355-.258.715-.857 1.079-1.797l.279-.761-5.408-13.826h3.91l3.25 9.23 3.11-9.23h3.91l-5.903 15.755Zm11.54-7.465v-10.968h-3.77v-2.857h7.49v13.825c0 1.118.173 1.909.52 2.374.347.466.936.699 1.765.699h2.971v2.856h-4.012c-1.769 0-3.038-.457-3.809-1.371-.77-.914-1.155-2.433-1.155-4.558Zm24.731 5.231c-.864.355-1.744.622-2.641.8a14.67 14.67 0 0 1-2.844.266c-2.378 0-4.196-.637-5.452-1.91-1.257-1.274-1.886-3.109-1.886-5.504 0-2.319.605-4.151 1.816-5.497 1.21-1.345 2.86-2.018 4.951-2.018 2.107 0 3.743.624 4.907 1.872 1.163 1.249 1.745 3.007 1.745 5.275v1.511h-9.636c.009 1.117.339 1.951.991 2.501.651.55 1.625.825 2.92.825.854 0 1.697-.123 2.526-.368.829-.246 1.697-.635 2.603-1.168v3.415Zm-3.149-7.998c-.017-.982-.269-1.725-.755-2.228-.487-.504-1.204-.756-2.152-.756-.855 0-1.536.261-2.044.781-.508.521-.808 1.259-.901 2.216l5.852-.013Zm17.393-5.078v3.25a8.532 8.532 0 0 0-2.139-.997 7.666 7.666 0 0 0-2.228-.336c-.83 0-1.456.12-1.879.362-.424.241-.635.598-.635 1.072 0 .677.905 1.228 2.717 1.651l.139.038 1.067.254c1.354.321 2.346.84 2.977 1.555.63.715.946 1.682.946 2.901 0 1.464-.481 2.562-1.441 3.294-.961.732-2.406 1.098-4.336 1.098-.855 0-1.731-.074-2.628-.222a19.102 19.102 0 0 1-2.729-.666v-3.25c.821.465 1.656.821 2.507 1.066a8.874 8.874 0 0 0 2.469.368c.872 0 1.532-.127 1.981-.381.448-.254.673-.622.673-1.104 0-.474-.159-.838-.476-1.092-.318-.254-1.073-.525-2.267-.812l-1.028-.229c-1.422-.322-2.459-.825-3.11-1.511-.652-.685-.978-1.603-.978-2.755 0-1.371.495-2.437 1.486-3.199.99-.761 2.382-1.142 4.176-1.142.804 0 1.606.065 2.406.196.8.132 1.576.328 2.33.591Zm16.669 4.545v9.229h-3.695v-8.658c0-1.024-.146-1.756-.438-2.196-.292-.44-.768-.66-1.428-.66-.677 0-1.206.298-1.587.895-.381.596-.571 1.424-.571 2.481v8.138h-3.694v-19.754h3.694v7.668c.262-.787.711-1.396 1.346-1.828.634-.432 1.4-.647 2.297-.647 1.346 0 2.362.446 3.047 1.339.686.893 1.029 2.224 1.029 3.993Zm16.047 8.531c-.864.355-1.744.622-2.641.8a14.67 14.67 0 0 1-2.844.266c-2.378 0-4.196-.637-5.452-1.91-1.257-1.274-1.886-3.109-1.886-5.504 0-2.319.605-4.151 1.816-5.497 1.21-1.345 2.86-2.018 4.951-2.018 2.107 0 3.743.624 4.907 1.872 1.163 1.249 1.745 3.007 1.745 5.275v1.511h-9.636c.009 1.117.339 1.951.991 2.501.651.55 1.625.825 2.92.825.854 0 1.697-.123 2.526-.368.829-.246 1.697-.635 2.603-1.168v3.415Zm-3.149-7.998c-.017-.982-.269-1.725-.755-2.228-.487-.504-1.204-.756-2.152-.756-.855 0-1.536.261-2.044.781-.508.521-.808 1.259-.901 2.216l5.852-.013Zm18.802 7.998c-.863.355-1.744.622-2.641.8a14.67 14.67 0 0 1-2.844.266c-2.378 0-4.195-.637-5.452-1.91-1.257-1.274-1.885-3.109-1.885-5.504 0-2.319.605-4.151 1.815-5.497 1.21-1.345 2.861-2.018 4.951-2.018 2.108 0 3.743.624 4.907 1.872 1.164 1.249 1.746 3.007 1.746 5.275v1.511h-9.636c.008 1.117.338 1.951.99 2.501.652.55 1.625.825 2.92.825.855 0 1.697-.123 2.526-.368.83-.246 1.697-.635 2.603-1.168v3.415Zm-3.149-7.998c-.017-.982-.268-1.725-.755-2.228-.487-.504-1.204-.756-2.152-.756-.855 0-1.536.261-2.044.781-.508.521-.808 1.259-.901 2.216l5.852-.013Zm13.572-9.56v4.037h4.862v2.857h-4.862v6.728c0 .635.154 1.09.463 1.365.309.275.823.413 1.543.413h2.856v2.856h-3.123c-2.133 0-3.576-.34-4.329-1.022-.753-.681-1.13-1.944-1.13-3.789v-6.551h-3.631v-2.857h3.631v-4.037h3.72Zm37.464 3.822-5.244 2.831 5.256 2.856-1.117 1.93-4.964-2.996v5.548h-2.247v-5.536c-.804.483-1.633.982-2.488 1.498-.855.517-1.68 1.012-2.476 1.486l-1.104-1.93 5.268-2.856-5.256-2.831 1.092-1.943 4.964 2.984v-5.536h2.247v5.536a323.25 323.25 0 0 0 2.495-1.498 307.61 307.61 0 0 1 2.469-1.486l1.105 1.943Zm12.149-4.52h2.806l-9.979 21.366h-2.806L936 416.954Zm-275.234 56.77h1.536c.88 0 1.472-.182 1.777-.546.305-.364.457-1.151.457-2.362v-2.602c0-1.388.207-2.37.622-2.945.415-.576 1.151-.953 2.209-1.13-1.066-.161-1.805-.525-2.215-1.092-.411-.567-.616-1.549-.616-2.945v-2.654c0-1.193-.15-1.972-.451-2.336-.3-.364-.895-.546-1.783-.546h-1.536v-2.412h1.904c2.099 0 3.502.311 4.208.933.707.622 1.06 1.869 1.06 3.739v2.73c0 1.278.231 2.16.692 2.647.462.486 1.289.73 2.482.73h.787v2.424h-.787c-1.193 0-2.02.246-2.482.737-.461.491-.692 1.371-.692 2.64v2.73c0 1.87-.353 3.119-1.06 3.745-.706.626-2.109.939-4.208.939h-1.904v-2.424Z"/> </svg>
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_doublecolon_view-transition-new/index.md
--- title: "::view-transition-new" slug: Web/CSS/::view-transition-new page-type: css-pseudo-element status: - experimental browser-compat: css.selectors.view-transition-new --- {{CSSRef}}{{SeeCompatTable}} The **`::view-transition-new`** [CSS](/en-US/docs/Web/CSS) [pseudo-element](/en-US/docs/Web/CSS/Pseudo-elements) represents the "new" view state of a view transition — a live representation of the new view, after the transition. During a view transition, `::view-transition-new` is included in the associated pseudo-element tree as explained in [The view transition process](/en-US/docs/Web/API/View_Transitions_API#the_view_transition_process). It is only ever a child of a {{cssxref("::view-transition-image-pair")}}, and never has any children. It is a replaced element, and therefore can be manipulated with properties such as {{cssxref("object-fit")}} and {{cssxref("object-position")}}. It has natural dimensions equal to the content's size. The following default styling is included in the UA stylesheet: ```css @keyframes -ua-view-transition-fade-in { from { opacity: 0; } } html::view-transition-new(*) { position: absolute; inset-block-start: 0; inline-size: 100%; block-size: auto; animation-name: -ua-view-transition-fade-in; animation-duration: inherit; animation-fill-mode: inherit; } ``` > **Note:** Additional view transition style sheet styles are also setup to animate `::view-transition-new`. These are dynamically generated during the view transition; see the specification [setup transition pseudo-elements](https://drafts.csswg.org/css-view-transitions-1/#setup-transition-pseudo-elements) and [update pseudo-element styles](https://drafts.csswg.org/css-view-transitions-1/#update-pseudo-element-styles) sections for more details. ## Syntax ```css-nolint ::view-transition-new(<pt-name-selector>) { /* ... */ } ``` `<pt-name-selector>` can be one of the following values: - `*` - : Causes the pseudo-element to match all view transition groups. - `root` - : Causes the pseudo-element to match the default `root` view transition group created by the user agent to contain the view transition for the overall page, meaning any element not assigned to its own specific view transition group via the {{cssxref("view-transition-name")}} property. - {{cssxref("custom-ident")}} - : Causes the pseudo-element to match a specific view transition group created by assigning the given {{cssxref("custom-ident")}} to an element via the {{cssxref("view-transition-name")}} property. ## Examples ```css figcaption { view-transition-name: figure-caption; } @keyframes grow-x { from { transform: scaleX(0); } to { transform: scaleX(1); } } @keyframes shrink-x { from { transform: scaleX(1); } to { transform: scaleX(0); } } ::view-transition-old(figure-caption), ::view-transition-new(figure-caption) { height: auto; right: 0; left: auto; transform-origin: right center; } ::view-transition-old(figure-caption) { animation: 0.25s linear both shrink-x; } ::view-transition-new(figure-caption) { animation: 0.25s 0.25s linear both grow-x; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [View Transitions API](/en-US/docs/Web/API/View_Transitions_API) - [Smooth and simple transitions with the View Transitions API](https://developer.chrome.com/docs/web-platform/view-transitions/)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/gradient/index.md
--- title: <gradient> slug: Web/CSS/gradient page-type: css-type browser-compat: css.types.image.gradient --- {{CSSRef}} The **`<gradient>`** [CSS](/en-US/docs/Web/CSS) [data type](/en-US/docs/Web/CSS/CSS_Types) is a special type of {{cssxref("&lt;image&gt;")}} that consists of a progressive transition between two or more colors. {{EmbedInteractiveExample("pages/css/type-gradient.html")}} A CSS gradient has [no intrinsic dimensions](/en-US/docs/Web/CSS/image#description); i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element to which it applies. ## Syntax The `<gradient>` data type is defined with one of the function types listed below. ### Linear gradient Linear gradients transition colors progressively along an imaginary line. They are generated with the {{cssxref("gradient/linear-gradient", "linear-gradient()")}} function. ### Radial gradient Radial gradients transition colors progressively from a center point (origin). They are generated with the {{cssxref("gradient/radial-gradient", "radial-gradient()")}} function. ### Conic gradient Conic gradients transition colors progressively around a circle. They are generated with the {{cssxref("gradient/conic-gradient", "conic-gradient()")}} function. ### Repeating gradient Repeating gradients duplicate a gradient as much as necessary to fill a given area. They are generated with the {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}, {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}}, and {{cssxref("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}} functions. ## Interpolation As with any interpolation involving colors, gradients are calculated in the alpha-premultiplied color space. This prevents unexpected shades of gray from appearing when both the color and the opacity are changing. (Be aware that older browsers may not use this behavior when using the [transparent keyword](/en-US/docs/Web/CSS/named-color#transparent).) ## Formal syntax {{csssyntax}} ## Examples ### Linear gradient example A simple linear gradient. ```html hidden <div class="linear-gradient">Linear gradient</div> ``` ```css hidden div { width: 240px; height: 80px; } ``` ```css .linear-gradient { background: linear-gradient( to right, red, orange, yellow, green, blue, indigo, violet ); } ``` {{EmbedLiveSample('Linear_gradient_example', 240, 120)}} ### Radial gradient example A simple radial gradient. ```html hidden <div class="radial-gradient">Radial gradient</div> ``` ```css hidden div { width: 240px; height: 80px; } ``` ```css .radial-gradient { background: radial-gradient(red, yellow, rgb(30 144 255)); } ``` {{EmbedLiveSample('Radial_gradient_example', 240, 120)}} ### Conic gradient example A simple conic gradient example. Note that this isn't supported widely across browser as of yet. ```html hidden <div class="conic-gradient">Conic gradient</div> ``` ```css hidden div { width: 200px; height: 200px; } ``` ```css .conic-gradient { background: conic-gradient(pink, coral, lime); } ``` {{EmbedLiveSample('Conic_gradient_example', 240, 240)}} ### Repeating gradient examples Simple repeating linear and radial gradient examples. ```html hidden <div class="linear-repeat"></div> <span>Repeating linear gradient</span> <hr /> <div class="radial-repeat"></div> <span>Repeating radial gradient</span> <hr /> <div class="conic-repeat"></div> <span>Repeating conic gradient</span> ``` ```css hidden div { display: inline-block; width: 240px; height: 80px; } span { font-weight: bold; vertical-align: top; } ``` ```css .linear-repeat { background: repeating-linear-gradient( to top left, pink, pink 5px, white 5px, white 10px ); } .radial-repeat { background: repeating-radial-gradient( lime, lime 15px, white 15px, white 30px ); } .conic-repeat { background: repeating-conic-gradient(lime, pink 30deg); } ``` {{EmbedLiveSample('Repeating_gradient_examples', 240, 300)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) - Gradient functions: {{cssxref("gradient/linear-gradient", "linear-gradient()")}}, {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}, {{cssxref("gradient/radial-gradient", "radial-gradient()")}}, {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}}, {{cssxref("gradient/conic-gradient", "conic-gradient()")}}, {{cssxref("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}} - [CSS Basic Data Types](/en-US/docs/Web/CSS/CSS_Types) - [CSS Units and Values](/en-US/docs/Web/CSS/CSS_Values_and_Units) - [Introduction to CSS: Values and Units](/en-US/docs/Learn/CSS/Building_blocks/Values_and_units)
0
data/mdn-content/files/en-us/web/css/gradient
data/mdn-content/files/en-us/web/css/gradient/conic-gradient/index.md
--- title: conic-gradient() slug: Web/CSS/gradient/conic-gradient page-type: css-function browser-compat: css.types.image.gradient.conic-gradient --- {{CSSRef}} The **`conic-gradient()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) creates an image consisting of a gradient with color transitions rotated around a center point (rather than radiating from the center). Example conic gradients include pie charts and {{glossary("color wheel", "color wheels")}}. The result of the `conic-gradient()` function is an object of the {{CSSxRef("&lt;gradient&gt;")}} data type, which is a special kind of {{CSSxRef("&lt;image&gt;")}}. {{EmbedInteractiveExample("pages/css/function-conic-gradient.html")}} ## Syntax ```css /* A conic gradient rotated 45 degrees, starting blue and finishing red */ conic-gradient(from 45deg, blue, red) /* A bluish purple box: the gradient goes from blue to red, but only the bottom right quadrant is visible, as the center of the conic gradient is at the top left corner */ conic-gradient(from 90deg at 0 0, blue, red) /* Interpolation in polar color space with longer hue interpolation method */ conic-gradient(in hsl longer hue, red, blue, green, red) /* Color wheel */ conic-gradient( hsl(360 100% 50%), hsl(315 100% 50%), hsl(270 100% 50%), hsl(225 100% 50%), hsl(180 100% 50%), hsl(135 100% 50%), hsl(90 100% 50%), hsl(45 100% 50%), hsl(0 100% 50%) ) ``` ### Values - {{CSSxRef("&lt;angle&gt;")}} - : Preceded by the `from` keyterm, and taking an angle as its value, defines the gradient rotation in clockwise direction. - `<position>` - : Using the same length, order, and keyterm values as the [`background-position`](/en-US/docs/Web/CSS/background-position) property, the `position` value defines center of the gradient. If not specified, the value used for `position` by default is `center`, meaning the gradient will be centered. - `<angular-color-stop>` - : A color-stop's {{CSSxRef("&lt;color&gt;")}} value, followed by one or two optional stop positions, (an {{CSSxRef("&lt;angle&gt;")}} along the gradient's circumference axis). - `<color-hint>` - : An {{Glossary("interpolation")}} hint defining how the gradient progresses between adjacent color stops. The length defines at which point between two color stops the gradient color should reach the midpoint of the color transition. If omitted, the midpoint of the color transition is the midpoint between two color stops. > **Note:** Rendering of [color stops in CSS gradients](#gradient_with_multiple_color_stops) follows the same rules as color stops in [SVG gradients](/en-US/docs/Web/SVG/Tutorial/Gradients). ## Description As with any gradient, a conic gradient has [no intrinsic dimensions](/en-US/docs/Web/CSS/image#description); i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element it applies to, or the size of the `<image>` if it is set to something other than the element size. To create a conic gradient that repeats so as to fill a 360 degree rotation, use the {{CSSxRef("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}} function instead. Because `<gradient>`s belong to the `<image>` data type, they can only be used where `<image>`s can be used. For this reason, `conic-gradient()` won't work on {{CSSxRef("background-color")}} and other properties that use the {{CSSxRef("&lt;color&gt;")}} data type. > **Note:** Why is it called a "conic" gradient? If the color stops are much lighter on one side than the other, it can look like a cone from above. ### Composition of a conic gradient The conic-gradient syntax is similar to the radial-gradient syntax, but the color-stops are placed around a gradient arc, the circumference of a circle, rather than on the gradient line emerging from the center of the gradient. With conic gradients, the colors transition as if spun around the center of a circle, starting at the top and going clockwise. In a radial gradient, the colors transition from the center of an ellipse, outward, in all directions. ![color stops along the circumference of a conic gradient and the axis of a radial gradient.](screenshot_2018-11-29_21.09.19.png) A conic gradient is specified by indicating a rotation angle, the center of the gradient, and then specifying a list of color-stops. Unlike linear and radial gradients, whose color-stops are placed by specifying a {{cssxref("length")}}, the color-stops of a conic gradient are specified with an [angle](/en-US/docs/Web/CSS/angle). Units include `deg` for degrees, `grad` for gradients, `rad` for radians, and `turn` for turns. There are 360 degrees, 400 gradians, 2π radians, and 1 turn in a circle. Browsers supporting conic gradients also accept percent values, with 100% equaling 360 degrees, but this is not in the specification. Similar to radial gradients, the conic gradient syntax provides for positioning the center of the gradient anywhere within, or even outside, the image. The values for the position are similar to the syntax for 2-value background-position. The gradient arc is the circumference of the gradient. The _starting point_ of the gradient or arc is north, or 12:00pm. The gradient is then rotated by the _from_ angle. The colors of the gradient are determined by the angled color stops, their starting points, ending points, and, in between, and optional angled color-stop points. The transitions between colors can be altered with color hints between adjacent colors' color stops. #### Customizing gradients By adding more angled color-stop points on the gradient arc, you can create a highly customized transition between multiple colors. A color-stop's position can be explicitly defined by using an {{CSSxRef("&lt;angle&gt;")}}. If you don't specify the location of a color stop, it is placed halfway between the one that precedes it and the one that follows it. If you don't specify an angle for the first or last color stop, their values are 0deg and 360deg respectively. The following two gradients are equivalent: ```css conic-gradient(red, orange, yellow, green, blue); conic-gradient(red 0deg, orange 90deg, yellow 180deg, green 270deg, blue 360deg); ``` By default, colors transition smoothly from the color at one color stop to the color at the subsequent color stop, with the midpoint between the colors being the half way point between the color transition. You can move this color transition midpoint to any point between two color stops by adding a color hint, indicating where the middle of the color transition should be. The following is solid red from the start to the 10% mark, transitions from red to blue over 80% of the turn, with the final 10% being solid blue. The midpoint of the red to blue gradient change, however, is at the 20% mark rather than the 50% mark as would have happened without the 80grad, or 20%, color hint. ```css conic-gradient(red 40grad, 80grad, blue 360grad); ``` If two or more color stops are at the same location, the transition will be a hard line between the first and last colors declared at that location. To use conic gradients to create pie charts — which is NOT the correct way to create pie charts as background images are not accessible — use hard color stops, where the color stop angles for two adjacent color stops are the same. The easiest way to do this is to use multiple position colors stops. The following two declarations are equivalent: ```css conic-gradient(#fff 0.09turn, #bbb 0.09turn, #bbb 0.27turn, #666 0.27turn, #666 0.54turn, #000 0.54turn); conic-gradient(#fff 0turn 0.09turn, #bbb 0.09turn 0.27turn, #666 0.27turn 0.54turn, #000 0.54turn 1turn); ``` Color stops should be listed in ascending order. Subsequent color stops of lower value will override the value of the previous color stop creating a hard transition. The following changes from red to yellow at the 30% mark, and then transitions from yellow to blue over 35% of the gradient: ```css conic-gradient(red .8rad, yellow .6rad, blue 1.3rad); ``` There are other effects you can create with conic gradients. Oddly, a checkerboard is one of them. By creating quadrants with an upper left and lower right white quadrant and lower left and upper right black quadrants, then repeating the gradient 16 times (four times across and four times down) you can make a checkerboard. ```css conic-gradient(#fff 90deg, #000 0.25turn 0.5turn, #fff 1rad 1.5rad, #000 300grad); background-size: 25% 25%; ``` And, yes, you can mix and match different angle units, but don't. The above is hard to read. ## Formal syntax {{csssyntax}} ## Accessibility concerns Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. While it is possible to create pie charts, checkerboards, and other effects with conic gradients, CSS images provide no native way to assign alternative text, and therefore the image represented by the conic gradient will not be accessible to screen reader users. If the image contains information critical to understanding the page's overall purpose, it is better to describe it semantically in the document. - [MDN Understanding WCAG, Guideline 1.1 explanations](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable#guideline_1.1_—_providing_text_alternatives_for_non-text_content) - [Understanding Success Criterion 1.1.1 | W3C Understanding WCAG 2.0](https://www.w3.org/TR/2016/NOTE-UNDERSTANDING-WCAG20-20161007/text-equiv-all.html) ## Examples ### Gradient at 40-degrees ```css hidden div { width: 100px; height: 100px; } ``` ```html hidden <div></div> ``` ```css div { background-image: conic-gradient(from 40deg, #fff, #000); } ``` {{EmbedLiveSample("Gradient_at_40-degrees", 120, 120)}} ### Off-centered gradient ```css hidden div { width: 100px; height: 100px; } ``` ```html hidden <div></div> ``` ```css div { background: conic-gradient(from 0deg at 0% 25%, blue, green, yellow 180deg); } ``` {{EmbedLiveSample("Off-centered_gradient", 120, 120)}} ### Gradient pie-chart This example uses multi-position color stops, with adjacent colors having the same color stop value, creating a striped effect. ```css hidden div { width: 100px; height: 100px; } ``` ```html hidden <div></div> ``` ```css div { background: conic-gradient(red 36deg, orange 36deg 170deg, yellow 170deg); border-radius: 50%; } ``` {{EmbedLiveSample("Gradient_pie-chart", 120, 120)}} ### Checkerboard ```css hidden div { width: 100px; height: 100px; } ``` ```html hidden <div></div> ``` ```css div { background: conic-gradient( #fff 0.25turn, #000 0.25turn 0.5turn, #fff 0.5turn 0.75turn, #000 0.75turn ) top left / 25% 25% repeat; border: 1px solid; } ``` {{EmbedLiveSample("Checkerboard", 120, 120)}} ### Interpolating with hue ```html hidden <div class="shorter"></div> <div class="longer"></div> ``` ```css hidden div { display: inline-block; margin-top: 1rem; width: 45vw; height: 80vh; } .shorter::before { content: "shorter hue"; display: block; margin-top: -1rem; } .longer::before { content: "longer hue"; display: block; margin-top: -1rem; } ``` In this example for interpolation [hsl](/en-US/docs/Web/CSS/color_value/hsl) color system is being used and [hue](/en-US/docs/Web/CSS/hue) is being interpolated. ```css .shorter { background-image: conic-gradient(in hsl shorter hue, red, blue); } .longer { background-image: conic-gradient(in hsl longer hue, red, blue); } ``` The box on the left uses [shorter interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#shorter), meaning color goes straight from red to blue using the shorter arc on the [color wheel](/en-US/docs/Glossary/Color_wheel). The box on the right uses [longer interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#longer), meaning the color goes from red to blue using the longer arc, traversing through greens, yellows, and oranges. {{EmbedLiveSample("Interpolating with hue", 240, 200)}} ### More conic-gradient examples Please see [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) for more examples. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) - Other gradient functions: {{cssxref("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}}, {{cssxref("gradient/linear-gradient", "linear-gradient()")}}, {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}, {{cssxref("gradient/radial-gradient", "radial-gradient()")}}, {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}} - [`<hue-interpolation-method>`](/en-US/docs/Web/CSS/hue-interpolation-method) - [`<color-interpolation-method>`](/en-US/docs/Web/CSS/color-interpolation-method) - {{cssxref("&lt;image&gt;")}} - {{cssxref("image/image","image()")}} - {{cssxref("element", "element()")}} - {{cssxref("image/image-set","image-set()")}} - {{cssxref("cross-fade", "cross-fade()")}}
0
data/mdn-content/files/en-us/web/css/gradient
data/mdn-content/files/en-us/web/css/gradient/linear-gradient/index.md
--- title: linear-gradient() slug: Web/CSS/gradient/linear-gradient page-type: css-function browser-compat: css.types.image.gradient.linear-gradient --- {{CSSRef}} The **`linear-gradient()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the {{CSSxRef("&lt;gradient&gt;")}} data type, which is a special kind of {{CSSxRef("&lt;image&gt;")}}. {{EmbedInteractiveExample("pages/css/function-linear-gradient.html")}} ## Syntax ```css /* A gradient tilted 45 degrees, starting blue and finishing red */ linear-gradient(45deg, blue, red) /* A gradient going from the bottom right to the top left corner, starting blue and finishing red */ linear-gradient(to left top, blue, red) /* Interpolation in rectangular color space */ linear-gradient(in oklab, blue, red) /* Interpolation in polar color space */ linear-gradient(in hsl, blue, red) /* Interpolation in polar color space with longer hue interpolation method */ linear-gradient(in hsl longer hue, blue, red) /* Color stop: A gradient going from the bottom to top, starting blue, turning green at 40% of its length, and finishing red */ linear-gradient(0deg, blue, green 40%, red) /* Color hint: A gradient going from the left to right, starting red, getting to the midpoint color 10% of the way across the length of the gradient, taking the rest of the 90% of the length to change to blue */ linear-gradient(.25turn, red, 10%, blue) /* Multi-position color stop: A gradient tilted 45 degrees, with a red bottom-left half and a blue top-right half, with a hard line where the gradient changes from red to blue */ linear-gradient(45deg, red 0 50%, blue 50% 100%) ``` ### Values - `<side-or-corner>` - : The position of the gradient line's starting point. If specified, it consists of the word `to` and up to two keywords: one indicates the horizontal side (`left` or `right`), and the other the vertical side (`top` or `bottom`). The order of the side keywords does not matter. If unspecified, it defaults to `to bottom`. The values `to top`, `to bottom`, `to left`, and `to right` are equivalent to the angles `0deg`, `180deg`, `270deg`, and `90deg`, respectively. The other values are translated into an angle. - {{CSSxRef("&lt;angle&gt;")}} - : The gradient line's angle of direction. A value of `0deg` is equivalent to `to top`; increasing values rotate clockwise from there. - `<linear-color-stop>` - : A color-stop's {{CSSxRef("&lt;color&gt;")}} value, followed by one or two optional stop positions, (each being either a {{CSSxRef("&lt;percentage&gt;")}} or a {{CSSxRef("&lt;length&gt;")}} along the gradient's axis). - `<color-hint>` - : An interpolation hint defining how the gradient progresses between adjacent color stops. The length defines at which point between two color stops the gradient color should reach the midpoint of the color transition. If omitted, the midpoint of the color transition is the midpoint between two color stops. > **Note:** Rendering of [color stops in CSS gradients](#composition_of_a_linear_gradient) follows the same rules as color stops in [SVG gradients](/en-US/docs/Web/SVG/Tutorial/Gradients). ## Description As with any gradient, a linear gradient has [no intrinsic dimensions](/en-US/docs/Web/CSS/image#description); i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element it applies to. To create a linear gradient that repeats to fill its container, use the {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}} function instead. Because `<gradient>`s belong to the `<image>` data type, they can only be used where [`<image>`](/en-US/docs/Web/CSS/image)s can be used. For this reason, `linear-gradient()` won't work on {{CSSxRef("background-color")}} and other properties that use the {{CSSxRef("&lt;color&gt;")}} data type. ### Composition of a linear gradient A linear gradient is defined by an axis—the _gradient line_—and two or more _color-stop points_. Each point on the axis is a distinct color; to create a smooth gradient, the `linear-gradient()` function draws a series of colored lines perpendicular to the gradient line, each one matching the color of the point where it intersects the gradient line. ![linear-gradient.png](linear-gradient.png) The gradient line is defined by the center of the box containing the gradient image and by an angle. The colors of the gradient are determined by two or more points: the starting point, the ending point, and, in between, optional color-stop points. The _starting point_ is the location on the gradient line where the first color begins. The _ending point_ is the point where the last color ends. Each of these two points is defined by the intersection of the gradient line with a perpendicular line passing from the box corner which is in the same quadrant. The ending point can be understood as the symmetrical point of the starting point. These somewhat complex definitions lead to an interesting effect sometimes called _magic corners_: the corners nearest to the starting and ending points have the same color as their respective starting or ending points. #### Customizing Gradients By adding more color-stop points on the gradient line, you can create a highly customized transition between multiple colors. A color-stop's position can be explicitly defined by using a {{CSSxRef("&lt;length&gt;")}} or a {{CSSxRef("&lt;percentage&gt;")}}. If you don't specify the location of a color, it is placed halfway between the one that precedes it and the one that follows it. The following two gradients are equivalent. ```css linear-gradient(red, orange, yellow, green, blue); linear-gradient(red 0%, orange 25%, yellow 50%, green 75%, blue 100%); ``` By default, colors transition smoothly from the color at one color-stop to the color at the subsequent color-stop, with the midpoint between the colors being the halfway point between the color transition. You can move this midpoint to any position between two color-stops by adding an unlabelled % color hint between the two colors to indicate where the middle of the color transition should be. The following example is solid red from the start to the 10% mark and solid blue from 90% to the end. Between 10% and 90% the color transitions from red to blue, however, the midpoint of the transition is at the 30% mark rather than 50% as would have happened without the 30% color hint. ```css linear-gradient(red 10%, 30%, blue 90%); ``` If two or more color-stops are at the same location, the transition will be a hard line between the first and last colors declared at that location. Color-stops should be listed in ascending order. Subsequent color-stops of lower value will override the value of the previous color-stop creating a hard transition. The following changes from red to yellow at the 40% mark, and then transitions from yellow to blue over 25% of the gradient: ```css linear-gradient(red 40%, yellow 30%, blue 65%); ``` Multi-position color-stops are allowed. A color can be declared as two adjacent color-stops by including both positions in the CSS declaration. The following three gradients are equivalent: ```css linear-gradient(red 0%, orange 10%, orange 30%, yellow 50%, yellow 70%, green 90%, green 100%); linear-gradient(red, orange 10% 30%, yellow 50% 70%, green 90%); linear-gradient(red 0%, orange 10% 30%, yellow 50% 70%, green 90% 100%); ``` By default, if there is no color with a `0%` stop, the first color declared will be at that point. Similarly, the last color will continue to the `100%` mark, or be at the `100%` mark if no length has been declared on that last stop. ## Formal syntax {{csssyntax}} ## Examples ### Gradient at a 45-degree angle ```css hidden body { width: 100vw; height: 100vh; } ``` ```css body { background: linear-gradient(45deg, red, blue); } ``` {{EmbedLiveSample("Gradient_at_a_45-degree_angle", 120, 120)}} ### Gradient that starts at 60% of the gradient line ```css hidden body { width: 100vw; height: 100vh; } ``` ```css body { background: linear-gradient(135deg, orange 60%, cyan); } ``` {{EmbedLiveSample("Gradient_that_starts_at_60_of_the_gradient_line", 120, 120)}} ### Interpolation in rectangular color space ```css hidden body { width: 100vw; height: 100vh; } ``` ```css body { background: linear-gradient(90deg in oklab, blue, red); } ``` {{EmbedLiveSample("Interpolation in rectangular color space", 120, 120)}} ### Interpolating with hue ```html hidden <div class="shorter">shorter hue</div> <div class="longer">longer hue</div> ``` ```css hidden div { height: 50vh; color: white; font-weight: bolder; } ``` In this example for interpolation, [hsl](/en-US/docs/Web/CSS/color_value/hsl) color system is being used and [hue](/en-US/docs/Web/CSS/hue) is being interpolated. ```css .shorter { background: linear-gradient(90deg in hsl shorter hue, red, blue); } .longer { background: linear-gradient(90deg in hsl longer hue, red, blue); } ``` The box on the top uses [shorter interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#shorter), meaning color goes straight from red to blue using the shorter arc on [color wheel](/en-US/docs/Glossary/Color_wheel). The box on the bottom uses [longer interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#longer), meaning the color goes from red to blue using the longer arc, traversing through greens, yellows, and oranges. {{EmbedLiveSample("Interpolating with hue", 120, 120)}} ### Gradient with multi-position color-stops This example uses multi-position color-stops, with adjacent colors having the same color-stop value, creating a striped effect. ```css hidden body { width: 100vw; height: 100vh; } ``` ```css body { background: linear-gradient( to right, red 20%, orange 20% 40%, yellow 40% 60%, green 60% 80%, blue 80% ); } ``` {{EmbedLiveSample("Gradient_with_multi-position_color_stops", 120, 120)}} ### More linear-gradient examples Please see [using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) for more examples. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) - Other gradient functions: {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}, {{cssxref("gradient/radial-gradient", "radial-gradient()")}}, {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}}, {{cssxref("gradient/conic-gradient", "conic-gradient()")}}, {{cssxref("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}} - [`<hue-interpolation-method>`](/en-US/docs/Web/CSS/hue-interpolation-method) - [`<color-interpolation-method>`](/en-US/docs/Web/CSS/color-interpolation-method) - {{CSSxRef("&lt;image&gt;")}} - {{cssxref("element", "element()")}} - {{cssxref("image/image","image()")}} - {{cssxref("image/image-set","image-set()")}} - {{cssxref("cross-fade", "cross-fade()")}} - [CSS images module](/en-US/docs/Web/CSS/CSS_images) - [New functions, gradients, and hues in CSS colors (Level 4)](/en-US/blog/css-color-module-level-4/) on MDN blog (2023)
0
data/mdn-content/files/en-us/web/css/gradient
data/mdn-content/files/en-us/web/css/gradient/repeating-linear-gradient/index.md
--- title: repeating-linear-gradient() slug: Web/CSS/gradient/repeating-linear-gradient page-type: css-function browser-compat: css.types.image.gradient.repeating-linear-gradient --- {{CSSRef}} The **`repeating-linear-gradient()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) creates an image consisting of repeating linear gradients. It is similar to {{cssxref("gradient/linear-gradient", "linear-gradient()")}} and takes the same arguments, but it repeats the color stops infinitely in all directions so as to cover its entire container. The function's result is an object of the {{cssxref("&lt;gradient&gt;")}} data type, which is a special kind of {{cssxref("&lt;image&gt;")}}. {{EmbedInteractiveExample("pages/css/function-repeating-linear-gradient.html")}} The length of the gradient that repeats is the distance between the first and last color stop. If the first color does not have a color-stop-length, the color-stop-length defaults to 0. With each repetition, the positions of the color stops are shifted by a multiple of the length of the basic linear gradient. Thus, the position of each ending color stop coincides with a starting color stop; if the color values are different, this will result in a sharp visual transition. This can be altered with repeating the first color again as the last color. As with any gradient, a repeating linear gradient has [no intrinsic dimensions](/en-US/docs/Web/CSS/image#description); i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element it applies to. Because `<gradient>`s belong to the `<image>` data type, they can only be used where `<image>`s can be used. For this reason, `repeating-linear-gradient()` won't work on {{Cssxref("background-color")}} and other properties that use the {{cssxref("&lt;color&gt;")}} data type. ## Syntax ```css /* A repeating gradient tilted 45 degrees, starting blue and finishing red, repeating 3 times */ repeating-linear-gradient(45deg, blue, red 33.3%) /* A repeating gradient going from the bottom right to the top left, starting blue and finishing red, repeating every 20px */ repeating-linear-gradient(to left top, blue, red 20px) /* A gradient going from the bottom to top, starting blue, turning green after 40%, and finishing red. This gradient doesn't repeat because the last color stop defaults to 100% */ repeating-linear-gradient(0deg, blue, green 40%, red) /* A gradient repeating five times, going from the left to right, starting red, turning green, and back to red */ repeating-linear-gradient(to right, red 0%, green 10%, red 20%) /* Interpolation in rectangular color space */ repeating-linear-gradient(in oklab, blue, red 50px) /* Interpolation in polar color space */ repeating-linear-gradient(in hsl, blue, red 50px) /* Interpolation in polar color space with longer hue interpolation method */ repeating-linear-gradient(in hsl longer hue, blue, red 50px) ``` ### Values - `<side-or-corner>` - : The position of the gradient line's starting point. If specified, it consists of the word `to` and up to two keywords: one indicates the horizontal side (`left` or `right`), and the other the vertical side (`top` or `bottom`). The order of the side keywords does not matter. If unspecified, it defaults to `to bottom`. The values `to top`, `to bottom`, `to left`, and `to right` are equivalent to the angles `0deg`, `180deg`, `270deg`, and `90deg` respectively. The other values are translated into an angle. - {{cssxref("&lt;angle&gt;")}} - : The gradient line's angle of direction. A value of `0deg` is equivalent to `to top`; increasing values rotate clockwise from there. - `<linear-color-stop>` - : A color-stop's {{CSSxRef("&lt;color&gt;")}} value, followed by one or two optional stop positions, (each being either a {{CSSxRef("&lt;percentage&gt;")}} or a {{CSSxRef("&lt;length&gt;")}} along the gradient's axis). A percentage of `0%`, or a length of `0`, represents the start of the gradient; the value `100%` is 100% of the image size, meaning the gradient will not repeat. - `<color-hint>` - : The color-hint is an interpolation hint defining how the gradient progresses between adjacent color stops. The length defines at which point between two color stops the gradient color should reach the midpoint of the color transition. If omitted, the midpoint of the color transition is the midpoint between two color stops. > **Note:** Rendering of [color stops in CSS gradients](#gradient_with_multiple_color_stops) follows the same rules as color stops in [SVG gradients](/en-US/docs/Web/SVG/Tutorial/Gradients). ### Formal syntax {{csssyntax}} ## Examples ### Zebra stripes ```css hidden body { width: 100vw; height: 100vh; } ``` ```css body { background-image: repeating-linear-gradient( -45deg, transparent, transparent 20px, black 20px, black 40px ); /* with multiple color stop lengths */ background-image: repeating-linear-gradient( -45deg, transparent 0 20px, black 20px 40px ); } ``` {{EmbedLiveSample('Zebra_stripes', 120, 120)}} ### Ten repeating horizontal bars ```css hidden body { width: 100vw; height: 100vh; } ``` ```css body { background-image: repeating-linear-gradient( to bottom, rgb(26 198 204), rgb(26 198 204) 7%, rgb(100 100 100) 10% ); } ``` {{EmbedLiveSample('Ten_repeating_horizontal_bars', 120, 120)}} Because the last color stop is 10% and the gradient is vertical, each gradient in the repeated gradient is 10% of the height, fitting 10 horizontal bars. ### Interpolation in rectangular color space ```css hidden body { width: 100vw; height: 100vh; } ``` ```css body { background: repeating-linear-gradient(90deg in oklab, blue, red 100px); } ``` {{EmbedLiveSample("Interpolation in rectangular color space", 120, 120)}} ### Interpolating with hue ```html hidden <div class="shorter">shorter hue</div> <div class="longer">longer hue</div> ``` ```css hidden div { height: 50vh; color: #330; font-weight: bolder; padding-left: 1.5rem; } ``` In this example for interpolation, [hsl](/en-US/docs/Web/CSS/color_value/hsl) color system is being used and [hue](/en-US/docs/Web/CSS/hue) is being interpolated. ```css .shorter { background: repeating-linear-gradient( 90deg in hsl shorter hue, red, blue 300px ); } .longer { background: repeating-linear-gradient( 90deg in hsl longer hue, red, blue 300px ); } ``` The box on the top uses [shorter interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#shorter), meaning the color goes from red to blue using the shorter arc on the [color wheel](/en-US/docs/Glossary/Color_wheel). The box on the bottom uses [longer interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#longer), meaning the color goes from red to blue using the longer arc, traversing through greens, yellows, and oranges. {{EmbedLiveSample("Interpolating with hue", 120, 120)}} > **Note:** Please see [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) for more examples. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) - Other gradient functions: {{cssxref("gradient/linear-gradient", "linear-gradient()")}}, {{cssxref("gradient/radial-gradient", "radial-gradient()")}}, {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}}, {{cssxref("gradient/conic-gradient", "conic-gradient()")}}, {{cssxref("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}} - [`<hue-interpolation-method>`](/en-US/docs/Web/CSS/hue-interpolation-method) - [`<color-interpolation-method>`](/en-US/docs/Web/CSS/color-interpolation-method) - {{cssxref("&lt;image&gt;")}} - {{cssxref("image/image","image()")}} - {{cssxref("element", "element()")}} - {{cssxref("image/image-set","image-set()")}} - {{cssxref("cross-fade", "cross-fade()")}}
0
data/mdn-content/files/en-us/web/css/gradient
data/mdn-content/files/en-us/web/css/gradient/repeating-radial-gradient/index.md
--- title: repeating-radial-gradient() slug: Web/CSS/gradient/repeating-radial-gradient page-type: css-function browser-compat: css.types.image.gradient.repeating-radial-gradient --- {{CSSRef}} The **`repeating-radial-gradient()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) creates an image consisting of repeating gradients that radiate from an origin. It is similar to {{cssxref("gradient/radial-gradient", "radial-gradient()")}} and takes the same arguments, but it repeats the color stops infinitely in all directions so as to cover its entire container, similar to {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}. The function's result is an object of the {{cssxref("&lt;gradient&gt;")}} data type, which is a special kind of {{cssxref("&lt;image&gt;")}}. {{EmbedInteractiveExample("pages/css/function-repeating-radial-gradient.html")}} With each repetition, the positions of the color stops are shifted by a multiple of the dimensions of the basic radial gradient (the distance between the last color stop and the first). Thus, the position of each ending color stop coincides with a starting color stop; if the color values are different, this will result in a sharp visual transition, which can be mitigated by repeating the first color as the last color. As with any gradient, a repeating radial gradient has [no intrinsic dimensions](/en-US/docs/Web/CSS/image#description); i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element it applies to. Because `<gradient>`s belong to the `<image>` data type, they can only be used where `<image>`s can be used. For this reason, `repeating-radial-gradient()` won't work on {{cssxref("background-color")}} and other properties that use the {{cssxref("&lt;color&gt;")}} data type. ## Syntax ```css /* A gradient at the center of its container, starting red, changing to blue, and finishing green, with the colors repeating every 30px */ repeating-radial-gradient(circle at center, red 0, blue, green 30px) /* An elliptical gradient near the top left of its container, starting red, changing to green and back again, repeating five times between the center and the bottom right corner, and only once between the center and the top left corner */ repeating-radial-gradient(farthest-corner at 20% 20%, red 0, green, red 20%) ``` ### Values - {{cssxref("&lt;position&gt;")}} - : The position of the gradient, interpreted in the same way as {{cssxref("background-position")}} or {{cssxref("transform-origin")}}. If unspecified, it defaults to `center`. - `<shape>` - : The gradient's shape. The value can be `circle` (meaning that the gradient's shape is a circle with constant radius) or `ellipse` (meaning that the shape is an axis-aligned ellipse). If unspecified, it defaults to `ellipse`. - `<extent-keyword>` - : A keyword describing how big the ending shape must be. The possible values are: | Keyword | Description | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `closest-side` | The gradient's ending shape meets the side of the box closest to its center (for circles) or meets both the vertical and horizontal sides closest to the center (for ellipses). | | `closest-corner` | The gradient's ending shape is sized so that it exactly meets the closest corner of the box from its center. | | `farthest-side` | Similar to `closest-side`, except the ending shape is sized to meet the side of the box farthest from its center (or vertical and horizontal sides). | | `farthest-corner` | The gradient's ending shape is sized so that it exactly meets the farthest corner of the box from its center. | > **Note:** Early implementations of this function included other keywords (`cover` and `contain`) as synonyms of the standard `farthest-corner` and `closest-side`, respectively. Use the standard keywords only, as some implementations have already dropped those older variants. - `<color-stop>` - : A color-stop's {{cssxref("&lt;color&gt;")}} value, followed by an optional stop position (either a {{cssxref("&lt;percentage&gt;")}} or a {{cssxref("&lt;length&gt;")}} along the gradient's axis). A percentage of `0%`, or a length of `0`, represents the center of the gradient; the value `100%` represents the intersection of the ending shape with the virtual gradient ray. Percentage values in between are linearly positioned on the virtual gradient ray. ### Formal syntax {{csssyntax}} ## Examples ### Black and white gradient ```html hidden <div class="radial-gradient"></div> ``` ```css hidden .radial-gradient { width: 120px; height: 120px; } ``` ```css .radial-gradient { background: repeating-radial-gradient( black, black 5px, white 5px, white 10px ); } ``` {{EmbedLiveSample('Black_and_white_gradient', 120, 120)}} ### Farthest-corner ```html hidden <div class="radial-gradient"></div> ``` ```css hidden .radial-gradient { width: 240px; height: 120px; } ``` ```css .radial-gradient { background: repeating-radial-gradient( ellipse farthest-corner at 20% 20%, red, black 5%, blue 5%, green 10% ); background: repeating-radial-gradient( ellipse farthest-corner at 20% 20%, red 0 5%, green 5% 10% ); } ``` {{EmbedLiveSample('Farthest-corner', 120, 120)}} The elliptical gradient will be centered 20% from the top left, and will repeat 10 times between the center and the farthest corner (the bottom right corner). Browsers supporting multi position color stops will display a red and green striped ellipse. Browsers not supporting the syntax yet will see a gradient that goes from red to black and then from blue to green. ### Interpolating with hue ```html hidden <div class="shorter"></div> <div class="longer"></div> ``` ```css hidden div { display: inline-block; margin-top: 1rem; width: 45vw; height: 80vh; } .shorter::before { content: "shorter hue"; display: block; margin-top: -1rem; } .longer::before { content: "longer hue"; display: block; margin-top: -1rem; } ``` In this example for interpolation, [hsl](/en-US/docs/Web/CSS/color_value/hsl) color system is being used and [hue](/en-US/docs/Web/CSS/hue) is being interpolated. ```css .shorter { background-image: repeating-radial-gradient( circle at center in hsl shorter hue, red 30px, blue 60px ); } .longer { background-image: repeating-radial-gradient( circle at center in hsl longer hue, red 30px, blue 60px ); } ``` The box on the left uses [shorter interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#shorter), meaning the color goes from red to blue using the shorter arc on [color wheel](/en-US/docs/Glossary/Color_wheel). The box on the right uses [longer interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#longer), meaning the color goes from red to blue using the longer arc, traversing through greens, yellows, and oranges. {{EmbedLiveSample("Interpolating with hue", 240, 200)}} > **Note:** Please see [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) for more examples. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) - Other gradient functions: {{cssxref("gradient/radial-gradient", "radial-gradient()")}}, {{cssxref("gradient/linear-gradient", "linear-gradient()")}}, {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}, {{cssxref("gradient/conic-gradient", "conic-gradient()")}}, {{cssxref("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}} - [`<hue-interpolation-method>`](/en-US/docs/Web/CSS/hue-interpolation-method) - [`<color-interpolation-method>`](/en-US/docs/Web/CSS/color-interpolation-method) - {{cssxref("&lt;image&gt;")}} - {{cssxref("image/image","image()")}} - {{cssxref("element", "element()")}} - {{cssxref("image/image-set","image-set()")}} - {{cssxref("cross-fade", "cross-fade()")}}
0
data/mdn-content/files/en-us/web/css/gradient
data/mdn-content/files/en-us/web/css/gradient/repeating-conic-gradient/index.md
--- title: repeating-conic-gradient() slug: Web/CSS/gradient/repeating-conic-gradient page-type: css-function browser-compat: css.types.image.gradient.repeating-conic-gradient --- {{CSSRef}} The **`repeating-conic-gradient()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) creates an image consisting of a repeating gradient (rather than a {{cssxref("gradient/conic-gradient","single gradient")}}) with color transitions rotated around a center point (rather than {{cssxref("gradient/repeating-radial-gradient","radiating from the center")}}). {{EmbedInteractiveExample("pages/css/function-repeating-conic-gradient.html")}} ## Syntax ```css /* Starburst: a blue on blue starburst: the gradient is a starburst of lighter and darker blue, centered in the upper left quadrant, offset by 3degrees so there is no up/down straight line */ repeating-conic-gradient( from 3deg at 25% 25%, hsl(200 100% 50%) 0deg 15deg, hsl(200 100% 60%) 10deg 30deg ) /* Interpolation in polar color space with longer hue interpolation method */ repeating-conic-gradient(in hsl shorter hue, red, blue 90deg, green 180deg) ``` ### Values - {{CSSxRef("&lt;angle&gt;")}} - : Preceded by the `from` keyterm, and taking an angle as its value, defines the gradient rotation in clockwise direction. - `<position>` - : Using the same length, order and keyterm values as the [background-position](/en-US/docs/Web/CSS/background-position) property, the position defines center of the gradient. If omitted, the default value is `center`, meaning the gradient will be centered. - `<angular-color-stop>` - : A color-stop's {{CSSxRef("&lt;color&gt;")}} value, followed by one or two optional stop positions, (an {{CSSxRef("&lt;angle&gt;")}} along the gradient's circumference axis). The last color stop minus the first color-stop angle defines the size of the repeating gradient. - `<color-hint>` - : An {{Glossary("interpolation")}} hint defining how the gradient progresses between adjacent color stops. The length defines at which point between two color stops the gradient color should reach the midpoint of the color transition. If omitted, the midpoint of the color transition is the midpoint between two color stops. > **Note:** Rendering of [color stops in CSS gradients](#gradient_with_multiple_color_stops) follows the same rules as color stops in [SVG gradients](/en-US/docs/Web/SVG/Tutorial/Gradients). ## Description Example repeating conic gradients include starbursts. The result of the `repeating-conic-gradient()` function is an object of the {{CSSxRef("&lt;gradient&gt;")}} data type, which is a special kind of {{CSSxRef("&lt;image&gt;")}}. If neither the first nor the last color stops include a color stop angle greater than 0deg or less than 360 degrees respectively, the conic-gradient will not repeat. As with any gradient, a repeating-conic gradient has [no intrinsic dimensions](/en-US/docs/Web/CSS/image#description); i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element it applies to, or the size the `<image>` is set to if it's set to something other than the element's size. Because `<gradient>`s belong to the `<image>` data type, they can only be used where `<image>`s can be used. For this reason, `repeating-conic-gradient()` won't work on {{CSSxRef("background-color")}} and other properties that use the {{CSSxRef("&lt;color&gt;")}} data type. > **Note:** To create a conic gradient that does not repeat, make the gradient a full 360 degree rotation, or use the {{cssxref("gradient/conic-gradient", "conic-gradient()")}} function instead. ### Understanding repeating conic gradients The repeating-conic-gradient syntax is similar to the {{cssxref("gradient/conic-gradient", "conic-gradient()")}} and {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}} syntax. Like the non-repeating conic-gradient, the color-stops are placed around a gradient arc. Like the repeating radial gradient, the size of the repeating section is the first color stop subtracted from the angle of the last color stop. ![Comparison of the color stops for repeating and non-repeating conic and radial gradients](repeatingconicgradient.png) The above gradients are defined as being one third blue, one third red, and one third yellow. ```css repeating-conic-gradient(from 0deg, red 0deg 30deg, yellow 30deg 60deg, blue 60deg 90deg); repeating-radial-gradient(red 0 8%, yellow 8% 16%, blue 16% 24%); conic-gradient(red 120deg, yellow 120deg 240deg, blue 240deg); radial-gradient(red 33%, yellow 33% 66%, blue 66%); ``` For a repeating gradient to repeat we define the first and last color stops. Like in non-repeating gradients, the first and last color stops are assumed to be 0 and either 100% or 360deg if not explicitly declared. When defaulting to these values, the repeating arc is 360 degrees, and therefore doesn't repeat. Like the non-repeating conic gradient, the color-stops are placed around a gradient arc — the circumference of a circle, rather than on the gradient line emerging from the center of the gradient. The colors transition as if spun around the center of a circle, starting at the top if no `from <angle>` is declared, and going clockwise for the size of the angle that is the different between the largest and smallest color angle, then repeating. A repeating conic gradient is specified by indicating a rotation angle, the center of the gradient, and then specifying a list of color-stops. Like non-repeating conic gradients, the color-stops of a repeating conic gradient are specified with an {{cssxref('angle')}}. Units include `deg` for degrees, `grad` for gradients, `rad` for radians, and `turn` for turns. There are 360 degrees, 400 gradians, 2π radians, and 1 turn in a circle. Browsers supporting repeating conic gradients also accept percent values, with 100% equaling 360 degrees, but this is not in the specification. Radial and conic gradient syntax provides for positioning the center of the gradient anywhere within, or even outside, the image. The values for the position are similar to the syntax for 2-value {{cssxref('background-position')}}. The gradient arc is part of the circumference of the gradient. 0 degrees is north, or 12:00pm. The colors of the gradient are determined by the angled color stops, their starting points, ending points, and, in between, and optional angled color-stop points. The transitions between colors can be altered with color hints between adjacent colors' color stops. #### Customizing gradients By adding more angled color-stop points on the gradient arc, you can create a highly customized transition between multiple colors. A color-stop's position can be explicitly defined by using an {{CSSxRef("&lt;angle&gt;")}}. If you don't specify the location of a color stop, it is placed halfway between the one that precedes it and the one that follows it. Like the non-repeating gradient counterpart, if you don't specify an angle for the first or last color stop, the values will be 0deg and 360deg. If you don't declare an angle for either, you'll get a non-repeating conic gradient. If you declare a non 0 or 360degree for the first or last respectively, the gradient will repeat based on that value. For example, if you don't declare an angle for the first color, and declare 10% on the last color stop, the arc will repeat 10 times. Rather, the starting point is the first color stop declared, and the last color stop is the last color stop angle declared. The following two gradients are equivalent: ```css repeating-conic-gradient(red, orange, yellow, green, blue 50%); repeating-conic-gradient(from -45deg, red 45deg, orange, yellow, green, blue 225deg) ``` By default, colors transition smoothly from the color at one color stop to the color at the subsequent color stop, with the midpoint between the colors being the half way point between the color transition. You can move this color transition midpoint to any point between two color stops by adding a color hint, indicating where the middle of the color transition should be. If two or more color stops are at the same location, the transition will be a hard line between the first and last colors declared at that location. While you can mix and match different angle units, don't. It makes CSS hard to read. ### Formal syntax {{csssyntax}} ## Accessibility concerns Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. While it is possible to create pie charts, checkerboards, and other effects with conic gradients, CSS images provide no native way to assign alternative text, and therefore the image represented by the conic gradient will not be accessible to screen reader users. If the image contains information critical to understanding the page's overall purpose, it is better to describe it semantically in the document. - [MDN Understanding WCAG, Guideline 1.1 explanations](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable#guideline_1.1_—_providing_text_alternatives_for_non-text_content) - [Understanding Success Criterion 1.1.1 | W3C Understanding WCAG 2.0](https://www.w3.org/TR/2016/NOTE-UNDERSTANDING-WCAG20-20161007/text-equiv-all.html) ## Examples ### Black and white starburst ```css hidden div { width: 200px; height: 200px; } ``` ```html hidden <div></div> ``` ```css div { background-image: repeating-conic-gradient(#fff 0 9deg, #000 9deg 18deg); } ``` {{EmbedLiveSample("Black_and_white_starburst", 220, 220)}} ### Off-centered gradient This gradient repeats 18 times, but since we only see the right half, we only see 9 repeats. ```css hidden div { width: 200px; height: 200px; } ``` ```html hidden <div></div> ``` ```css div { background: repeating-conic-gradient( from 3deg at 25% 25%, green, blue 2deg 5deg, green, yellow 15deg 18deg, green 20deg ); } ``` {{EmbedLiveSample("Off-centered_gradient", 220, 220)}} ### Interpolating with hue ```html hidden <div class="shorter"></div> <div class="longer"></div> ``` ```css hidden div { display: inline-block; margin-top: 1rem; width: 45vw; height: 80vh; } .shorter::before { content: "shorter hue"; display: block; margin-top: -1rem; } .longer::before { content: "longer hue"; display: block; margin-top: -1rem; } ``` In this example for interpolation, [hsl](/en-US/docs/Web/CSS/color_value/hsl) color system is being used and [hue](/en-US/docs/Web/CSS/hue) is being interpolated. ```css .shorter { background-image: repeating-conic-gradient( in hsl shorter hue, red, blue 180deg ); } .longer { background-image: repeating-conic-gradient( in hsl longer hue, red, blue 180deg ); } ``` The box on the left uses [shorter interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#shorter), meaning color goes straight from red to blue using the shorter arc on the [color wheel](/en-US/docs/Glossary/Color_wheel). The box on the right uses [longer interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#longer), meaning the color goes from red to blue using the longer arc, traversing through greens, yellows, and oranges. {{EmbedLiveSample("Interpolating with hue", 240, 200)}} ### More repeating-conic-gradient examples Please see [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) for more examples. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) - Other gradient functions: {{cssxref("gradient/conic-gradient", "conic-gradient()")}}, {{cssxref("gradient/linear-gradient", "linear-gradient()")}}, {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}, {{cssxref("gradient/radial-gradient", "radial-gradient()")}}, {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}} - [`<hue-interpolation-method>`](/en-US/docs/Web/CSS/hue-interpolation-method) - [`<color-interpolation-method>`](/en-US/docs/Web/CSS/color-interpolation-method) - {{cssxref("&lt;image&gt;")}} - {{cssxref("image/image","image()")}} - {{cssxref("element", "element()")}} - {{cssxref("image/image-set","image-set()")}} - {{cssxref("cross-fade", "cross-fade()")}}
0
data/mdn-content/files/en-us/web/css/gradient
data/mdn-content/files/en-us/web/css/gradient/radial-gradient/index.md
--- title: radial-gradient() slug: Web/CSS/gradient/radial-gradient page-type: css-function browser-compat: css.types.image.gradient.radial-gradient --- {{CSSRef}} The **`radial-gradient()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) creates an image consisting of a progressive transition between two or more colors that radiate from an origin. Its shape may be a circle or an ellipse. The function's result is an object of the {{cssxref("&lt;gradient&gt;")}} data type, which is a special kind of {{cssxref("&lt;image&gt;")}}. {{EmbedInteractiveExample("pages/css/function-radial-gradient.html")}} ## Syntax ```css /* A gradient at the center of its container, starting red, changing to blue, and finishing green */ radial-gradient(circle at center, red 0, blue, green 100%) /* hsl color space with longer hue interpolation */ radial-gradient(circle at center in hsl longer hue, red 0, blue, green 100%) ``` A radial gradient is specified by indicating the center of the gradient (where the 0% ellipse will be) and the size and shape of the _ending shape_ (the 100% ellipse). ### Values - {{cssxref("&lt;position&gt;")}} - : The position of the gradient, interpreted in the same way as {{cssxref("background-position")}} or {{cssxref("transform-origin")}}. If unspecified, it defaults to `center`. - `<ending-shape>` - : The gradient's ending-shape. The value can be `circle` (meaning that the gradient's shape is a circle with a constant radius) or `ellipse` (meaning that the shape is an axis-aligned ellipse). If unspecified, it defaults to `ellipse`. - `<size>` - : Determines the size of the gradient's ending shape. If omitted it defaults to farthest-corner. It can be given explicitly or by keyword. For the purpose of the keyword definitions, consider the gradient box edges as extending infinitely in both directions, rather than being finite line segments. Both circle and ellipse gradients accept the following keywords for their `<size>`: | Keyword | Description | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `closest-side` | The gradient's ending shape meets the side of the box closest to its center (for circles) or meets both the vertical and horizontal sides closest to the center (for ellipses). | | `closest-corner` | The gradient's ending shape is sized so that it exactly meets the closest corner of the box from its center. | | `farthest-side` | Similar to `closest-side`, except the ending shape is sized to meet the side of the box farthest from its center (or vertical and horizontal sides). | | `farthest-corner` | The default value, the gradient's ending shape is sized so that it exactly meets the farthest corner of the box from its center. | If `<ending-shape>` is specified as `circle`, the size may be given explicitly as a {{cssxref("length")}}, which provides an explicit circle radius. Negative values are invalid. If `<ending-shape>` is specified as `ellipse`, the size may be given as a {{cssxref("length-percentage")}} with two values to provide an explicit ellipse size. The first value represents the horizontal radius and the second is the vertical radius. Percentage values are relative to the corresponding dimension of the gradient box. Negative values are invalid. When the `<ending-shape>` keyword is omitted, the gradient shape is determined by the size given. One `<length>` value provides a circle, while two values in `<length-percentage>`units provide an ellipse. A single `<percentage>` value is not valid. - `<linear-color-stop>` - : A color-stop's {{cssxref("&lt;color&gt;")}} value, followed by one or two optional stop positions (either a {{cssxref("&lt;percentage&gt;")}} or a {{cssxref("&lt;length&gt;")}} along the gradient's axis). A percentage of `0%`, or a length of `0`, represents the center of the gradient; the value `100%` represents the intersection of the ending shape with the virtual gradient ray. Percentage values in between are linearly positioned on the gradient ray. Including two stop positions is equivalent to declaring two color stops with the same color at the two positions. - `<color-hint>` - : The color-hint is an interpolation hint defining how the gradient progresses between adjacent color stops. The length defines at which point between two color stops the gradient color should reach the midpoint of the color transition. If omitted, the midpoint of the color transition is the midpoint between two color stops. ## Description As with any gradient, a radial gradient has [no intrinsic dimensions](/en-US/docs/Web/CSS/image#description); i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete size will match the size of the element it applies to. To create a radial gradient that repeats so as to fill its container, use the {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}} function instead. Because `<gradient>`s belong to the `<image>` data type, they can only be used where `<image>`s can be used. For this reason, `radial-gradient()` won't work on {{Cssxref("background-color")}} and other properties that use the {{cssxref("&lt;color&gt;")}} data type. ### Composition of a radial gradient ![Graph explaining radial gradients: the virtual radiant ray is horizontal starting from the midpoint. The elliptical gradient, and therefore the ending shape, has the same aspect ratio as the box upon which it is declared.](radial_gradient.png) A radial gradient is defined by a _center point_, an _ending shape_, and two or more _color-stop points_. To create a smooth gradient, the `radial-gradient()` function draws a series of concentric shapes radiating out from the center to the _ending shape_ (and potentially beyond). The ending shape may be either a circle or an ellipse. Color-stop points are positioned on a _virtual gradient ray_ that extends horizontally from the center towards the right. Percentage-based color-stop positions are relative to the intersection between the ending shape and this gradient ray, which represents `100%`. Each shape is a single color determined by the color on the gradient ray it intersects. ## Formal syntax {{csssyntax}} ## Examples ### Simple gradient ```html hidden <div class="radial-gradient"></div> ``` ```css hidden .radial-gradient { width: 240px; height: 120px; } ``` ```css .radial-gradient { background-image: radial-gradient(cyan 0%, transparent 20%, salmon 40%); } ``` {{EmbedLiveSample('Simple_gradient', 120, 120)}} ### Non-centered gradient ```html hidden <div class="radial-gradient"></div> ``` ```css hidden .radial-gradient { width: 240px; height: 120px; } ``` ```css .radial-gradient { background-image: radial-gradient( farthest-corner at 40px 40px, #f35 0%, #43e 100% ); } ``` {{EmbedLiveSample('Non-centered_gradient', 240, 120)}} ### Interpolating with hue ```html hidden <div class="shorter"></div> <div class="longer"></div> ``` ```css hidden div { display: inline-block; margin-top: 1rem; width: 45vw; height: 80vh; } .shorter::before { content: "shorter hue"; display: block; margin-top: -1rem; } .longer::before { content: "longer hue"; display: block; margin-top: -1rem; } ``` In this example for interpolation, [hsl](/en-US/docs/Web/CSS/color_value/hsl) color system is being used and [hue](/en-US/docs/Web/CSS/hue) is being interpolated. ```css .shorter { background-image: radial-gradient( circle at center in hsl shorter hue, red, blue ); } .longer { background-image: radial-gradient( circle at center in hsl longer hue, red, blue ); } ``` The box on the left uses [shorter interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#shorter), meaning the color goes straight from red to blue using the shorter arc on [color wheel](/en-US/docs/Glossary/Color_wheel). The box on the right uses [longer interpolation](/en-US/docs/Web/CSS/hue-interpolation-method#longer), meaning the color goes from red to blue using the longer arc, traversing through greens, yellows, and oranges. {{EmbedLiveSample("Interpolating with hue", 240, 200)}} ### More radial-gradient examples Please see [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) for more examples. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS gradients](/en-US/docs/Web/CSS/CSS_images/Using_CSS_gradients) - Other gradient functions: {{cssxref("gradient/repeating-radial-gradient", "repeating-radial-gradient()")}}, {{cssxref("gradient/linear-gradient", "linear-gradient()")}}, {{cssxref("gradient/repeating-linear-gradient", "repeating-linear-gradient()")}}, {{cssxref("gradient/conic-gradient", "conic-gradient()")}}, {{cssxref("gradient/repeating-conic-gradient", "repeating-conic-gradient()")}} - [`<hue-interpolation-method>`](/en-US/docs/Web/CSS/hue-interpolation-method) - [`<color-interpolation-method>`](/en-US/docs/Web/CSS/color-interpolation-method) - {{cssxref("&lt;image&gt;")}} - {{cssxref("image/image","image()")}} - {{cssxref("element", "element()")}} - {{cssxref("image/image-set","image-set()")}} - {{cssxref("cross-fade", "cross-fade()")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_colon_volume-locked/index.md
--- title: ":volume-locked" slug: Web/CSS/:volume-locked page-type: css-pseudo-class browser-compat: css.selectors.volume-locked --- {{CSSRef}} The **`:volume-locked`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) selector represents an element that is capable of making sound, such as {{htmlelement("audio")}} or {{htmlelement("video")}}, but the audio volume of the media element is currently "locked" by the user. User agents may set media [`muted`](/en-US/docs/Web/API/HTMLMediaElement/muted) or [`volume`](/en-US/docs/Web/API/HTMLMediaElement/volume) values according to user preferences (e.g., remembering the last set value across sessions, on a per-site basis, or otherwise). An element that is `:volume-locked` cannot be muted, un-muted, or have its volume changed via JavaScript. The locked status is an operating system or user agent preference. ## Syntax ```css :volume-locked { /* ... */ } ``` ## Examples ### CSS ```css :volume-locked { border: 5px solid green; } video:volume-locked { border: 5px solid aqua; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref(":buffering")}} - {{cssxref(":muted")}} - {{cssxref(":paused")}} - {{cssxref(":playing")}} - {{cssxref(":seeking")}} - {{cssxref(":stalled")}} - [CSS selectors](/en-US/docs/Web/CSS/CSS_selectors) - [`volume`](/en-US/docs/Web/API/HTMLMediaElement/volume) property of {{domxref("HTMLMediaElement")}} objects
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/mask-composite/index.md
--- title: mask-composite slug: Web/CSS/mask-composite page-type: css-property browser-compat: css.properties.mask-composite --- {{CSSRef}} The **`mask-composite`** [CSS](/en-US/docs/Web/CSS) property represents a compositing operation used on the current mask layer with the mask layers below it. ## Syntax ```css /* Keyword values */ mask-composite: add; mask-composite: subtract; mask-composite: intersect; mask-composite: exclude; /* Global values */ mask-composite: inherit; mask-composite: initial; mask-composite: revert; mask-composite: revert-layer; mask-composite: unset; ``` One or more of the keyword values listed below, separated by commas. ### Values For the composition the current mask layer is referred to as _source_, while all layers below it are referred to as _destination_. - `add` - : The source is placed over the destination. - `subtract` - : The source is placed, where it falls outside of the destination. - `intersect` - : The parts of source that overlap the destination, replace the destination. - `exclude` - : The non-overlapping regions of source and destination are combined. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Compositing mask layers with addition {{EmbedGHLiveSample("css-examples/masking/mask-composite.html", '100%', 550)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Clipping and Masking in CSS](https://css-tricks.com/clipping-masking-css/)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/scroll-padding-inline/index.md
--- title: scroll-padding-inline slug: Web/CSS/scroll-padding-inline page-type: css-shorthand-property browser-compat: css.properties.scroll-padding-inline --- {{CSSRef}} The `scroll-padding-inline` [shorthand property](/en-US/docs/Web/CSS/Shorthand_properties) sets the scroll padding of an element in the inline dimension. {{EmbedInteractiveExample("pages/css/scroll-padding-inline.html")}} The scroll-padding properties define offsets for the _optimal viewing region_ of the scrollport: the region used as the target region for placing things in view of the user. This allows the author to exclude regions of the scrollport that are obscured by other content (such as fixed-positioned toolbars or sidebars) or to put more breathing room between a targeted element and the edges of the scrollport. ## Constituent properties This property is a shorthand for the following CSS properties: - [`scroll-padding-inline-end`](/en-US/docs/Web/CSS/scroll-padding-inline-end) - [`scroll-padding-inline-start`](/en-US/docs/Web/CSS/scroll-padding-inline-start) ## Syntax ```css /* Keyword values */ scroll-padding-inline: auto; /* <length> values */ scroll-padding-inline: 10px; scroll-padding-inline: 1em 0.5em; scroll-padding-inline: 10%; /* Global values */ scroll-padding-inline: inherit; scroll-padding-inline: initial; scroll-padding-inline: revert; scroll-padding-inline: revert-layer; scroll-padding-inline: unset; ``` ### Values - `<length-percentage>` - : An inwards offset from the corresponding edge of the scrollport, as a valid length or a percentage. - `auto` - : The offset is determined by the user agent. This will generally be 0px, but a user agent is able to detect and do something else if a non-zero value is more appropriate. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS scroll snap](/en-US/docs/Web/CSS/CSS_scroll_snap) - [Well-controlled scrolling with CSS scroll snap](https://web.dev/articles/css-scroll-snap)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/-webkit-text-security/index.md
--- title: "-webkit-text-security" slug: Web/CSS/-webkit-text-security page-type: css-property status: - non-standard browser-compat: css.properties.-webkit-text-security --- {{CSSRef}}{{Non-standard_Header}} **`-webkit-text-security`** is a non-standard CSS property that obfuscates characters in a {{HtmlElement("form")}} field (such as {{HtmlElement("input")}} or {{HtmlElement("textarea")}}) by replacing them with a shape. It only affects fields that are _not_ of `type=password`. ## Syntax ```css -webkit-text-security: circle; -webkit-text-security: disc; -webkit-text-security: square; -webkit-text-security: none; /* Global values */ -webkit-text-security: inherit; -webkit-text-security: initial; -webkit-text-security: revert; -webkit-text-security: revert-layer; -webkit-text-security: unset; ``` ## Formal definition {{CSSInfo}} ## Formal syntax {{CSSSyntax}} ## Examples ### Obscuring a text input Try typing in the field below. If your browser supports this property, the characters should be visually replaced with squares. #### HTML ```html <label for="name">Name:</label> <input type="text" name="name" id="name" /> ``` #### CSS ```css input { -webkit-text-security: square; } ``` #### Result {{EmbedLiveSample("Obscuring_a_text_input")}} ## Specifications Not part of any standard. ## Browser compatibility {{Compat}} ## See also - [WebKit CSS extensions](/en-US/docs/Web/CSS/WebKit_Extensions)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/min-block-size/index.md
--- title: min-block-size slug: Web/CSS/min-block-size page-type: css-property browser-compat: css.properties.min-block-size --- {{CSSRef}} The **`min-block-size`** [CSS](/en-US/docs/Web/CSS) property defines the minimum horizontal or vertical size of an element's block, depending on its writing mode. It corresponds to either the {{cssxref("min-width")}} or the {{cssxref("min-height")}} property, depending on the value of {{cssxref("writing-mode")}}. If the writing mode is vertically oriented, the value of `min-block-size` relates to the minimum width of the element; otherwise, it relates to the minimum height of the element. A related property is {{cssxref("min-inline-size")}}, which defines the other dimension of the element. {{EmbedInteractiveExample("pages/css/min-block-size.html")}} ## Syntax ```css /* <length> values */ min-block-size: 100px; min-block-size: 5em; /* <percentage> values */ min-block-size: 10%; /* Keyword values */ min-block-size: max-content; min-block-size: min-content; min-block-size: fit-content; min-block-size: fit-content(20em); /* Global values */ min-block-size: inherit; min-block-size: initial; min-block-size: revert; min-block-size: revert-layer; min-block-size: unset; ``` ### Values The `min-block-size` property takes the same values as the {{cssxref("min-width")}} and {{cssxref("min-height")}} properties. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting minimum block size for vertical text #### HTML ```html <p class="exampleText">Example text</p> ``` #### CSS ```css .exampleText { writing-mode: vertical-rl; background-color: yellow; min-block-size: 200px; } ``` #### Result {{EmbedLiveSample("Setting_minimum_block_size_for_vertical_text")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - The mapped physical properties: {{cssxref("min-width")}} and {{cssxref("min-height")}} - {{cssxref("writing-mode")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/justify-tracks/index.md
--- title: justify-tracks slug: Web/CSS/justify-tracks page-type: css-property status: - experimental browser-compat: css.properties.justify-tracks --- {{CSSRef}}{{SeeCompatTable}} The **`justify-tracks`** CSS property sets the alignment in the masonry axis for grid containers that have [masonry](/en-US/docs/Web/CSS/CSS_grid_layout/Masonry_layout) in their inline axis. ## Syntax ```css /* Keyword values */ justify-tracks: start; justify-tracks: space-between; justify-tracks: center; justify-tracks: start, center, end; /* Global values */ justify-tracks: inherit; justify-tracks: initial; justify-tracks: revert; justify-tracks: revert-layer; justify-tracks: unset; ``` The property can take a single value, in which case all tracks are aligned in the same way. If a list of values is used then the first value applies to the first track in the grid axis, the second to the next, and so on. If there are fewer values than tracks, the last value is used for all remaining tracks. If there are more values than tracks, any additional values are ignored. ### Values - `start` - : The items are packed flush to each other toward the start edge of the alignment container in the masonry axis. - `end` - : The items are packed flush to each other toward the end edge of the alignment container in the masonry axis. - `center` - : The items are packed flush to each other toward the center of the alignment container along the masonry axis. - `normal` - : Acts as `start`. - `space-between` - : The items are evenly distributed within the alignment container along the masonry axis. The spacing between each pair of adjacent items is the same. The first item is flush with the main-start edge, and the last item is flush with the main-end edge. - `space-around` - : The items are evenly distributed within the alignment container along the masonry axis. The spacing between each pair of adjacent items is the same. The empty space before the first and after the last item equals half of the space between each pair of adjacent items. - `space-evenly` - : The items are evenly distributed within the alignment container along the masonry axis. The spacing between each pair of adjacent items, the main-start edge and the first item, and the main-end edge and the last item, are all exactly the same. - `stretch` - : The items stretch along the masonry axis to fill the content box. Items with definite size do not stretch. - `left` - : The items are packed flush to each other toward the left edge of the alignment container. - `right` - : The items are packed flush to each other toward the right edge of the alignment container. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Masonry layout with multiple values for justify-tracks {{EmbedGHLiveSample("css-examples/grid/masonry/justify-tracks.html", '100%', 1000)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - Related CSS properties: {{cssxref("align-tracks")}}, {{cssxref("masonry-auto-flow")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/text-decoration-color/index.md
--- title: text-decoration-color slug: Web/CSS/text-decoration-color page-type: css-property browser-compat: css.properties.text-decoration-color --- {{CSSRef}} The **`text-decoration-color`** [CSS](/en-US/docs/Web/CSS) property sets the color of decorations added to text by {{ cssxref("text-decoration-line") }}. The color applies to decorations, such as underlines, overlines, strikethroughs, and wavy lines like those used to mark misspellings, in the scope of the property's value. {{EmbedInteractiveExample("pages/css/text-decoration-color.html")}} CSS does not provide a direct mechanism for specifying a unique color for each line type. This effect can nevertheless be achieved by nesting elements, applying a different line type to each element (with the {{cssxref("text-decoration-line")}} property), and specifying the line color (with `text-decoration-color`) on a per-element basis. ## Syntax ```css /* <color> values */ text-decoration-color: currentcolor; text-decoration-color: red; text-decoration-color: #00ff00; text-decoration-color: rgb(255 128 128 / 50%); text-decoration-color: transparent; /* Global values */ text-decoration-color: inherit; text-decoration-color: initial; text-decoration-color: revert; text-decoration-color: revert-layer; text-decoration-color: unset; ``` ### Values - {{cssxref("&lt;color&gt;")}} - : The color of the line decoration. ## Accessibility concerns It is important to ensure that the contrast ratio between the color of the text, the background the text is placed over, and the text decoration line is high enough that people experiencing low vision conditions will be able to read the content of the page. Color contrast ratio is determined by comparing the luminosity of the text and background color values. Color alone should not be used to convey meaning. For example, change of text and text-decoration-color alone is not enough to indicate a link has focus. - [WebAIM: Color Contrast Checker](https://webaim.org/resources/contrastchecker/) - [MDN Understanding WCAG, Guideline 1.4 explanations](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable#guideline_1.4_make_it_easier_for_users_to_see_and_hear_content_including_separating_foreground_from_background) - [Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0](https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html) ## Formal definition {{CSSInfo}} ## Formal syntax {{csssyntax}} ## Examples ### Basic example ```html <p> This paragraph has <s>some erroneous text</s> inside it that I want to call attention to. </p> ``` ```css p { text-decoration-line: underline; text-decoration-color: cyan; } s { text-decoration-line: line-through; text-decoration-color: red; text-decoration-style: wavy; } ``` {{ EmbedLiveSample('Examples') }} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - When setting multiple line-decoration properties at once, it may be more convenient to use the {{cssxref("text-decoration")}} shorthand property instead. - The {{cssxref("&lt;color&gt;")}} data type - Other color-related properties: {{cssxref("background-color")}}, {{cssxref("border-color")}}, {{cssxref("outline-color")}}, {{cssxref("text-decoration-color")}}, {{cssxref("text-emphasis-color")}}, {{cssxref("text-shadow")}}, {{cssxref("caret-color")}}, and {{cssxref("column-rule-color")}} - [Applying color to HTML elements using CSS](/en-US/docs/Web/CSS/CSS_colors/Applying_color)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/scroll-margin-right/index.md
--- title: scroll-margin-right slug: Web/CSS/scroll-margin-right page-type: css-property browser-compat: css.properties.scroll-margin-right --- {{CSSRef}} The `scroll-margin-right` property defines the right margin of the scroll snap area that is used for snapping this box to the snapport. The scroll snap area is determined by taking the transformed border box, finding its rectangular bounding box (axis-aligned in the scroll container's coordinate space), then adding the specified outsets. {{EmbedInteractiveExample("pages/css/scroll-margin-right.html")}} ## Syntax ```css /* <length> values */ scroll-margin-right: 10px; scroll-margin-right: 1em; /* Global values */ scroll-margin-right: inherit; scroll-margin-right: initial; scroll-margin-right: revert; scroll-margin-right: revert-layer; scroll-margin-right: unset; ``` ### Values - {{CSSXref("&lt;length&gt;")}} - : An outset from the right edge of the scroll container. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS scroll snap](/en-US/docs/Web/CSS/CSS_scroll_snap) - [Well-controlled scrolling with CSS scroll snap](https://web.dev/articles/css-scroll-snap)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/css_grid_layout/index.md
--- title: CSS grid layout slug: Web/CSS/CSS_grid_layout page-type: css-module spec-urls: https://drafts.csswg.org/css-grid/ --- {{CSSRef}} The **CSS grid layout** module excels at dividing a page into major regions or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives. Like tables, grid layout enables an author to align elements into columns and rows. However, many more layouts are either possible or easier with CSS grid than they were with tables. For example, a grid container's child elements could position themselves so they actually overlap and layer, similar to CSS positioned elements. ## Basic example The example below shows a three-column track grid with new rows created at a minimum of 100 pixels and a maximum of auto. Items have been placed onto the grid using line-based placement. ```css hidden * { box-sizing: border-box; } .wrapper { max-width: 940px; margin: 0 auto; } .wrapper > div { border: 2px solid rgb(233 171 88); border-radius: 5px; background-color: rgb(233 171 88 / 50%); padding: 1em; color: #d9480f; } ``` ### HTML ```html <div class="wrapper"> <div class="one">One</div> <div class="two">Two</div> <div class="three">Three</div> <div class="four">Four</div> <div class="five">Five</div> <div class="six">Six</div> </div> ``` ### CSS ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; grid-auto-rows: minmax(100px, auto); } .one { grid-column: 1 / 3; grid-row: 1; } .two { grid-column: 2 / 4; grid-row: 1 / 3; } .three { grid-column: 1; grid-row: 2 / 5; } .four { grid-column: 3; grid-row: 3; } .five { grid-column: 2; grid-row: 4; } .six { grid-column: 3; grid-row: 4; } ``` {{EmbedLiveSample("Basic_example", "100%", "460")}} ## Reference ### Properties - {{CSSxRef("display")}} - {{CSSxRef("grid-template-columns")}} - {{CSSxRef("grid-template-rows")}} - {{CSSxRef("grid-template-areas")}} - {{CSSxRef("grid-template")}} - {{CSSxRef("grid-auto-columns")}} - {{CSSxRef("grid-auto-rows")}} - {{CSSxRef("grid-auto-flow")}} - {{CSSxRef("grid")}} - {{CSSxRef("grid-row-start")}} - {{CSSxRef("grid-column-start")}} - {{CSSxRef("grid-row-end")}} - {{CSSxRef("grid-column-end")}} - {{CSSxRef("grid-row")}} - {{CSSxRef("grid-column")}} - {{CSSxRef("grid-area")}} - {{CSSxRef("row-gap")}} - {{CSSxRef("column-gap")}} - {{CSSxRef("gap")}} - {{CSSxRef("masonry-auto-flow")}} {{Experimental_Inline}} - {{CSSxRef("align-tracks")}} {{Experimental_Inline}} - {{CSSxRef("justify-tracks")}} {{Experimental_Inline}} ### Functions - {{CSSxRef("repeat", "repeat()")}} - {{CSSxRef("minmax", "minmax()")}} - {{CSSxRef("fit-content_function", "fit-content()")}} ### Data types - {{CSSxRef("&lt;flex&gt;")}} ## Guides - [Basic concepts of grid layout](/en-US/docs/Web/CSS/CSS_grid_layout/Basic_concepts_of_grid_layout) - [Relationship of grid layout with other layout methods](/en-US/docs/Web/CSS/CSS_grid_layout/Relationship_of_grid_layout_with_other_layout_methods) - [Grid template areas](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_template_areas) - [Grid layout using line-based placement](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_using_line-based_placement) - [Grid layout using named grid lines](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_using_named_grid_lines) - [Auto-placement in grid layout](/en-US/docs/Web/CSS/CSS_grid_layout/Auto-placement_in_grid_layout) - [Box alignment in grid layout](/en-US/docs/Web/CSS/CSS_grid_layout/Box_alignment_in_grid_layout) - [Grids, logical values, and writing modes](/en-US/docs/Web/CSS/CSS_grid_layout/Grids_logical_values_and_writing_modes) - [Grid layout and accessibility](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_and_accessibility) - [Realizing common layouts using grids](/en-US/docs/Web/CSS/CSS_grid_layout/Realizing_common_layouts_using_grids) - [Subgrid](/en-US/docs/Web/CSS/CSS_grid_layout/Subgrid) - [Masonry layout](/en-US/docs/Web/CSS/CSS_grid_layout/Masonry_layout) {{Experimental_Inline}} ## Specifications {{Specifications}} ## See also - Glossary terms: - [Grid](/en-US/docs/Glossary/Grid) - [Grid lines](/en-US/docs/Glossary/Grid_Lines) - [Grid tracks](/en-US/docs/Glossary/Grid_Tracks) - [Grid cell](/en-US/docs/Glossary/Grid_Cell) - [Grid area](/en-US/docs/Glossary/Grid_Areas) - [Gutters](/en-US/docs/Glossary/Gutters) - [Grid axis](/en-US/docs/Glossary/Grid_Axis) - [Grid row](/en-US/docs/Glossary/Grid_Row) - [Grid column](/en-US/docs/Glossary/Grid_Column) - [CSS flexible box layout](/en-US/docs/Web/CSS/CSS_flexible_box_layout) module - [CSS display](/en-US/docs/Web/CSS/CSS_display) module - [Grid by Example](https://gridbyexample.com/) - A collection of usage examples and video tutorials - [CSS Grid Reference - Codrops](https://tympanus.net/codrops/css_reference/grid/) - [CSS Grid Inspector - Firefox DevTools](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html) - [CSS Grid Playground](https://mozilladevelopers.github.io/playground/css-grid/) - [CSS Grid Garden](https://cssgridgarden.com) - A game for learning CSS grid
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/box_alignment_in_grid_layout/index.md
--- title: Box alignment in grid layout slug: Web/CSS/CSS_grid_layout/Box_alignment_in_grid_layout page-type: guide --- {{CSSRef}} CSS Grid Layout implements the specification [Box Alignment Level 3](https://drafts.csswg.org/css-align/) which is the same standard [flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout) uses for aligning items in its flex container. This specification details how alignment should work in all the different layout methods. Layout methods will conform to the specification where possible and implement individual behavior based on their differences (features and constraints). While the specification currently specifies alignment details for all layout methods, browsers have not fully implemented all of the specification; however, the CSS Grid Layout method has been widely adopted. This guide presents demonstrations of how box alignment in grid layout works. You will see many similarities in how these properties and values work in flexbox. Due to grid being two-dimensional and flexbox one-dimensional there are some small differences that you should watch out for. So we will start by looking at the two axes that we deal with when aligning things in a grid. ## The two axes of a grid layout When working with grid layout you have two axes available to align things against – the _block axis_ and the _inline axis_. The block axis is the axis upon which blocks are laid out in block layout. If you have two paragraphs on your page they display one below the other, so it is this direction we describe as the block axis. ![Block axes are vertical.](block_axis.png) The _inline axis_ runs across the block axis, it is the direction in which text in regular inline flow runs. ![Inline / row axis are horizontal.](7_inline_axis.png) We are able to align the content inside grid areas, and the grid tracks themselves on these two axes. ## Aligning items on the Block Axis The {{cssxref("align-self")}} and {{cssxref("align-items")}} properties control alignment on the block axis. When we use these properties, we are changing the alignment of the item within the grid area you have placed it. ### Using align-items In the following example, I have four grid areas within my grid. I can use the {{cssxref("align-items")}} property on the grid container, to align the items using one of the following values: - `auto` - `normal` - `start` - `end` - `center` - `stretch` - `baseline` - `first baseline` - `last baseline` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(8, 1fr); gap: 10px; grid-auto-rows: 100px; grid-template-areas: "a a a a b b b b" "a a a a b b b b" "c c c c d d d d" "c c c c d d d d"; align-items: start; } .item1 { grid-area: a; } .item2 { grid-area: b; } .item3 { grid-area: c; } .item4 { grid-area: d; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Using_align-items', '500', '500') }} Keep in mind that once you set `align-items: start`, the height of each child `<div>` will be determined by the contents of the `<div>`. This is in contrast to omitting [`align-items`](/en-US/docs/Web/CSS/align-items) completely, in which case the height of each `<div>` stretches to fill its grid area. The {{cssxref("align-items")}} property sets the {{cssxref("align-self")}} property for all of the child grid items. This means that you can set the property individually, by using `align-self` on a grid item. ### Using align-self In this next example, I am using the `align-self` property, to demonstrate the different alignment values. The first area, is showing the default behavior of `align-self`, which is to stretch. The second item, has an `align-self` value of `start`, the third `end` and the fourth `center`. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(8, 1fr); gap: 10px; grid-auto-rows: 100px; grid-template-areas: "a a a a b b b b" "a a a a b b b b" "c c c c d d d d" "c c c c d d d d"; } .item1 { grid-area: a; } .item2 { grid-area: b; align-self: start; } .item3 { grid-area: c; align-self: end; } .item4 { grid-area: d; align-self: center; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Using_align-self', '500', '500') }} ### Items with an intrinsic aspect ratio The specification details that the default behavior in {{cssxref("align-self")}} is to stretch, except for items which have an intrinsic aspect ratio, in this case they behave as `start`. The reason for this, is that if items with an aspect ratio are set to stretch, this default would distort them. This behavior has now been clarified in the specification, with browsers yet to implement the correct behavior. Until that happens, you can ensure that items do not stretch, such as images, which are direct children of the grid, by setting {{cssxref("align-self")}} and {{cssxref("justify-self")}} to start. This will mimic the correct behavior once implemented. ## Justifying Items on the Inline Axis As {{cssxref("align-items")}} and {{cssxref("align-self")}} deal with the alignment of items on the block axis, {{cssxref("justify-items")}} and {{cssxref("justify-self")}} do the same job on the inline axis. The values you can choose from are the same as for `align-self`. - `auto` - `normal` - `start` - `end` - `center` - `stretch` - `baseline` - `first baseline` - `last baseline` You can see the same example as used for {{cssxref("align-items")}}, below. This time we are applying the {{cssxref("justify-self")}} property. Once again the default is `stretch`, other than for items with an intrinsic aspect ratio. This means that by default, grid items will cover their grid area, unless you change that by setting alignment. The first item in the example demonstrates this default alignment: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(8, 1fr); gap: 10px; grid-auto-rows: 100px; grid-template-areas: "a a a a b b b b" "a a a a b b b b" "c c c c d d d d" "c c c c d d d d"; } .item1 { grid-area: a; } .item2 { grid-area: b; justify-self: start; } .item3 { grid-area: c; justify-self: end; } .item4 { grid-area: d; justify-self: center; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Justifying_Items_on_the_Inline_Axis', '500', '500') }} As with {{cssxref("align-self")}} and {{cssxref("align-items")}}, you can apply {{cssxref("justify-items")}} to the grid container, to set the {{cssxref("justify-self")}} value for all items. The {{cssxref("justify-self")}} and {{cssxref("justify-items")}} properties are not implemented in flexbox. This is due to the one-dimensional nature of [flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout), and that there may be multiple items along the axis, making it impossible to justify a single item. To align items along the main, inline axis in flexbox you use the {{cssxref("justify-content")}} property. ### Shorthand properties The {{CSSxRef("place-items")}} property is shorthand for {{CSSxRef("align-items")}} and {{CSSxRef("justify-items")}}. The {{CSSxRef("place-self")}} property is shorthand for {{CSSxRef("align-self")}} and {{CSSxRef("justify-self")}}. ## Center an item in the area By combining the align and justify properties we can easily center an item inside a grid area. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; grid-auto-rows: 200px; grid-template-areas: ". a a ." ". a a ."; } .item1 { grid-area: a; align-self: center; justify-self: center; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> </div> ``` {{ EmbedLiveSample('Center_an_item_in_the_area', '500', '500') }} ## Aligning the grid tracks on the block axis If you have a situation where your grid tracks use an area that is smaller than the grid container, then you can align the grid tracks themselves, inside that container. Once again, this operates on the block and inline axes, with {{cssxref("align-content")}} aligning tracks on the block axis, and {{cssxref("justify-content")}} performing alignment on the inline axis. The {{CSSxRef("place-content")}} property is shorthand for {{cssxref("align-content")}} and {{cssxref("justify-content")}}. The values for {{cssxref("align-content")}}, {{cssxref("justify-content")}} and {{cssxref("place-content")}} are: - `normal` - `start` - `end` - `center` - `stretch` - `space-around` - `space-between` - `space-evenly` - `baseline` - `first baseline` - `last baseline` In the below example I have a grid container of 500 pixels by 500 pixels. I have defined 3 row and column tracks each of 100 pixels with a 10 pixel gutter. This means that there is space inside the grid container both in the block and inline directions. The `align-content` property is applied to the grid container as it works on the entire grid. ### Default alignment The default behavior in grid layout is `start`, which is why our grid tracks are in the top left corner of the grid, aligned against the start grid lines: ```css * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); height: 500px; width: 500px; gap: 10px; grid-template-areas: "a a b" "a a b" "c d d"; } .item1 { grid-area: a; } .item2 { grid-area: b; } .item3 { grid-area: c; } .item4 { grid-area: d; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Default_alignment', '500', '550') }} ### Setting align-content: end If I add `align-content` to my container, with a value of `end`, the tracks all move to the end line of the grid container in the block dimension: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); height: 500px; width: 500px; gap: 10px; grid-template-areas: "a a b" "a a b" "c d d"; align-content: end; } .item1 { grid-area: a; } .item2 { grid-area: b; } .item3 { grid-area: c; } .item4 { grid-area: d; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Setting_align-content_end', '500', '550') }} ### Setting align-content: space-between We can also use values for this property that you may be familiar with from flexbox; the space distribution values of `space-between`, `space-around` and `space-evenly`. If we update {{cssxref("align-content")}} to `space-between`, you can see how the elements on our grid space out: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); height: 500px; width: 500px; gap: 10px; grid-template-areas: "a a b" "a a b" "c d d"; align-content: space-between; } .item1 { grid-area: a; } .item2 { grid-area: b; } .item3 { grid-area: c; } .item4 { grid-area: d; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Setting_align-content_space-between', '500', '600') }} It is worth noting, that using these space distribution values may cause items on your grid to become larger. If an item spans more than one grid track, as further space is added between the tracks, that item needs to become large to absorb the space. We're always working in a strict grid. Therefore, if you decide to use these values, ensure that the content of your tracks can cope with the extra space, or that you have used alignment properties on the items, to cause them to move to the start rather than stretch. In the below image I have placed the grid with `align-content`, with a value of `start` alongside the grid when `align-content` has a value of `space-between`. You can see how items 1 and 2, which span two row tracks have taken on extra height as they gain the additional space added to the gap between those two tracks: ![Demonstrating how items become larger if we use space-between.](7_space-between.png) ## Justifying the grid tracks on the inline axis On the inline axis, we can use {{cssxref("justify-content")}} to perform the same type of alignment that we used {{cssxref("align-content")}} for in the block axis. Using the same example, I am setting {{cssxref("justify-content")}} to `space-around`. This once again causes tracks which span more than one column track to gain extra space: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); height: 500px; width: 500px; gap: 10px; grid-template-areas: "a a b" "a a b" "c d d"; align-content: space-between; justify-content: space-around; } .item1 { grid-area: a; } .item2 { grid-area: b; } .item3 { grid-area: c; } .item4 { grid-area: d; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Justifying_the_grid_tracks_on_the_inline_axis', '500', '550') }} ## Alignment and auto margins Another way to align items inside their area is to use auto margins. If you have ever centered your layout in the viewport, by setting the right and left margin of the container block to `auto`, you know that an auto margin absorbs all of the available space. By setting the margin to `auto` on both sides, it pushes the block into the middle as both margins attempt to take all of the space. In this next example, I have given item 1 a left margin of `auto`. You can see how the content is now pushed over to the right side of the area, as the auto margin takes up remaining space, after room for the content of that item has been assigned: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); height: 500px; width: 500px; gap: 10px; grid-template-areas: "a a b" "a a b" "c d d"; } .item1 { grid-area: a; margin-left: auto; } .item2 { grid-area: b; } .item3 { grid-area: c; } .item4 { grid-area: d; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> </div> ``` {{ EmbedLiveSample('Alignment_and_auto_margins', '500', '550') }} You can see how the item is aligned by using the [Firefox Grid Highlighter](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html): ![Image showing auto-margins using the Grid Highlighter.](7_auto_margins.png) ## Alignment and Writing Modes In all of these examples I have been working in English, which is a left-to-right language. This means that our start lines are top and left of our grid when thinking in physical directions. CSS Grid Layout, and the Box Alignment specification are designed to work with writing modes in CSS. This means that if you are working in a right to left language, such as Arabic, the start of the grid would be the top and right, so the default of `justify-content: start` would be for grid tracks to start on the right-hand side of the grid. Setting auto margins, using `margin-right` or `margin-left` however, or absolutely positioning items using the `top`, `right`, `bottom` and `left` offsets would not honor writing modes. In the next guide, we will look further into this interaction between CSS grid layout, box alignment and writing modes. This will be important to understand, if you develop sites that are then displayed in multiple languages, or if you want to mix languages or writing modes in a design.
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/grid_template_areas/index.md
--- title: Grid template areas slug: Web/CSS/CSS_grid_layout/Grid_template_areas page-type: guide --- {{CSSRef}} In the [previous guide](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_using_line-based_placement), we looked at grid lines and how to position items against those lines. When you use CSS grid layout, you always have lines, and this can be a straightforward way to place items on your grid. However, there is an alternate method to use for positioning items on the grid which you can use alone or in combination with line-based placement. This method involves placing our items using named template areas, and we will find out exactly how this method works. You will see very quickly why we sometimes call this the ascii-art method of grid layout! ## Naming a grid area You have already encountered the {{cssxref("grid-area")}} property. This is the property that can take as a value all four of the lines used to position a grid area. ```css .box1 { grid-area: 1 / 1 / 4 / 2; } ``` What we are doing here when defining all four lines, is defining the area by specifying the lines that enclose that area. ![The Grid Area defined by lines](4_area.png) We can also define an area by giving it a name and then specify the location of that area in the value of the {{cssxref("grid-template-areas")}} property. You can choose what you would like to name your area. For example, if I wish to create the layout shown below I can identify four main areas. - a header - a footer - a sidebar - the main content ![An image showing a simple two column layout with header and footer](4_layout.png) With the {{cssxref("grid-area")}} property I can assign each of these areas a name. This will not yet create any layout, but we now have named areas to use in a layout. ```css .header { grid-area: hd; } .footer { grid-area: ft; } .content { grid-area: main; } .sidebar { grid-area: sd; } ``` Having defined these names I then create my layout. This time, instead of placing my items using line numbers specified on the items themselves, I create the whole layout on the grid container. ```css .wrapper { display: grid; grid-template-columns: repeat(9, 1fr); grid-auto-rows: minmax(100px, auto); grid-template-areas: "hd hd hd hd hd hd hd hd hd" "sd sd sd main main main main main main" "ft ft ft ft ft ft ft ft ft"; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> </div> ``` {{ EmbedLiveSample('Naming_a_grid_area', '300', '330') }} Using this method we do not need to specify anything at all on the individual grid items, everything happens on our grid container. We can see the layout described as the value of the {{cssxref("grid-template-areas")}} property. ## Leaving a grid cell empty We have completely filled our grid with areas in this example, leaving no white space. However you can leave grid cells empty with this method of layout. To leave a cell empty use the full stop character, '`.`'. If I want to only display the footer directly under the main content I would need to leave the three cells underneath the sidebar empty. ```css .header { grid-area: hd; } .footer { grid-area: ft; } .content { grid-area: main; } .sidebar { grid-area: sd; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(9, 1fr); grid-auto-rows: minmax(100px, auto); grid-template-areas: "hd hd hd hd hd hd hd hd hd" "sd sd sd main main main main main main" ". . . ft ft ft ft ft ft"; } ``` ```html <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> </div> ``` {{ EmbedLiveSample('Leaving_a_grid_cell_empty', '300', '330') }} In order to make the layout neater I can use multiple `.` characters. As long as there is at least one white space between the full stops it will be counted as one cell. For a complex layout there is a benefit to having the rows and columns neatly aligned. It means that you can actually see, right there in the CSS, what this layout looks like. ## Spanning multiple cells In our example each of the areas spans multiple grid cells and we achieve this by repeating the name of that grid area multiple times with white space between. You can add extra white space in order to keep your columns neatly lined up in the value of `grid-template-areas`. You can see that I have done this in order that the `hd` and `ft` line up with `main`. The area that you create by chaining the area names must be rectangular, at this point there is no way to create an L-shaped area. The specification does note that a future level might provide this functionality. You can however span rows just as easily as columns. For example we could make our sidebar span down to the end of the footer by replacing the `.` with `sd`. ```css .header { grid-area: hd; } .footer { grid-area: ft; } .content { grid-area: main; } .sidebar { grid-area: sd; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(9, 1fr); grid-auto-rows: minmax(100px, auto); grid-template-areas: "hd hd hd hd hd hd hd hd hd" "sd sd sd main main main main main main" "sd sd sd ft ft ft ft ft ft"; } ``` ```html hidden <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> </div> ``` {{ EmbedLiveSample('Spanning_multiple_cells', '300', '330') }} The value of {{cssxref("grid-template-areas")}} must show a complete grid, otherwise it is invalid (and the property is ignored). This means that you must have the same number of cells for each row, if empty with a full stop character demonstrating that the cell is to be left empty. You will also create an invalid grid if your areas are not rectangular. ## Redefining the grid using media queries As our layout is now contained in one part of the CSS, this makes it very easy to make changes at different breakpoints. You can do this by redefining the grid, the position of items on the grid, or both at once. When doing this, define the names for your areas outside of any media queries. That way the content area would always be called `main` no matter where on the grid it is placed. For our layout above, we might like to have a very simple layout at narrow widths, defining a single column grid and stacking up our items. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .header { grid-area: hd; } .footer { grid-area: ft; } .content { grid-area: main; } .sidebar { grid-area: sd; } .wrapper { display: grid; grid-auto-rows: minmax(100px, auto); grid-template-columns: 1fr; grid-template-areas: "hd" "main" "sd" "ft"; } ``` We can then redefine that layout inside media queries to go to our two columns layout, and perhaps take it to a three column layout if the available space is even wider. Note that for the wide layout I keep my nine column track grid, I redefine where items are placed using `grid-template-areas`. ```css @media (min-width: 500px) { .wrapper { grid-template-columns: repeat(9, 1fr); grid-template-areas: "hd hd hd hd hd hd hd hd hd" "sd sd sd main main main main main main" "sd sd sd ft ft ft ft ft ft"; } } @media (min-width: 700px) { .wrapper { grid-template-areas: "hd hd hd hd hd hd hd hd hd" "sd sd main main main main main ft ft"; } } ``` ```html hidden <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> </div> ``` {{ EmbedLiveSample('Redefining_the_grid_using_media_queries', '550', '330') }} ## Using `grid-template-areas` for UI elements Many of the grid examples you will find online make the assumption that you will use grid for main page layout, however grid can be just as useful for small elements as those larger ones. Using {{cssxref("grid-template-areas")}} can be especially nice as it is easy to see in the code what your element looks like. ### Media object example As a very simple example we can create a "media object". This is a component with space for an image or other media on one side and content on the other. The image might be displayed on the right or left of the box. ![Images showing an example media object design](4_media_objects.png) Our grid is a two-column track grid, with the column for the image sized at `1fr` and the text `3fr`. If you wanted a fixed width image area, then you could set the image column as a pixel width, and assign the text area `1fr`. A single column track of `1fr` would then take up the rest of the space. We give the image area a grid area name of `img` and the text area `content`, then we can lay those out using the `grid-template-areas` property. ```css * { box-sizing: border-box; } .media { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 400px; display: grid; grid-template-columns: 1fr 3fr; grid-template-areas: "img content"; margin-bottom: 1em; } .media .image { grid-area: img; background-color: #ffd8a8; } .media .text { grid-area: content; padding: 10px; } ``` ```html <div class="media"> <div class="image"></div> <div class="text"> This is a media object example. We can use grid-template-areas to switch around the image and text part of the media object. </div> </div> ``` {{ EmbedLiveSample('Media_object_example', '300', '200') }} ### Displaying the image on the other side of the box We might want to be able to display our box with the image the other way around. To do this, we redefine the grid to put the `1fr` track last, and flip the values in {{cssxref("grid-template-areas")}}. ```css * { box-sizing: border-box; } .media { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 400px; display: grid; grid-template-columns: 1fr 3fr; grid-template-areas: "img content"; margin-bottom: 1em; } .media.flipped { grid-template-columns: 3fr 1fr; grid-template-areas: "content img"; } .media .image { grid-area: img; background-color: #ffd8a8; } .media .text { grid-area: content; padding: 10px; } ``` ```html <div class="media flipped"> <div class="image"></div> <div class="text"> This is a media object example. We can use grid-template-areas to switch around the image and text part of the media object. </div> </div> ``` {{ EmbedLiveSample('Displaying_the_image_on_the_other_side_of_the_box', '300', '200') }} ## Grid definition shorthands Having looked at various ways of placing items on our grids and many of the properties used to define grid, this is a good time to take a look at a couple of shorthands that are available for defining the grid and many things about it all in one line of CSS. These can quickly become difficult to read for other developers, or even your future self. However they are part of the specification and it is likely you will come across them in examples or in use by other developers, even if you choose not to use them. Before using any shorthand it is worth remembering that shorthands not only enable the setting of many properties in one go, they also act to **reset things** to their initial values that you do not, or cannot set in the shorthand. Therefore if you use a shorthand, be aware that it may reset things you have applied elsewhere. The two shorthands for the grid container are the Explicit Grid Shorthand `grid-template` and the Grid Definition Shorthand `grid`. ### `grid-template` The {{cssxref("grid-template")}} property sets the following properties: - {{cssxref("grid-template-rows")}} - {{cssxref("grid-template-columns")}} - {{cssxref("grid-template-areas")}} The property is referred to as the Explicit Grid Shorthand because it is setting those things that you control when you define an explicit grid, and not those which impact any implicit row or column tracks that might be created. The following code creates a layout using {{cssxref("grid-template")}} that is the same as the layout created earlier in this guide. ```css .wrapper { display: grid; grid-template: "hd hd hd hd hd hd hd hd hd" minmax(100px, auto) "sd sd sd main main main main main main" minmax(100px, auto) "ft ft ft ft ft ft ft ft ft" minmax(100px, auto) / 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr; } ``` The first value is our `grid-template-areas` value but we also declare the size of the row at the end of each row. This is what the `minmax(100px, auto)` is doing. Then after `grid-template-areas` we have a forward slash, after that is an explicit track listing of column tracks. ### `grid` The {{cssxref("grid")}} shorthand goes a step further and also sets properties used by the implicit grid. So you will be setting: - {{cssxref("grid-template-rows")}} - {{cssxref("grid-template-columns")}} - {{cssxref("grid-template-areas")}} - {{cssxref("grid-auto-rows")}} - {{cssxref("grid-auto-columns")}} - {{cssxref("grid-auto-flow")}} You can use this syntax in the exact same way as the {{cssxref("grid-template")}} shorthand, just be aware than when doing so you will reset the other values set by the property. ```css .wrapper { display: grid; grid: "hd hd hd hd hd hd hd hd hd" minmax(100px, auto) "sd sd sd main main main main main main" minmax(100px, auto) "ft ft ft ft ft ft ft ft ft" minmax(100px, auto) / 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr; } ``` We will revisit the other functionality offered by this shorthand later in these guides when we take a look at auto placement and the grid-auto-flow property. If you have worked through these initial guides you now should be in a position to create grid layouts using line-based placement or named areas. Take some time to build some common layout patterns using grid, while there are lots of new terms to learn, the syntax is relatively straightforward. As you develop examples, you are likely to come up with some questions and use cases for things we haven't covered yet. In the rest of these guides we will be looking at some more of the detail included in the specification – in order that you can begin to create advanced layouts with it.
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/masonry_layout/index.md
--- title: Masonry layout slug: Web/CSS/CSS_grid_layout/Masonry_layout page-type: guide status: - experimental browser-compat: - css.properties.grid-template-columns.masonry - css.properties.grid-template-rows.masonry --- {{CSSRef}} {{SeeCompatTable}} Level 3 of the [CSS grid layout](/en-US/docs/Web/CSS/CSS_grid_layout) specification includes a `masonry` value for {{cssxref("grid-template-columns")}} and {{cssxref("grid-template-rows")}}. This guide details what masonry layout is and how to use it. Masonry layout is a layout method where one axis uses a typical strict grid layout, most often columns, and the other a masonry layout. On the masonry axis, rather than sticking to a strict grid with gaps being left after shorter items, the items in the following row rise up to completely fill the gaps. ## Creating a masonry layout To create the most common masonry layout, your columns will be the grid axis and the rows the masonry axis. Define this layout with `grid-template-columns` and `grid-template-rows`: ```css .container { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); grid-template-rows: masonry; } ``` The child elements of this container will now lay out item by item along the rows, as they would with regular grid layout automatic placement. However, as they move onto a new row the items will display according to the masonry algorithm. Items will load into the column with the most room causing a tightly packed layout without strict row tracks. {{EmbedGHLiveSample("css-examples/grid/masonry/block-axis.html", '100%', 800)}} It is also possible to create a masonry layout with items loading into rows. {{EmbedGHLiveSample("css-examples/grid/masonry/inline-axis.html", '100%', 1000)}} ## Controlling the grid axis On the grid axis, things will work just as you expect them to in grid layout. You can cause items to span multiple tracks while remaining in auto-placement, using the `span` keyword. Items may also be positioned using line-based positioning. ### Masonry layout with spanning items In this example two of the items span two tracks, and the masonry items work around them. {{EmbedGHLiveSample("css-examples/grid/masonry/spanners.html", '100%', 800)}} This example includes an item which has positioning for columns. Items with definite placement are placed before the masonry layout happens. {{EmbedGHLiveSample("css-examples/grid/masonry/positioned.html", '100%', 1000)}} ## Controlling the masonry axis The masonry axis operates under different rules as it is following the masonry layout rules rather than normal grid auto-placement rules. To control this axis we have three additional properties defined in the Grid Level 3 specification {{cssxref("align-tracks")}}, {{cssxref("justify-tracks")}}, and {{cssxref("masonry-auto-flow")}}. ### masonry-auto-flow The `masonry-auto-flow` property gives you a way to change how the masonry algorithm behaves. Give it a value of `next` and items will display in order on the grid axis, rather than going into whichever track has the most free space. The value `positioned` will ignore items with definite placement and place items in order-modified document order. {{EmbedGHLiveSample("css-examples/grid/masonry/masonry-auto-flow.html", '100%', 1000)}} ### align-tracks The `align-tracks` property allows for the alignment of items in grid containers with masonry in their block axis. The property aligns the items within their track, much in the way flex layout works. The property takes the same values as `align-content`, however you can specify multiple values to have different alignment values per track on the grid axis. If you specify more values than tracks the additional values are ignored. If there are more tracks than values any additional tracks will use the last specified value. {{EmbedGHLiveSample("css-examples/grid/masonry/align-tracks.html", '100%', 1000)}} ### justify-tracks The `justify-tracks` property works in the same way as align-tracks, however it is used when the masonry axis is the inline axis. {{EmbedGHLiveSample("css-examples/grid/masonry/justify-tracks.html", '100%', 1000)}} ## Fallback In browsers [that do not support masonry](#browser_compatibility), regular grid auto-placement will be used instead. ## Browser compatibility {{Compat}} ## See also - [Native CSS Masonry Layout In CSS Grid](https://www.smashingmagazine.com/native-css-masonry-layout-css-grid/)
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/auto-placement_in_grid_layout/index.md
--- title: Auto-placement in grid layout slug: Web/CSS/CSS_grid_layout/Auto-placement_in_grid_layout page-type: guide --- {{CSSRef}} In addition to the ability to place items accurately onto a created grid, the CSS Grid Layout specification contains rules that control what happens when you create a grid and do not place some or all of the child items. You can see auto-placement in action in the simplest of ways by creating a grid on a set of items. ## Default placement If you give the items no placement information they will position themselves on the grid, one in each grid cell. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` {{ EmbedLiveSample('Default_placement', '500', '230') }} ## Default rules for auto-placement As you can see with the above example, if you create a grid all child items will lay themselves out one into each grid cell. The default flow is to arrange items by row. Grid will lay an item out into each cell of row 1. If you have created additional rows using the `grid-template-rows` property then grid will continue placing items in these rows. If the grid does not have enough rows in the explicit grid to place all of the items new _implicit_ rows will be created. ### Sizing rows in the implicit grid The default for automatically created rows in the implicit grid is for them to be auto-sized. This means that they will contain the content added to them without causing an overflow. You can however control the size of these rows with the property `grid-auto-rows`. To cause all created rows to be 100 pixels tall for example you would use: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; grid-auto-rows: 100px; } ``` {{ EmbedLiveSample('Sizing_rows_in_the_implicit_grid', '500', '330') }} ### Sizing rows using minmax() You can use {{cssxref("minmax","minmax()")}} in your value for {{cssxref("grid-auto-rows")}} enabling the creation of rows that are a minimum size but then grow to fit content if it is taller. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div> Four <br />This cell <br />Has extra <br />content. <br />Max is auto <br />so the row expands. </div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; grid-auto-rows: minmax(100px, auto); } ``` {{ EmbedLiveSample('Sizing_rows_using_minmax', '500', '330') }} ### Sizing rows using a track listing You can also pass in a track listing, this will repeat. The following track listing will create an initial implicit row track as 100 pixels and a second as `200px`. This will continue for as long as content is added to the implicit grid. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> <div>Six</div> <div>Seven</div> <div>Eight</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; grid-auto-rows: 100px 200px; } ``` {{ EmbedLiveSample('Sizing_rows_using_a_track_listing', '500', '450') }} ### Auto-placement by column You can also ask grid to auto-place items by column. Using the property {{cssxref("grid-auto-flow")}} with a value of `column`. In this case grid will add items in rows that you have defined using {{cssxref("grid-template-rows")}}. When it fills up a column it will move onto the next explicit column, or create a new column track in the implicit grid. As with implicit row tracks, these column tracks will be auto sized. You can control the size of implicit column tracks with {{cssxref("grid-auto-columns")}}, this works in the same way as {{cssxref("grid-auto-rows")}}. In this next example I have created a grid with three row tracks of 200 pixels height. I am auto-placing by column and the columns created will be a column width of 300 pixels, then a column width of 100 pixels until there are enough column tracks to hold all of the items. ```css .wrapper { display: grid; grid-template-rows: repeat(3, 200px); gap: 10px; grid-auto-flow: column; grid-auto-columns: 300px 100px; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> <div>Six</div> <div>Seven</div> <div>Eight</div> </div> ``` {{ EmbedLiveSample('Auto-placement_by_column', '500', '700') }} ## The order of auto placed items A grid can contain a mixture of items. Some of the items may have a position on the grid, but others may be auto-placed. This can be helpful, if you have a document order that reflects the order in which items sit on the grid you may not need to write CSS rules to place absolutely everything. The specification contains a long section detailing the [Grid item placement algorithm](https://drafts.csswg.org/css-grid/#auto-placement-algo), however for most of us we just need to remember a few simple rules for our items. ### Order modified document order Grid places items that have not been given a grid position in what is described in the specification as "order modified document order". This means that if you have used the `order` property at all, the items will be placed by that order, not their DOM order. Otherwise they will stay by default in the order that they are entered in the document source. ### Items with placement properties The first thing grid will do is place any items that have a position. In the example below I have 12 grid items. Item 2 and item 5 have been placed using line based placement on the grid. You can see how those items are placed and the other items then auto-place in the spaces. The auto-placed items will place themselves before the placed items in DOM order, they don't start after the position of a placed item that comes before them. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> <div>Six</div> <div>Seven</div> <div>Eight</div> <div>Nine</div> <div>Ten</div> <div>Eleven</div> <div>Twelve</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 100px; gap: 10px; } .wrapper div:nth-child(2) { grid-column: 3; grid-row: 2 / 4; } .wrapper div:nth-child(5) { grid-column: 1 / 3; grid-row: 1 / 3; } ``` {{ EmbedLiveSample('Items_with_placement_properties', '500', '500') }} ### Deal with items that span tracks You can use placement properties while still taking advantage of auto-placement. In this next example I have added to the layout by setting items 1, 5, and 9 (4n+1) to span two tracks both for rows and columns. I do this with the {{cssxref("grid-column-end")}} and {{cssxref("grid-row-end")}} properties and setting the value of this to `span 2`. What this means is that the start line of the item will be set by auto-placement, and the end line will span two tracks. You can see how this then leaves gaps in the grid, as for the auto-placed items if grid comes across an item that doesn't fit into a track, it will move to the next row until it finds a space the item can fit in. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> <div>Six</div> <div>Seven</div> <div>Eight</div> <div>Nine</div> <div>Ten</div> <div>Eleven</div> <div>Twelve</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 100px; gap: 10px; } .wrapper div:nth-child(4n + 1) { grid-column-end: span 2; grid-row-end: span 2; background-color: #ffa94d; } .wrapper div:nth-child(2) { grid-column: 3; grid-row: 2 / 4; } .wrapper div:nth-child(5) { grid-column: 1 / 3; grid-row: 1 / 3; } ``` {{ EmbedLiveSample('Deal_with_items_that_span_tracks', '500', '800') }} ### Filling in the gaps So far, other than items we have specifically placed, grid is always progressing forward and keeping items in DOM order. This is generally what you want, if you are laying out a form for example you wouldn't want the labels and fields to become jumbled up in order to fill in some gap. However sometimes, we are laying things out that don't have a logical order and we would like to create a layout that doesn't have gaps in it. To do this, add the property {{cssxref("grid-auto-flow")}} with a value of `dense` to the container. This is the same property you use to change the flow order to `column`, so if you were working in columns you would add both values `grid-auto-flow: column dense`. Having done this, grid will now backfill the gaps, as it moves through the grid it leaves gaps as before, but then if it finds an item that will fit in a previous gap it will pick it up and take it out of DOM order to place it in the gap. As with any other reordering in grid this does not change the logical order. Tab order for example, will still follow the document order. We will take a look at the potential accessibility issues of Grid Layout in the [Grid layout and accessibility guide](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_and_accessibility), but you should take care when creating this disconnect between the visual order and display order. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> <div>Six</div> <div>Seven</div> <div>Eight</div> <div>Nine</div> <div>Ten</div> <div>Eleven</div> <div>Twelve</div> </div> ``` ```css .wrapper div:nth-child(4n + 1) { grid-column-end: span 2; grid-row-end: span 2; background-color: #ffa94d; } .wrapper div:nth-child(2) { grid-column: 3; grid-row: 2 / 4; } .wrapper div:nth-child(5) { grid-column: 1 / 3; grid-row: 1 / 3; } .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 100px; gap: 10px; grid-auto-flow: dense; } ``` {{ EmbedLiveSample('Filling_in_the_gaps', '500', '730') }} ### Anonymous grid items There is a mention in the specification of anonymous grid items. These are created if you have a string of text inside your grid container, that is not wrapped in any other element. In the example below we have three grid items, assuming you had set the parent with a class of `grid` to `display: grid`. The first is an anonymous item as it has no enclosing markup, this item will always be dealt with via the auto-placement rules. The other two are grid items enclosed in a div, they might be auto-placed or you could place these with a positioning method onto your grid. ```html <div class="grid"> I am a string and will become an anonymous item <div>A grid item</div> <div>A grid item</div> </div> ``` Anonymous items are always auto-placed because there is no way to target them. Therefore if you have some unwrapped text for some reason in your grid, be aware that it might show up somewhere unexpected as it will be auto-placed according to the auto-placement rules. ### Use cases for auto-placement Auto-placement is useful whenever you have a collection of items. That could be items that do not have a logical order such as a gallery of photos, or product listing. In that case you might choose to use the dense packing mode to fill in any holes in your grid. In my image gallery example I have some landscape and some portrait images. I have set landscape images – with a class of `landscape` to span two column tracks. I then use `grid-auto-flow: dense` to create a densely packed grid. Try removing the line `grid-auto-flow: dense` to see the content reflow to leave gaps in the layout. {{EmbedGHLiveSample("css-examples/grid/docs/autoplacement.html", '100%', 1200)}} Auto-placement can also help you lay out interface items which do have logical order. An example is the definition list in this next example. Definition lists are an interesting challenge to style as they are flat, there is nothing wrapping the groups of `dt` and `dd` items. In my example I am allowing auto-placement to place the items, however I have classes that start a `dt` in column 1, and `dd` in column 2, this ensure that terms go on one side and definitions on the other - no matter how many of each we have. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } ``` ```html <div class="wrapper"> <dl> <dt>Mammals</dt> <dd>Cat</dd> <dd>Dog</dd> <dd>Mouse</dd> <dt>Fish</dt> <dd>Guppy</dd> <dt>Birds</dt> <dd>Pied Wagtail</dd> <dd>Owl</dd> </dl> </div> ``` ```css dl { display: grid; grid-template-columns: auto 1fr; max-width: 300px; margin: 1em; line-height: 1.4; } dt { grid-column: 1; font-weight: bold; } dd { grid-column: 2; } ``` {{ EmbedLiveSample('Use_cases_for_auto-placement', '500', '230') }} ## What can't we do with auto-placement (yet)? There are a couple of things that often come up as questions. Currently we can't do things like target every other cell of the grid with our items. A related issue may have already come to mind if you followed the last guide about named lines on the grid. It would be to define a rule that said "auto-place items against the next line named "n", and grid would then skip other lines. There is [an issue raised about this](https://github.com/w3c/csswg-drafts/issues/796) on the CSSWG GitHub repository, and you would be welcome to add your own use cases to this. It may be that you come up with your own use cases for auto-placement or any other part of grid layout. If you do, raise them as issues or add to an existing issue that could solve your use case. This will help to make future versions of the specification better.
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/grid_layout_and_accessibility/index.md
--- title: Grid layout and accessibility slug: Web/CSS/CSS_grid_layout/Grid_layout_and_accessibility page-type: guide --- {{CSSRef}} Those of us who have been doing web development for more years than we care to remember might consider that CSS Grid is a little bit like using "tables for layout". Back in the early days of web design, the way we constructed page layout was to use HTML tables, then fragment our design into the cells of those tables in order to create a layout. This had some advantages over the "CSS Positioning" that came afterwards, in that we could take advantage of the alignment and full height columns offered by table display. The biggest downside however was that it tied our design to the markup, often creating accessibility issues as it did so. In order to lay the design out in the table we often broke up the content in ways that made no sense at all when read out by a screen reader for example. In moving to CSS we often spoke about CSS for layout enabling a separation of content and markup and presentation. The ultimate aim being that we could create a semantic and well structured document, then apply CSS to create the layout we desired. Sites such as the [CSS Zen Garden](https://www.csszengarden.com/) showcased this ability. The CSS Zen Garden challenged us to take identical markup and create a unique design using CSS. [CSS grid layout](/en-US/docs/Web/CSS/CSS_grid_layout) does not have the same issues that tables did, our grid structure is defined in CSS rather than in the markup. If we need to add an element we can use something with no semantic meaning. On paper grid helps us properly fulfill that promise of content separated from markup, however is it possible to go too far with this idea? Is it possible that we could _create_ an accessibility issue through our use of grids? ## Re-ordering content in CSS Grid Layout We've already seen in these guides that grid gives us power to re-order the content of our page in various ways. We can use the {{cssxref("order")}} property, which will change how items auto-place. We can use `grid-auto-flow: dense` which will take items visually out of DOM order. We can also position items using line-based placement of grid template areas, without considering their location in the source. The [specification](https://drafts.csswg.org/css-grid/#order-accessibility) includes a section that covers Reordering and Accessibility. In the introduction to that section are details of what the specification expects browsers to do when the content is visually reordered using Grid Layout. > Grid layout gives authors great powers of rearrangement over the document. However, these are not a substitute for correct ordering of the document source. The order property and grid placement do not affect ordering in non-visual media (such as speech). Likewise, rearranging grid items visually does not affect the default traversal order of sequential navigation modes (such as cycling through links, see e.g. [tabindex](/en-US/docs/Web/HTML/Global_attributes/tabindex) HTML5). If you reorder things visually using grid layout, this will not change how the items are ordered if the content is being read out by a screen reader, or other text to speech user agent. In addition, the reordering will not change tab order. This means that someone navigating using the keyboard could be tabbing through links on your site and suddenly find themselves jumping from the top to the bottom of the document due to a reordered item being next in line. The specification warns authors (the CSSWG term for web developers) not to do this reordering. > Authors must use order and the grid-placement properties only for visual, not logical, reordering of content. Style sheets that use these features to perform logical reordering are non-conforming. What does this mean for designing with grid layout in practice? ### Visual not logical re-ordering Any time you reorder things with grid layout – or with flexbox – you only perform _visual reordering_. The underlying source is what controls things like text to speech, and the tab order of the document. You can see how this works with a very simple example. In this example I have used grid to lay out a set of boxes that contain links. I have used the line-based placement properties to position box 1 on the second row of the grid. Visually it now appears as the fourth item in the list. However, if I tab from link to link the tab order still begins with box 1, as it comes first in the source. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 100px; } .box1 { grid-column: 1; grid-row: 2; } ``` ```html <div class="wrapper"> <div class="box box1"><a href="">One</a></div> <div class="box box2"><a href="">Two</a></div> <div class="box box3"><a href="">Three</a></div> <div class="box box4"><a href="">Four</a></div> <div class="box box5"><a href="">Five</a></div> </div> ``` {{ EmbedLiveSample('Visual_not_logical_re-ordering', '500', '330') }} The specification says that in this scenario, if box 1 really makes sense logically in that position, we should go back to our source and make the change there rather than reordering using grid layout. This is what is meant by visual versus logical reordering, logical ordering is important for the meaning and structure of our document, and we should make sure that we preserve that structure. ## How should we approach accessibility for grid layout? From the specification we know that we need to ensure our document maintains the logical order of our content. How should we approach our development to make sure that we maintain accessibility for the different users and the ways that they interact with our pages? - Start with a structured and accessible document - : A grid layout should mean we do not need to change our document source in order to get the layout that we want. Therefore the starting point of your page should be a well structured and accessible source document. As is noted in the CSS Grid Layout specification, this is quite often going to give you a good structure for _your smallest screen devices too_. If a user is scrolling through a long document on mobile, the priorities for that user quite often map to what should be a priority in the source. - Create a responsive, and responsible grid - : With a solid document you can begin to add your layout, it is likely you will be using [media queries](/en-US/docs/Web/CSS/CSS_media_queries) to create additional columns and make changes for different screen sizes and devices. Grid can be really very useful here, elements deprioritized in the mobile source order can be moved into a sidebar in a desktop layout, for example. The key here is to keep testing, a very simple test is to _tab around the document_. Does that order still make sense? Check that you do not end up leaping from the top to the bottom of the layout in a peculiar way. If so that would be a sign that you need to address something about the layout. - Returning to the source - : If at any time in the design process you find yourself using grid to relocate the position of an element, consider whether you should return to your document and make a change to the logical order too. The nice thing about using CSS Grid Layout is that you should be able to move an item in the source to match the logical order, without needing to make big changes to your layout. This is a huge improvement over a {{cssxref("float")}}-based layout for example, where document source matters a lot in order to get layouts at different breakpoints. However the onus is on us as developers to remember to go back to our source and update it to maintain logical order. ## Grid and the danger of markup flattening Another issue to be aware of in CSS Grid Layout and to a lesser extent in CSS Flexbox, is the temptation to _flatten_ markup. As we have discovered, for an item to become a grid item it needs to be a direct child of the grid container. Therefore, where you have a {{HTMLElement("ul")}} element inside a grid container, _that_ `ul` becomes a grid item – the child {{HTMLElement("li")}} elements do not. The [subgrid](/en-US/docs/Web/CSS/CSS_grid_layout/Subgrid) value of `grid-template-columns` and `grid-template-rows` will solve this problem once widely implemented. It will allow the grid to be inherited by grid items and passed down the tree. Given that interoperable support for subgrid is limited, there is a definite temptation when developing a site using CSS Grid Layout to flatten out the markup, to remove semantic elements in order to make it simpler to create a layout. An example would be where some content was semantically marked up as a list but you decide to use a set of {{HTMLElement("div")}} elements instead as then you can have the element to be a direct child of a container set to `display: grid`. Be aware of this temptation and find ways to develop your design without stripping out the markup. Starting out with a well-structured document is a very good way to avoid the problem, as you will be aware that you are removing semantic elements in order to make the layout work if you actually have to go into the document and do so! ## Further reading There is not a lot of existing material regarding accessibility and CSS Grid Layout. Many of the issues are similar to those raised regarding CSS Flexbox, which also gives methods of reordering content with {{cssxref("flex-direction")}} and the {{cssxref("order")}} property. The concept of visual display following document source order is detailed in the _WCAG Techniques for Success Criteria – [Technique C27](https://www.w3.org/TR/WCAG20-TECHS/C27.html)_. As a way to start thinking about these issues, as you use CSS Grid Layout I would suggest reading _[Flexbox & the Keyboard Navigation Disconnect](https://tink.uk/flexbox-the-keyboard-navigation-disconnect/)_ from Léonie Watson. Also [the video of Léonie's presentation from ffconf](https://www.youtube.com/watch?v=spxT2CmHoPk) is helpful to understand more about how screen readers work with the visual representation of things in CSS. Adrian Roselli has also posted regarding [tab order in various browsers](https://adrianroselli.com/2015/10/html-source-order-vs-css-display-order.html) – although this was prior to grid support being fully implemented in Firefox.
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/basic_concepts_of_grid_layout/index.md
--- title: Basic concepts of grid layout slug: Web/CSS/CSS_grid_layout/Basic_concepts_of_grid_layout page-type: guide --- {{CSSRef}} [CSS grid layout](/en-US/docs/Web/CSS/CSS_grid_layout) introduces a two-dimensional grid system to CSS. Grids can be used to lay out major page areas or small user interface elements. This article introduces the CSS Grid Layout and the new terminology that is part of the CSS Grid Layout Level 1 specification. The features shown in this overview will then be explained in greater detail in the rest of this guide. ## What is a grid? A grid is a set of intersecting horizontal and vertical lines defining columns and rows. Elements can be placed onto the grid within these column and row lines. CSS grid layout has the following features: ### Fixed and flexible track sizes You can create a grid with fixed track sizes – using pixels for example. This sets the grid to the specified pixel which fits to the layout you desire. You can also create a grid using flexible sizes with percentages or with the `fr` unit designed for this purpose. ### Item placement You can place items into a precise location on the grid using line numbers, names or by targeting an area of the grid. Grid also contains an algorithm to control the placement of items not given an explicit position on the grid. ### Creation of additional tracks to hold content You can define an explicit grid with grid layout. The Grid Layout specification is flexible enough to add additional rows and columns when needed. Features such as adding "as many columns that will fit into a container" are included. ### Alignment control Grid contains alignment features so we can control how the items align once placed into a grid area, and how the entire grid is aligned. ### Control of overlapping content More than one item can be placed into a grid cell or area and they can partially overlap each other. This layering may then be controlled with the {{cssxref("z-index")}} property. Grid is a powerful specification that, when combined with other parts of CSS such as [flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout), can help you create layouts that were previously impossible to build in CSS. It all starts by creating a grid in your **grid container**. ## Grid container We create a _grid container_ by declaring `display: grid` or `display: inline-grid` on an element. As soon as we do this, all _direct children_ of that element become _grid items_. In this example, I have a containing div with a class of wrapper and, inside are five child elements. ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` I make the `.wrapper` a grid container. ```css .wrapper { display: grid; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('The_Grid_container', '200', '330') }} All the direct children are now grid items. In a web browser, you won't see any difference to how these items are displayed before turning them into a grid, as grid has created a single column grid for the items. At this point, you may find it useful to work with the [Grid Inspector](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html), available as part of Firefox's Developer Tools. If you view this example in Firefox and inspect the grid, you will see a small icon next to the value `grid`. Click this and then the grid on this element will be overlaid in the browser window. ![Using the Grid Highlighter in DevTools to view a grid](1-grid-inspector.png) As you learn and then work with the CSS Grid Layout, this tool will give you a better idea of what is happening with your grids visually. If we want to start making this more grid-like we need to add column tracks. ## Grid tracks We define rows and columns on our grid with the {{cssxref("grid-template-rows")}} and {{cssxref("grid-template-columns")}} properties. These define grid tracks. A _grid track_ is the space between any two adjacent lines on the grid. The image below shows a highlighted track – this is the first-row track in our grid. ![A box with 3 grid items. Above the three items is a solid light green area which is the track.](1_grid_track.png) Grid tracks are defined in the explicit grid by using the `grid-template-columns` and `grid-template-rows` properties or the shorthand `grid` or `grid-template` properties. Tracks are also created in the implicit grid by positioning a grid item outside of the tracks created in the explicit grid. ### Basic example I can add to our earlier example by adding the `grid-template-columns` property, then defining the size of the column tracks. I have now created a grid with three 200-pixel-wide column tracks. The child items will be laid out on this grid one in each grid cell. ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: 200px 200px 200px; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('Basic_example', '610', '140') }} ### The fr unit Tracks can be defined using any length unit. Grid also introduces an additional length unit to help us create flexible grid tracks. The new `fr` unit represents a fraction of the available space in the grid container. The next grid definition would create three equal width tracks that grow and shrink according to the available space. ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('The_fr_unit', '220', '140') }} ### Unequal sizes In this next example, we create a definition with a `2fr` track then two `1fr` tracks. The available space is split into four. Two parts are given to the first track and one part each to the next two tracks. ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: 2fr 1fr 1fr; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('Unequal_sizes', '220', '140') }} ### Mixing flexible and absolute sizes In this final example, we mix absolute sized tracks with `fr` units. The first track is 500 pixels, so the fixed width is taken away from the available space. The remaining space is divided into three and assigned in proportion to the two flexible tracks. ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: 500px 1fr 2fr; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('Mixing_flexible_and_absolute_sizes', '220', '140') }} ### Track listings with repeat() notation Large grids with many tracks can use the `repeat()` notation, to repeat all or a section of the track listing. For example the grid definition: ```css .wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr; } ``` Can also be written as: ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); } ``` Repeat notation can be used for a part of the track listing. In this next example I have created a grid with an initial 20-pixel track, then a repeating section of 6 `1fr` tracks then a final 20-pixel track. ```css .wrapper { display: grid; grid-template-columns: 20px repeat(6, 1fr) 20px; } ``` Repeat notation takes the track listing, and uses it to create a repeating pattern of tracks. In this next example, my grid will consist of 10 tracks, a `1fr` track, and then followed by a `2fr` track. This pattern will be repeated five times. ```css .wrapper { display: grid; grid-template-columns: repeat(5, 1fr 2fr); } ``` ### Implicit and explicit grids When creating our example grid we specifically defined our column tracks with the {{cssxref("grid-template-columns")}} property, but the grid also created rows on its own. These rows are part of the implicit grid. Whereas the explicit grid consists of any rows and columns defined with {{cssxref("grid-template-columns")}} or {{cssxref("grid-template-rows")}}. If you place something outside of the defined grid—or due to the amount of content, more grid tracks are needed—then the grid creates rows and columns in the implicit grid. These tracks will be auto-sized by default, resulting in their size being based on the content that is inside them. You can also define a set size for tracks created in the implicit grid with the {{cssxref("grid-auto-rows")}} and {{cssxref("grid-auto-columns")}} properties. In the below example, we use `grid-auto-rows` to ensure that tracks created in the implicit grid are 200 pixels tall. ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 200px; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('The_implicit_and_explicit_grid', '230', '450') }} ### Track sizing and minmax When setting up an explicit grid or defining the sizing for automatically created rows or columns we may want to give tracks a minimum size, but also ensure they expand to fit any content that is added. For example, I may want my rows to never collapse smaller than 100 pixels, but if my content stretches to 300 pixels in height, then I would like the row to stretch to that height. Grid has a solution for this with the {{cssxref("minmax", "minmax()")}} function. In this next example I am using `minmax()` in the value of {{cssxref("grid-auto-rows")}}. This means automatically created rows will be a minimum of 100 pixels tall, and a maximum of `auto`. Using `auto` means that the size will look at the content size and will stretch to give space for the tallest item in a cell, in this row. ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: minmax(100px, auto); } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div> Two <p>I have some more content in.</p> <p>This makes me taller than 100 pixels.</p> </div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` {{ EmbedLiveSample('Track_sizing_and_minmax', '240', '470') }} ## Grid lines It should be noted that when we define a grid we define the grid tracks, not the lines. Grid then gives us numbered lines to use when positioning items. In our three column, two row grid we have four column lines. ![Diagram showing numbered grid lines.](1_diagram_numbered_grid_lines.png) Lines are numbered according to the writing mode of the document. In a left-to-right language, line 1 is on the left-hand side of the grid. In a right-to-left language, it is on the right-hand side of the grid. Lines can also be named, and we will look at how to do this in a later guide in this series. ### Positioning items against lines We will be exploring line based placement in full detail in a later article. The following example demonstrates doing this in a simple way. When placing an item, we target the line – rather than the track. In the following example I am placing the first two items on our three column track grid, using the {{cssxref("grid-column-start")}}, {{cssxref("grid-column-end")}}, {{cssxref("grid-row-start")}} and {{cssxref("grid-row-end")}} properties. Working from left to right, the first item is placed against column line 1, and spans to column line 4, which in our case is the far-right line on the grid. It begins at row line 1 and ends at row line 3, therefore spanning two row tracks. The second item starts on grid column line 1, and spans one track. This is the default so I do not need to specify the end line. It also spans two row tracks from row line 3 to row line 5. The other items will place themselves into empty spaces on the grid. ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> <div class="box5">Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 100px; } .box1 { grid-column-start: 1; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; } .box2 { grid-column-start: 1; grid-row-start: 3; grid-row-end: 5; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('Positioning_items_against_lines', '230', '450') }} > **Note:** Don't forget that you can use the [Grid Inspector](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html) in Firefox Developer Tools to see how the items are positioned against the lines of the grid. ### Line-positioning shorthands The longhand values used above can be compressed onto one line for columns with {{cssxref("grid-column")}}, and one line for rows with {{cssxref("grid-row")}}. The following example would give the same positioning as in the previous code, but with far less CSS. The value before the forward slash character (`/`) is the start line, the value after the end line. You can omit the end value if the area only spans one track. ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 100px; } .box1 { grid-column: 1 / 4; grid-row: 1 / 3; } .box2 { grid-column: 1; grid-row: 3 / 5; } ``` ## Grid cells A _grid cell_ is the smallest unit on a grid. Conceptually it is like a table cell. As we saw in our earlier examples, once a grid is defined as a parent the child items will lay themselves out in one cell each of the defined grid. In the below image, I have highlighted the first cell of the grid. ![The first cell of the grid highlighted](1_grid_cell.png) ## Grid areas Items can span one or more cells both by row or by column, and this creates a _grid area_. Grid areas must be rectangular – it isn't possible to create an L-shaped area for example. The highlighted grid area spans two row and two column tracks. ![A grid area](1_grid_area.png) ## Gutters _Gutters_ or _alleys_ between grid cells can be created using the {{cssxref("column-gap")}} and {{cssxref("row-gap")}} properties, or the shorthand {{cssxref("gap")}}. In the below example, I am creating a 10-pixel gap between columns and a `1em` gap between rows. ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); column-gap: 10px; row-gap: 1em; } ``` > **Note:** When grid first shipped in browsers the {{cssxref("column-gap")}}, {{cssxref("row-gap")}} and {{cssxref("gap")}} were prefixed with the `grid-` prefix as `grid-column-gap`, `grid-row-gap` and `grid-gap` respectively. > > Browsers all now support unprefixed values, however the prefixed versions will be maintained as aliases making them safe to use. ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css hidden * { box-sizing: border-box; } .wrapper { column-gap: 10px; row-gap: 1em; border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('Gutters') }} Any space used by gaps will be accounted for before space is assigned to the flexible length `fr` tracks, and gaps act for sizing purposes like a regular grid track, however you cannot place anything into a gap. In terms of line-based positioning, the gap acts like a thick line. ## Nesting grids A grid item can become a grid container. In the following example, I have the three-column grid that I created earlier, with our two positioned items. In this case the first item has some sub-items. As these items are not direct children of the grid they do not participate in grid layout and so display in a normal document flow. ![Nested grid in flow](1_nested_grids_in_flow.png) ### Nesting without subgrid If I set `box1` to `display: grid` I can give it a track definition and it too will become a grid. The items then lay out on this new grid. ```css .box1 { grid-column-start: 1; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; display: grid; grid-template-columns: repeat(3, 1fr); } ``` ```html <div class="wrapper"> <div class="box box1"> <div class="nested">a</div> <div class="nested">b</div> <div class="nested">c</div> </div> <div class="box box2">Two</div> <div class="box box3">Three</div> <div class="box box4">Four</div> <div class="box box5">Five</div> </div> ``` ```css * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; gap: 3px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); } .box { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } .box1 { grid-column: 1 / 4; } .nested { border: 2px solid #ffec99; border-radius: 5px; background-color: #fff9db; padding: 1em; } ``` {{ EmbedLiveSample('Nesting_without_subgrid', '600', '340') }} In this case the nested grid has no relationship to the parent. As you can see in the example it has not inherited the {{cssxref("gap")}} of the parent and the lines in the nested grid do not align to the lines in the parent grid. ### Subgrid In addition to regular grids, _subgrid_ lets us create nested grids that use the track definition of the parent grid. To use them, we edit the above nested grid example to change the track definition of `grid-template-columns: repeat(3, 1fr)`, to `grid-template-columns: subgrid`. The nested grid then use the parent grid tracks to lay out items. ```css .box1 { grid-column-start: 1; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; display: grid; grid-template-columns: subgrid; } ``` ## Layering items with z-index Grid items can occupy the same cell, and in this case we can use the {{cssxref("z-index")}} property to control the order in which overlapping items stack. ### Overlapping without z-index If we return to our example with items positioned by line number, we can change this to make two items overlap. ```html <div class="wrapper"> <div class="box box1">One</div> <div class="box box2">Two</div> <div class="box box3">Three</div> <div class="box box4">Four</div> <div class="box box5">Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 100px; } .box1 { grid-column-start: 1; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; } .box2 { grid-column-start: 1; grid-row-start: 2; grid-row-end: 4; } ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .box { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('Overlapping_without_z-index', '230', '460') }} The item `box2` is now overlapping `box1`, it displays on top as it comes later in the source order. ### Controlling the order We can control the order in which items stack up by using the `z-index` property - just like positioned items. If we give `box2` a lower `z-index` than `box1` it will display below `box1` in the stack. ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 100px; } .box1 { grid-column-start: 1; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; z-index: 2; } .box2 { grid-column-start: 1; grid-row-start: 2; grid-row-end: 4; z-index: 1; } ``` ```html hidden <div class="wrapper"> <div class="box box1">One</div> <div class="box box2">Two</div> <div class="box box3">Three</div> <div class="box box4">Four</div> <div class="box box5">Five</div> </div> ``` ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .box { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` {{ EmbedLiveSample('Controlling_the_order', '230', '460') }} ## Next steps In this article, we took a very quick look at the possibilities of grid layouts. Explore and play with the code examples, and then move on to [the next part of this guide](/en-US/docs/Web/CSS/CSS_grid_layout/Relationship_of_grid_layout_with_other_layout_methods), where we will really start to dig into the details of CSS Grid Layout.
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/grid_layout_and_progressive_enhancement/index.md
--- title: CSS Grid Layout and progressive enhancement slug: Web/CSS/CSS_grid_layout/Grid_layout_and_progressive_enhancement page-type: guide --- {{CSSRef}} In Spring of 2017, we saw for the first time a major specification like Grid being shipped into browsers almost simultaneously, and we now have CSS Grid Layout support in the public versions of Firefox, Chrome, Opera, Safari and Edge. However, while evergreen browsers mean that many of us are going to see the majority of users having Grid Layout support very quickly, there are also old or non-supporting browsers to contend with. In this guide we will walk through a variety of strategies for support. ## The supporting browsers CSS Grid Layout is unprefixed in all modern browsers. Support for all the properties and values detailed in these guides is interoperable across browsers. This means that if you write some Grid Layout code in Firefox, it should work in the same way in Chrome. This is no longer an experimental specification, and you are safe to use it in production. ## Is it safe to use CSS grids for my layout? Yes. As with any front-end technology choice, the decision to use CSS Grid Layout will come down to the browsers your site visitors are typically using. ## Starting to use Grid in production It is worth noting that you do not have to use grid in an _all or nothing_ way. Start by enhancing elements in your design with grid, that could otherwise display using an older method. Overwriting of legacy methods with grid layout works surprisingly well, due to the way grid interacts with these other methods. ### Floats [Floats](/en-US/docs/Learn/CSS/CSS_layout/Floats) used to be used to create multiple column layouts. If you're supporting an old codebase with floated layouts, there will be no conflict. Grid items ignore the float property; the fact is that _a grid item takes precedence._ In the example below, I have a simple media object. If the {{cssxref("float")}} is not removed from legacy CSS, as the container is a grid container, it's OK. We can use the alignment properties that are implemented in CSS Grids. The {{cssxref("float")}} no longer applies, and I can use the CSS Box Alignment property {{cssxref("align-self")}} to align my content to the end of the container: ```css * { box-sizing: border-box; } img { max-width: 100%; display: block; } .media { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 400px; display: grid; grid-template-columns: 1fr 2fr; grid-template-areas: "img content"; margin-bottom: 1em; } .media::after { content: ""; display: block; clear: both; } .media .text { padding: 10px; align-self: end; } /* old code we can't remove */ .media .image { float: left; width: 150px; margin-right: 20px; } ``` ```html <div class="media"> <div class="image"> <img src="https://via.placeholder.com/150x150" alt="placeholder" /> </div> <div class="text"> This is a media object example. I am using floats for older browsers and grid for new ones. </div> </div> ``` {{ EmbedLiveSample('Floats', '500', '200') }} The image below shows the media object in a non-supporting browser on the left, and a supporting one on the right: ![A simple example of overriding a floated layout using grid. Both have the image aligned left. The text is vertically aligned at top in the float example and at the bottom in the grid example.](10-float-simple-override.png) ### Using feature queries The above example is very simple, and we can get away without needing to write code that would be a problem to browsers that do not support grid, and legacy code is not an issue to our grid supporting browsers. However, things are not always so simple. #### A more complex example In this next example, I have a set of floated cards. I have given the cards a {{cssxref("width")}}, in order to {{cssxref("float")}} them. To create gaps between the cards, I use a {{cssxref("margin")}} on the items, and then a negative margin on the container: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 600px; margin: 0 auto; } .wrapper li { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper ul { overflow: hidden; margin: 0 -10px; padding: 0; list-style: none; } .wrapper li { float: left; width: calc(33.333333% - 20px); margin: 0 10px 20px 10px; } ``` ```html <div class="wrapper"> <ul> <li class="card"> <h2>One</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Two</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Three</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Four</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Five</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Six</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> </ul> </div> ``` {{ EmbedLiveSample('A_more_complex_example', '550', '450') }} The example demonstrates the typical problem that we have with floated layouts: if additional content is added to any one card, the layout breaks. ![A floated cards layout demonstrating the problem caused by uneven content height. The top row has 3 cards. The fourth card is floated under the third card. Then a bottom row has contains the fifth and sixth cards. There is a largish empty space under the fourth card. ](10-floated-cards.png) As a concession for older browsers, I have set a {{cssxref("min-height")}} on the items, and hope that my content editors won't add too much content and make a mess of the layout! I then enhance the layout using grid. I can turn my {{HTMLElement("ul")}} into a grid container with three column tracks. However, the width I have assigned to the list items themselves still applies, and it now makes those items a third of the width of the track: ![Six very tall, very narrow grid items with text overflowing on the right. After applying grid to our container, the width of the items is now incorrect as they display at one third of the item width.](10-float-width-problem.png) If I reset the width to `auto`, then this will stop the float behavior happening for older browsers. I need to be able to define the width for older browsers, and remove the width for grid supporting browsers. Thanks to [CSS Feature Queries](/en-US/docs/Web/CSS/@supports) I can do this, right in my CSS. #### A solution using feature queries _Feature queries_ will look very familiar if you have ever used a [media query](/en-US/docs/Web/CSS/CSS_media_queries) to create a responsive layout. Rather than checking a {{glossary("viewport")}} width, or some feature of the browser or device, we check for support of a CSS property and value pair using an {{cssxref("@supports")}} rule. Inside the feature query, we can then write any CSS we need to apply our modern layout, and remove anything required for the older layout. ```css @supports (display: grid) { .wrapper { /* do anything for grid supporting browsers here. */ } } ``` Feature queries have excellent browser support, and all of the browsers that support the updated grid specification support feature queries too. You can use them to deal with the issue we have with our enhanced: floated layout. I use an `@supports` rule to check for support of `display: grid`. I then do my grid code on the {{HTMLElement("ul")}}, set my width and {{cssxref("min-height")}} on the {{HTMLElement("li")}} to `auto`. I also remove the margins and negative margins, and replace the spacing with the {{cssxref("gap")}} property. This means I don't get a final margin on the last row of boxes. The layout now works, even if there is more content in one of the cards, than the others: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 600px; margin: 0 auto; } .wrapper li { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper ul { overflow: hidden; margin: 0 -10px; padding: 0; list-style: none; } .wrapper li { float: left; width: calc(33.333333% - 20px); margin: 0 10px 20px 10px; } @supports (display: grid) { .wrapper ul { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin: 0; } .wrapper li { width: auto; min-height: auto; margin: 0; } } ``` ```html <div class="wrapper"> <ul> <li class="card"> <h2>One</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Two</h2> <p>We can use CSS Grid to overwrite older methods.</p> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Three</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Four</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Five</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Six</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> </ul> </div> ``` {{ EmbedLiveSample('A_solution_using_feature_queries', '550', '480') }} ## Overwriting other values of `display` Due to the problems of creating grids of items using floats, many of us would use a different method to the floated method shown above to lay out a set of cards. Using `display: inline-block` is an alternate method. Once again I can use feature queries to overwrite a layout that uses `display: inline-block`, and again I don't need to overwrite everything. An item that is set to `inline-block` becomes a grid item, and so the behavior of `inline-block` no longer applies. I have used the {{cssxref("vertical-align")}} property on my item when in the `inline-block` display mode, but this property does not apply to grid items and, therefore, is ignored once the item becomes a grid item: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 600px; margin: 0 auto; } .wrapper li { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper ul { margin: 0 -10px; padding: 0; list-style: none; } .wrapper li { display: inline-block; vertical-align: top; width: calc(33.333333% - 20px); margin: 0 10px 20px 10px; } @supports (display: grid) { .wrapper ul { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin: 0; } .wrapper li { width: auto; margin: 0; } } ``` ```html <div class="wrapper"> <ul> <li class="card"> <h2>One</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Two</h2> <p>We can use CSS Grid to overwrite older methods.</p> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Three</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Four</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Five</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> <li class="card"> <h2>Six</h2> <p>We can use CSS Grid to overwrite older methods.</p> </li> </ul> </div> ``` {{ EmbedLiveSample('Overwriting_other_values_of_display', '500', '480') }} Once again it is the width on the item we need to address, and then any other properties we want to enhance. In this example I have again used `gap`, rather than margins and negative margins to create my gutters. ## How does the specification define these overrides? The CSS Grid Layout specification details why we can overwrite the behavior of certain properties when something becomes a grid item. The key sections of the specification are: - [Establishing Grid Containers](https://drafts.csswg.org/css-grid/#grid-containers) - [Grid Items](https://drafts.csswg.org/css-grid/#grid-items) - [Grid Item Display](https://drafts.csswg.org/css-grid/#grid-item-display) As this behavior is detailed in the specification, you are safe to rely on using these overrides in your support for older browsers. Nothing described here should be seen as a "hack". Rather, we are taking advantage of the fact that the grid specification details the interaction between different layout methods. ### Other values of display When an element has a parent set to `display: grid` it is _blockified_, as defined in the [CSS display specification](https://drafts.csswg.org/css-display-3/#blockify). In the case of our item set to `inline-block`, this is why `display: inline-block` no longer applied. If you are using `display: table` for your legacy layout, an item set to `display: table-cell` generates anonymous boxes. Therefore, if you use `display: table-cell` without any parent element set to `display-table`, an anonymous table wrapper is created around any adjacent cells, just as if you had wrapped them in a div or other element set to `display: table`. If you have an item set to `display: table-cell`, and then in a feature query change the parent to `display: grid`, this anonymous box creation will not happen. This means you can overwrite `display: table` based layouts, without having additional anonymous boxes. ### Floated elements As we have already seen, {{cssxref("float")}} and also {{cssxref("clear")}} have no effect on a grid item. Therefore you do not need to explicitly set items to `float: none`. ### Vertical alignment The alignment property {{cssxref("vertical-align")}} has no effect on a grid item. In layouts using `display: inline-block` or `display: table`, you might use the vertical-align property to perform basic alignment. In your grid layout you then have the far more powerful box alignment properties. ### Multiple-column layout You can also use multiple column layout as your legacy browser plan, as the `column-*` properties do not apply when applied to a grid container. ## Further reading - For an excellent explanation of feature queries, and how to use them well, see [Using Feature Queries in CSS (2016)](https://hacks.mozilla.org/2016/08/using-feature-queries-in-css/). - [CSS Grid and the New Autoprefixer (2018)](https://css-tricks.com/css-grid-in-ie-css-grid-and-the-new-autoprefixer/)
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/grid_layout_using_line-based_placement/index.md
--- title: Grid layout using line-based placement slug: Web/CSS/CSS_grid_layout/Grid_layout_using_line-based_placement page-type: guide --- {{CSSRef}} In the [article covering the basic concepts of grid layout](/en-US/docs/Web/CSS/CSS_grid_layout/Basic_concepts_of_grid_layout), we started to look at how to position items on a grid using line numbers. In this article, we will fully explore how this fundamental feature of the specification works. Starting your exploration of grid with numbered lines is the most logical place to begin because when you use grid layout, you always have numbered lines. The lines are numbered for columns and rows, and are indexed from 1. Note that grid is indexed according to the writing mode of the document. In a left to right language such as English line 1 is on the left-hand side of the grid. If you are working in a right-to-left language such as Arabic then line 1 will be the far right of the grid. We will learn more about the interaction between writing modes and grids in a later guide. ## A basic example As a very simple example we can take a grid with 3 column tracks and 3 row tracks. This gives us 4 lines in each dimension. Inside our grid container, we have four child elements. If we do not place these on to the grid in any way they will lay out according to the auto-placement rules, one item in each of the first four cells. If you use the [Firefox Grid Highlighter](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html) you can see how the grid has defined columns and rows. ![Our Grid highlighted in DevTools](highlighted_grid.png) ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` {{ EmbedLiveSample('A_basic_example', '300', '330') }} ## Positioning items by line number We can use line-based placement to control where these items sit on the grid. We would like the first item to start on the far left of the grid and span a single column track. It should also start on the first row line, at the top of the grid and span to the fourth row line. ```css .box1 { grid-column-start: 1; grid-column-end: 2; grid-row-start: 1; grid-row-end: 4; } ``` As you position some items, other items on the grid will continue to be laid out using the auto-placement rules. We will take a proper look at how these work in a later guide but you can see as you work that grid is laying out un-placed items into empty cells of the grid. Addressing each item individually we can place all four items spanning row and column tracks. Note that we can leave cells empty if we wish. One of the very nice things about Grid Layout is the ability to have white space in our designs without having to push things around using margins to prevent floats from rising up into the space we have left. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-column-start: 1; grid-column-end: 2; grid-row-start: 1; grid-row-end: 4; } .box2 { grid-column-start: 3; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; } .box3 { grid-column-start: 2; grid-column-end: 3; grid-row-start: 1; grid-row-end: 2; } .box4 { grid-column-start: 2; grid-column-end: 4; grid-row-start: 3; grid-row-end: 4; } ``` {{ EmbedLiveSample('Positioning_items_by_line_number', '300', '330') }} ## The `grid-column` and `grid-row` shorthands We have quite a lot of code here to position each item. It should come as no surprise to know there is a [shorthand](/en-US/docs/Web/CSS/Shorthand_properties). The {{cssxref("grid-column-start")}} and {{cssxref("grid-column-end")}} properties can be combined into {{cssxref("grid-column")}}, {{cssxref("grid-row-start")}} and {{cssxref("grid-row-end")}} into {{cssxref("grid-row")}}. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-column: 1 / 2; grid-row: 1 / 4; } .box2 { grid-column: 3 / 4; grid-row: 1 / 3; } .box3 { grid-column: 2 / 3; grid-row: 1 / 2; } .box4 { grid-column: 2 / 4; grid-row: 3 / 4; } ``` {{ EmbedLiveSample('The_grid-column_and_grid-row_shorthands', '300', '330') }} ## Default spans In the above examples, we specified every end row and column line, in order to demonstrate the properties, however in practice if an item only spans one track you can omit the `grid-column-end` or `grid-row-end` value. Grid defaults to spanning one track. ### Default spans with longhand placement This means that our initial, long-hand, example would look like this: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-column-start: 1; grid-row-start: 1; grid-row-end: 4; } .box2 { grid-column-start: 3; grid-row-start: 1; grid-row-end: 3; } .box3 { grid-column-start: 2; grid-row-start: 1; } .box4 { grid-column-start: 2; grid-column-end: 4; grid-row-start: 3; } ``` {{ EmbedLiveSample('Default_spans_with_longhand_placement', '300', '330') }} ### Default spans with shorthand placement Our shorthand would look like the following code, with no forward slash and second value for the items spanning one track only. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-column: 1; grid-row: 1 / 4; } .box2 { grid-column: 3; grid-row: 1 / 3; } .box3 { grid-column: 2; grid-row: 1; } .box4 { grid-column: 2 / 4; grid-row: 3; } ``` {{ EmbedLiveSample('Default_spans_with_shorthand_placement', '300', '330') }} ## The `grid-area` property We can take things a step further and define each area with a single property – {{cssxref("grid-area")}}. The order of the values for grid-area are as follows. - grid-row-start - grid-column-start - grid-row-end - grid-column-end ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-area: 1 / 1 / 4 / 2; } .box2 { grid-area: 1 / 3 / 3 / 4; } .box3 { grid-area: 1 / 2 / 2 / 3; } .box4 { grid-area: 3 / 2 / 4 / 4; } ``` {{ EmbedLiveSample('The_grid-area_property', '300', '330') }} This order of values for `grid-area` can seem a little strange, it is the opposite of the direction in which we specify margins and padding as a shorthand for example. It may help to realize that this is due to grid using the flow-relative directions defined in the CSS Writing Modes specification. We will explore how grids work with writing modes in a later article however we have the concept of four flow-relative directions: - block-start - block-end - inline-start - inline-end We are working in English, a left-to-right language. Our block-start is the top row line of the grid container, block-end is the final row line of the container. Our inline-start is the left-hand column line as inline-start is always the point from which text would be written in the current writing mode, inline-end is the final column line of our grid. When we specify our grid area using the `grid-area` property we first define both start lines `block-start` and `inline-start`, then both end lines `block-end` and `inline-end`. This seems unusual at first as we are used to the physical properties of top, right, bottom and left but makes more sense if you start to think of websites as being multi-directional in writing mode. ## Counting backwards We can also count backwards from the block and inline end of the grid, for English that would be the right-hand column line and final row line. These lines can be addressed as `-1`, and you can count back from there – so the second last line is `-2`. It is worth noting that the final line is the final line of the _explicit grid_, the grid defined by `grid-template-columns` and `grid-template-rows`, and does not take into account any rows or columns added in the _implicit grid_ outside of that. In this next example, we have flipped the layout we were working with by working from the right and bottom of our grid when placing the items. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-column-start: -1; grid-column-end: -2; grid-row-start: -1; grid-row-end: -4; } .box2 { grid-column-start: -3; grid-column-end: -4; grid-row-start: -1; grid-row-end: -3; } .box3 { grid-column-start: -2; grid-column-end: -3; grid-row-start: -1; grid-row-end: -2; } .box4 { grid-column-start: -2; grid-column-end: -4; grid-row-start: -3; grid-row-end: -4; } ``` {{ EmbedLiveSample('Counting_backwards', '300', '330') }} ### Stretching an item across the grid Being able to address the start and end lines of the grid is useful as you can then stretch an item right across the grid with: ```css .item { grid-column: 1 / -1; } ``` ## Gutters or Alleys The CSS Grid Specification includes the ability to add gutters between column and row tracks with the {{cssxref("column-gap")}} and {{cssxref("row-gap")}} properties. These specify a gap that acts much like the {{cssxref("column-gap")}} property in multi-column layout. > **Note:** When grid first shipped in browsers the {{cssxref("column-gap")}}, {{cssxref("row-gap")}} and {{cssxref("gap")}} properties were prefixed with the `grid-` prefix as `grid-column-gap`, `grid-row-gap` and `grid-gap` respectively. > > Browsers are updating their rendering engines to remove this prefix, however the prefixed versions will be maintained as aliases, making them safe to use. Gaps only appear between tracks of the grid, they do not add space to the top and bottom, left or right of the container. We can add gaps to our earlier example by using these properties on the grid container. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-column: 1; grid-row: 1 / 4; } .box2 { grid-column: 3; grid-row: 1 / 3; } .box3 { grid-column: 2; grid-row: 1; } .box4 { grid-column: 2 / 4; grid-row: 3; } .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); column-gap: 20px; row-gap: 1em; } ``` {{ EmbedLiveSample('Gutters_or_Alleys', '300', '350') }} ### The gap shorthand The two properties can also be expressed as a shorthand, {{cssxref("gap")}}. If you only give one value for `gap` it will apply to both column and row gaps. If you specify two values, the first is used for `row-gap` and the second for `column-gap`. ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); gap: 1em 20px; } ``` In terms of line-based positioning of items, the gap acts as if the line has gained extra width. Anything starting at that line starts after the gap and you cannot address the gap or place anything into it. If you want gutters that act more like regular tracks you can of course define a track for the purpose instead. ## Using the `span` keyword In addition to specifying the start and end lines by number, you can specify a start line and then the number of tracks you would like the area to span. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` ```css .box1 { grid-column: 1; grid-row: 1 / span 3; } .box2 { grid-column: 3; grid-row: 1 / span 2; } .box3 { grid-column: 2; grid-row: 1; } .box4 { grid-column: 2 / span 2; grid-row: 3; } ``` {{ EmbedLiveSample('Using_the_span_keyword', '300', '330') }} You can also use the `span` keyword in the value of `grid-row-start`/`grid-row-end` and `grid-column-start/grid-column-end`. The following two examples will create the same grid area. In the first we set the start row line, then the end line we explain that we want to span 3 lines. The area will start at line 1 and span 3 lines to line 4. ```css .box1 { grid-column-start: 1; grid-row-start: 1; grid-row-end: span 3; } ``` In the second example, we specify the end row line we want the item to finish at and then set the start line as `span 3`. This means the item will need to span upwards from the specified row line. The area will start at line 4 and span 3 lines to line 1. ```css .box1 { grid-column-start: 1; grid-row-start: span 3; grid-row-end: 4; } ``` To become familiar with line based positioning in grid try to build a few common layouts by placing items onto grids with varying numbers of columns. Remember that if you do not place all of the items, any leftover items will be placed according to auto-placement rules. This may result in the layout you want, but if something is appearing somewhere unexpected, check that you have set a position for it. Also, remember that items on the grid can overlap each other when you place them explicitly like this. That can create some nice effects, however you can also end up with things overlapping incorrectly if you specify the wrong start or end line. The [Firefox Grid Highlighter](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html) can be very useful as you learn, especially if your grid is quite complicated.
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/subgrid/index.md
--- title: Subgrid slug: Web/CSS/CSS_grid_layout/Subgrid page-type: guide browser-compat: css.properties.grid-template-columns.subgrid --- {{CSSRef}} Level 2 of the CSS Grid Layout specification includes a `subgrid` value for {{cssxref("grid-template-columns")}} and {{cssxref("grid-template-rows")}}. This guide details what subgrid does and gives some use cases and design patterns that the feature solves. ## Introduction to subgrid When you add `display: grid` to a grid container, only the direct children become grid items and can then be placed on the grid you created. The children of these items display in normal flow. You can "nest" grids by making a grid item a grid container. These grids, however, are independent of the parent grid and of each other, meaning that they do not take their track sizing from the parent grid. This makes it difficult to line nested grid items up with the main grid. If you set the value `subgrid` on `grid-template-columns`, `grid-template-rows` or both, instead of creating a new track listing the nested grid uses the tracks defined on the parent. For example, if you use `grid-template-columns: subgrid` and the nested grid spans three column tracks of the parent, the nested grid will have three column tracks of the same size as the parent grid. Gaps are inherited but can also be overridden with a different {{cssxref("gap")}} value. Line names can be passed from the parent into the subgrid, and the subgrid can also declare its own line names. ## Subgrid for columns In the example below, I have a grid layout with nine `1fr` column tracks and four rows that are a minimum of 100px tall. I place `.item` from column lines 2 to 7 and rows 2 to 4. I then make this grid item into a grid, giving it column tracks that are a subgrid and defining rows as normal. As the item spans five column tracks, this means that the subgrid has five-column tracks. I can then place `.subitem` on this grid. The rows in this example are not a subgrid, and so behave as a nested grid does normally. The grid area on the parent expands to be large enough for this nested grid. {{EmbedGHLiveSample("css-examples/grid/subgrid/columns.html", '100%', 1200)}} Note that line numbering restarts inside the subgrid — column line 1, when inside the subgrid, is the first line of the subgrid. The subgridded element doesn't inherit the line numbers of the parent grid. This means that you can safely lay out a component that may be placed in different positions on the main grid, knowing that the line numbers on the component will always be the same. ## Subgrid for rows The next example is the same setup; however, we are using `subgrid` as the value of `grid-template-rows` and defining explicit column tracks. So, the column tracks behave as a regular nested grid, but the rows are tied to the two tracks that the child spans. {{EmbedGHLiveSample("css-examples/grid/subgrid/rows.html", '100%', 1200)}} ## A subgrid in both dimensions You can define both rows and columns as a subgrid, as in the example below. This means that your subgrid is tied in both dimensions to the number of tracks on the parent. {{EmbedGHLiveSample("css-examples/grid/subgrid/both.html", '100%', 1200)}} ### No implicit grid in a subgridded dimension If you need to autoplace items and do not know how many items you will have, take care when creating a subgrid, as it will prevent additional rows from being created to hold those items. Take a look at the next example — it uses the same parent and child grid as in the example above. However, I have twelve items inside the subgrid trying to autoplace into ten grid cells. As the subgrid is on both dimensions, there is nowhere for the extra two items to go, so they go into the last track of the grid, as defined in the specification. {{EmbedGHLiveSample("css-examples/grid/subgrid/no-implicit.html", '100%', 1200)}} If we remove the `grid-template-rows` value, we enable regular creation of implicit tracks and although these won't line up with the tracks of the parent, as many as are required will be created. {{EmbedGHLiveSample("css-examples/grid/subgrid/implicit.html", '100%', 1200)}} ## The gap properties and subgrid If you have a {{cssxref("gap")}}, {{cssxref("column-gap")}}, or {{cssxref("row-gap")}} specified on the parent, this will be passed into the subgrid, so it will have the same spacing between tracks as the parent. In some situations, however, you may wish the subgrid tracks to have a different gap or no gap. This can be achieved by using the `gap-*` properties on the grid container of the subgrid. You can see this in the example below. The parent grid has a gap of 20px for rows and columns. The subgrid has `row-gap` set to `0`. {{EmbedGHLiveSample("css-examples/grid/subgrid/gap.html", '100%', 1200)}} If you inspect this in the Firefox Grid Inspector, you can see how the line of the grid is in the correct place down the center of the gap, so when we set the gap to 0, it acts in a similar way to applying a negative margin to an element, giving the space from the gap back to the item. ![The smaller item displays in the gap as row-gap is set to 0 on the subgrid.](gap.png) ## Named grid lines When using CSS Grid, you can name lines on your grid and then position items based on those names rather than the line number. The line names on the parent grid are passed into the subgrid, and you can place items using them. In the example below, I named lines on the parent `col-start` and `col-end` and then used those to place the subitem. {{EmbedGHLiveSample("css-examples/grid/subgrid/line-names.html", '100%', 1200)}} You can also specify line names on the subgrid. This is achieved by adding a list of line names enclosed in square brackets after the `subgrid` keyword. If you have four lines in your subgrid, to name them all, you could use the syntax `grid-template-columns: subgrid [line1] [line2] [line3] [line4]` Lines specified on the subgrid are added to any lines specified on the parent, so you can use either or both. To demonstrate this, I have positioned one item in the example below using the parent lines and one using the subgrid lines. {{EmbedGHLiveSample("css-examples/grid/subgrid/adding-line-names.html", '100%', 1200)}} ## Using subgrids Other than needing to take care of items that do not fit in your subgrid, a subgrid acts very similarly to any nested grid; the only difference is that the track sizing of the subgrid is set on the parent grid. As with any nested grid, however, the size of the content in the subgrid can change the track sizing, assuming a track sizing method is used that allows content to affect the size. In such a case, auto-sized row tracks, for example, will grow to fit content in the main grid and content in the subgrid. As the subgrid value acts in much the same way as a regular nested grid, it is easy to switch between the two. For example, if you realize that you need an implicit grid for rows, you would need to remove the `subgrid` value of `grid-template-rows` and perhaps give a value for `grid-auto-rows` to control the implicit track sizing. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - On the Mozilla Developer YouTube Channel, see the videos [Laying out forms using subgrid](https://www.youtube.com/watch?v=gmQlK3kRft4) and [Don't Wait To Use Subgrid For Better Card Layouts](https://www.youtube.com/watch?v=lLnFtK1LNu4) - [Hello Subgrid!](https://noti.st/rachelandrew/i6gUcF/hello-subgrid) A presentation from CSSConf.eu
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/grids_logical_values_and_writing_modes/index.md
--- title: Grids, logical values, and writing modes slug: Web/CSS/CSS_grid_layout/Grids_logical_values_and_writing_modes page-type: guide --- {{CSSRef}} In these guides, I have already touched on an important feature of grid layout: the support for different writing modes that is built into the specification. For this guide, we will look at this feature of grid and other modern layout methods, learning a little about writing modes and logical vs. physical properties as we do so. ## Logical and physical properties and values CSS is full of **physical** positioning keywords – left and right, top and bottom. If we position an item using absolute positioning, we use these physical keywords as offset values to push the item around. In the code snippet below, the item is placed 20 pixels from the top, and 30 pixels from the left of the container: ```css .container { position: relative; } .item { position: absolute; top: 20px; left: 30px; } ``` ```html <div class="container"> <div class="item">Item</div> </div> ``` Another place you might see physical keywords in use, is when using `text-align: right` to align text to the right. There are also physical **properties** in CSS. We add margins, padding, and borders using these physical properties of {{cssxref("margin-left")}}, {{cssxref("padding-left")}}, and so on. We call these keywords and properties _physical_ because they relate to the screen you are looking at. Left is always left, no matter what direction your text is running. ### Issues with physical properties This can become an issue when developing a site that has to work in multiple languages, including languages that have text starting on the right, rather than the left. Browsers are pretty good at dealing with text direction, and you don't even need to be working in a {{glossary("rtl")}} language to take a look. In the example below, I have two paragraphs. The first paragraph has {{cssxref("text-align")}} set to `left`, the second has no `text-align` property set. I have added `dir="rtl"` to the `html` element, which switches the writing mode from the default for an English language document of `ltr`. You can see that the first paragraph remains left to right, due to the `text-align` value being `left`. The second however, switches direction and the text runs from right to left . ```html hidden <p class="left"> I have my text set to <code>text-align: left</code> I will always align left even if the direction of the text in this document is rtl. </p> <p>I have no alignment set and use the direction set in the document.</p> ``` ```css hidden body { direction: rtl; } p { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; margin: 1em; color: #d9480f; } .left { text-align: left; } ``` {{EmbedLiveSample("Issues with physical properties","",200)}} This is a very simple example of the problem with physical values and properties being used in CSS. They prevent the browser being able to do the work to switch writing mode, as they make the assumption that the text is flowing left to right and top to bottom. ### Logical properties and values Logical properties and values do not make an assumption about text direction. Which is why in Grid Layout we use the keyword `start` when aligning something to the start of the container. For me, working in English, `start` may well be on the left, however it doesn't have to be, and the word `start` infers no physical location. ## Block and Inline Once we begin dealing with logical, rather than physical properties, we stop seeing the world as left to right, and top to bottom. We need a new reference point, and this is where understanding the _block_ and _inline_ axes, that we met previously in the guide to _alignment_, becomes very useful. If you can start to see layout in terms of block and inline, the way things work in grid start to make a lot more sense. ![An image showing the default direction of the Block and Inline Axes.](8-horizontal-tb.png) ## CSS writing modes I'm going to introduce another specification here, that I will be using in my examples: the CSS Writing Modes specification. This spec details how we can use these different writing modes in CSS, not just for the support of languages that have a different writing mode to English, but also for creative purposes. I'll be using the {{cssxref("writing-mode")}} property to make changes to the writing mode applied to our grid, in order to demonstrate how the logical values work. If you want to dig into writing modes further, however, then I would recommend that you read Jen Simmons excellent article on [CSS Writing Modes](https://24ways.org/2016/css-writing-modes/). This goes into more depth on that specification than we will touch upon here. ### `writing-mode` Writing Modes are more than just left to right and right to left text, and the `writing-mode` property helps us display text running in other directions. The {{cssxref("writing-mode")}} property can have values of: - `horizontal-tb` - `vertical-rl` - `vertical-lr` - `sideways-rl` - `sideways-lr` The value `horizontal-tb` is the default for text on the web. It is the direction in which you are reading this guide. The other properties will change the way that text flows in our document, matching the different writing modes found around the world. As a simple example, I have two paragraphs below. The first uses the default `horizontal-tb`, and the second uses `vertical-rl`. In the mode text still runs left to right, however the direction of the text is vertical - inline text now runs down the page, from top to bottom. ```css hidden .wrapper > p { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; margin: 1em; color: #d9480f; max-width: 300px; } ``` ```html <div class="wrapper"> <p style="writing-mode: horizontal-tb"> I have writing mode set to the default <code>horizontal-tb</code> </p> <p style="writing-mode: vertical-rl"> I have writing mode set to <code>vertical-rl</code> </p> </div> ``` {{ EmbedLiveSample('writing-mode', '500', '420') }} ## Writing modes in grid layouts If we now take a look at a grid layout example, we can see how changing the writing mode means changing our idea of where the Block and Inline Axis are. ### Default writing mode The grid in this example has three columns and two row tracks. This means there are three tracks running down the block axis. In default writing mode, grid auto-places items starting at the top left, moving along to the right, filling up the three cells on the inline axis. It then moves onto the next line, creating a new Row track, and fills in more items: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(2, 100px); gap: 10px; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> <div class="item5">Item 5</div> </div> ``` {{ EmbedLiveSample('Default_writing_mode', '500', '230') }} ### Setting writing mode If we add `writing-mode: vertical-lr` to the grid container, we can see that the block and inline Axis are now running in a different direction. The block or _column_ axis now runs across the page from left to right, Inline runs down the page, creating rows from top to bottom. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { writing-mode: vertical-lr; display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(2, 100px); gap: 10px; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> <div class="item4">Item 4</div> <div class="item5">Item 5</div> </div> ``` {{ EmbedLiveSample('Setting_writing_mode', '500', '330') }} ## Logical values for alignment With the block and inline axis able to change direction, the logical values for the alignment properties start to make more sense. In this next example, I am using alignment to align items inside a grid that is set to `writing-mode: vertical-lr`. The `start` and `end` properties work in exactly the same way that they do in the default writing mode, and remain logical in a way that using left and right, top and bottom to align items would not do. This occurs once we've flipped the grid onto the side, like this: ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { writing-mode: vertical-lr; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 100px); gap: 10px; } .item1 { grid-column: 1 / 4; align-self: start; } .item2 { grid-column: 1 / 3; grid-row: 2 / 4; align-self: start; } .item3 { grid-column: 3; grid-row: 2 / 4; align-self: end; justify-self: end; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> </div> ``` {{ EmbedLiveSample('Logical_values_for_alignment', '500', '280') }} If you want to see how these work, with a right to left as well as top to bottom writing mode, switch `vertical-lr` to `vertical-rl`, which is a vertical writing mode running from right to left. ## Auto-placement and Writing Modes In the example already shown, you can see how writing mode changes the direction in which items place themselves onto the grid. Items will, by default, place themselves along the Inline axis then move onto a new row. However, that inline axis may not always run from left to right. ## Line-based placement and Writing Modes The key thing to remember when placing items by line number, is that line 1 is the start line, no matter which writing mode you are in. Line -1 is the end line, no matter which writing mode you are in. ### Line-based placement with left to right text In this next example, I have a grid which is in the default `ltr` direction. I have positioned three items using line-based placement. - Item 1 starts at column line 1, spanning one track. - Item 2 starts at column line -1, spanning to -3. - Item 3 starts at column line 1, spanning to column line 3. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 100px); gap: 10px; } .item1 { grid-column: 1; } .item2 { grid-column: -1 / -3; } .item3 { grid-column: 1 / 3; grid-row: 2; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> </div> ``` {{ EmbedLiveSample('Line-based_placement_with_left_to_right_text', '500', '240') }} ### Line-based placement with right to left text If I now add the {{cssxref("direction")}} property with a value of `rtl` to the grid container, line 1 becomes the right-hand side of the grid, and line -1 on the left. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { direction: rtl; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 100px); gap: 10px; } .item1 { grid-column: 1; } .item2 { grid-column: -1 / -3; } .item3 { grid-column: 1 / 3; grid-row: 2; } ``` ```html <div class="wrapper"> <div class="item1">Item 1</div> <div class="item2">Item 2</div> <div class="item3">Item 3</div> </div> ``` {{ EmbedLiveSample('Line-based_placement_with_right_to_left_text', '500', '240') }} What this demonstrates, is that if you are switching the direction of your text, either for entire pages or for parts of pages, and are using lines: you may want to name your lines, if you do not want the layout to completely switch direction. For some things, for example, where a grid contains text content, this switching may be exactly what you want. For other usage it may not. ### The strange order of values in the `grid-area` property You can use the {{cssxref("grid-area")}} property to specify all four lines of a grid area as one value. When people first encounter this, they are often surprised that the values do not follow the same order as the shorthand for margin – which runs clockwise: top, right, bottom, left. The order of `grid-area` values is: - `grid-row-start` - `grid-column-start` - `grid-row-end` - `grid-column-end` Which for English, in left-to-right means the order is: - `top` - `left` - `bottom` - `right` This is anti-clockwise! So the reverse of what we do for margins and padding. Once you realize that `grid-area` sees the world as "block and inline", you can remember that we are setting the two starts, then the two ends. It becomes much more logical once you know! ## Mixed writing modes and grid layout In addition to displaying documents, using the correct writing mode for the language, writing modes can be used creatively within documents that are otherwise `ltr`. In this next example I have a grid layout with a set of links down one side. I've used writing modes to turn these on their side in the column track: ```css .wrapper { display: grid; grid-gap: 20px; grid-template-columns: 1fr auto; font: 1em Helvetica, Arial, sans-serif; } .wrapper nav { writing-mode: vertical-lr; } .wrapper ul { list-style: none; margin: 0; padding: 1em; display: flex; justify-content: space-between; } .wrapper a { text-decoration: none; } ``` ```html <div class="wrapper"> <div class="content"> <p> Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus winter purslane kale. Celery potato scallion desert raisin horseradish spinach carrot soko. Lotus root water spinach fennel kombu maize bamboo shoot green bean swiss chard seakale pumpkin onion chickpea gram corn pea. Brussels sprout coriander water chestnut gourd swiss chard wakame kohlrabi beetroot carrot watercress. Corn amaranth salsify bunya nuts nori azuki bean chickweed potato bell pepper artichoke. </p> <p> Nori grape silver beet broccoli kombu beet greens fava bean potato quandong celery. Bunya nuts black-eyed pea prairie turnip leek lentil turnip greens parsnip. Sea lettuce lettuce water chestnut eggplant winter purslane fennel azuki bean earthnut pea sierra leone bologi leek soko chicory celtuce parsley jícama salsify. </p> </div> <nav> <ul> <li><a href="">Link 1</a></li> <li><a href="">Link 2</a></li> <li><a href="">Link 3</a></li> </ul> </nav> </div> ``` {{ EmbedLiveSample('Mixed_writing_modes_and_grid_layout', '500', '280') }} ## Physical values and grid layout We encounter physical properties frequently when building websites, and while the grid placement and alignment properties and values respect writing modes, there are things you may want to do with Grid that force you to use physical properties and values. In the guide to [Box alignment and grids](/en-US/docs/Web/CSS/CSS_grid_layout/Box_alignment_in_grid_layout), I demonstrated how auto margins work in a grid area. Using an auto margin to push one item away from the others is a common flexbox trick too, however this also ties the layout to the physical space. If you use absolute positioning within a grid area, then you will again be using physical offsets to push the item around inside the grid area. The key thing is to be aware of, is the tension between physical and logical properties and values. For example, be aware that you may need to make changes to your CSS to cope with a switch from `ltr` to `rtl`. ### Logical properties for everything! Our new layout methods give us the ability to use these logical values to place items, however, as soon as we start to combine them with the physical properties used for margins and padding, we need to remember that those physical properties will not change according to writing mode. The [CSS Logical Properties specification](https://drafts.csswg.org/css-logical/) means that you can use the [logical equivalents](/en-US/docs/Web/CSS/CSS_logical_properties_and_values) for properties, such as {{cssxref("margin-left")}} and {{cssxref("margin-right")}}, in your CSS. These properties and values have good support in modern browsers. Your understanding of block and inline through grid will help you to understand how to use these too.
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/grid_layout_using_named_grid_lines/index.md
--- title: Layout using named grid lines slug: Web/CSS/CSS_grid_layout/Grid_layout_using_named_grid_lines page-type: guide --- {{CSSRef}} In previous guides, we've looked at placing items by the lines created by defining grid tracks and also how to place items using named template areas. In this guide, we are going to look at how these two things work together when we use named lines. Line naming is incredibly useful, but some of the more baffling looking grid syntax comes from this combination of names and track sizes. Once you work through some examples, it should become clearer and easier to work with. ## Naming lines when defining a grid You can assign some or all of the lines in your grid a name when you define your grid with the `grid-template-rows` and `grid-template-columns` properties. To demonstrate I'll use the simple layout created in the guide on line-based placement. This time I'll create the grid using named lines. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` When defining the grid, I name my lines inside square brackets. Those names can be anything you like. I have defined a name for the start and end of the container, both for rows and columns. Then defined the center block of the grid as `content-start` and `content-end` again, both for columns and rows although you do not need to name all of the lines on your grid. You might choose to name just some key lines for your layout. ```css .wrapper { display: grid; grid-template-columns: [main-start] 1fr [content-start] 1fr [content-end] 1fr [main-end]; grid-template-rows: [main-start] 100px [content-start] 100px [content-end] 100px [main-end]; } ``` Once the lines have names, we can use the name to place the item rather than the line number. ```css .box1 { grid-column-start: main-start; grid-row-start: main-start; grid-row-end: main-end; } .box2 { grid-column-start: content-end; grid-row-start: main-start; grid-row-end: content-end; } .box3 { grid-column-start: content-start; grid-row-start: main-start; } .box4 { grid-column-start: content-start; grid-column-end: main-end; grid-row-start: content-end; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> <div class="box4">Four</div> </div> ``` {{ EmbedLiveSample('Naming_lines_when_defining_a_grid', '500', '330') }} Everything else about line-based placement still works in the same way and you can mix named lines and line numbers. Naming lines is useful when creating a responsive design where you redefine the grid, rather than then needing to redefine the content position by changing the line number in your media queries, you can ensure that the line is always named the same in your definitions. ### Giving lines multiple names You may want to give a line more than one name, perhaps it denotes the sidebar-end and the main-start for example. To do this add the names inside the square brackets with whitespace between them `[sidebar-end main-start]`. You can then refer to that line by either of the names. ## Implicit grid areas from named lines When naming the lines, I mentioned that you can name these anything you like. The name is a [custom ident](https://drafts.csswg.org/css-values-4/#custom-idents), an author-defined name. When choosing the name you need to avoid words that might appear in the specification and be confusing - such as `span`. Idents are not quoted. While you can choose any name, if you append `-start` and `-end` to the lines around an area, as I have in the example above, grid will create you a named area of the main name used. Taking the above example, I have `content-start` and `content-end` both for rows and for columns. This means I get a grid area named `content`, and could place something in that area should I wish to. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` I'm using the same grid definitions as above, however this time I am going to place a single item into the named area `content`. ```css .wrapper { display: grid; grid-template-columns: [main-start] 1fr [content-start] 1fr [content-end] 1fr [main-end]; grid-template-rows: [main-start] 100px [content-start] 100px [content-end] 100px [main-end]; } .thing { grid-area: content; } ``` ```html <div class="wrapper"> <div class="thing">I am placed in an area named content.</div> </div> ``` {{ EmbedLiveSample('Implicit_grid_areas_from_named_lines', '500', '330') }} We don't need to define where our areas are with `grid-template-areas` as our named lines have created an area for us. ## Implicit Grid lines from named areas We have seen how named lines create a named area, and this also works in reverse. Named template areas create named lines that you can use to place your items. If we take the layout created in the guide to Grid Template Areas, we can use the lines created by our areas to see how this works. In this example I have added an extra div with a class of `overlay`. We have named areas created using the `grid-area` property, then a layout created in `grid-template-areas`. The area names are: - `hd` - `ft` - `main` - `sd` This gives us column and row lines: - `hd-start` - `hd-end` - `sd-start` - `sd-end` - `main-start` - `main-end` - `ft-start` - `ft-end` You can see the named lines in the image, note that some lines have two names - for example `sd-end` and `main-start` refer to the same column line. ![An image showing the implicit line names created by our grid areas.](5_multiple_lines_from_areas.png) To position `overlay` using these implicit named lines is the same as positioning an item using lines that we have named. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(9, 1fr); grid-auto-rows: minmax(100px, auto); grid-template-areas: "hd hd hd hd hd hd hd hd hd" "sd sd sd main main main main main main" "ft ft ft ft ft ft ft ft ft"; } .header { grid-area: hd; } .footer { grid-area: ft; } .content { grid-area: main; } .sidebar { grid-area: sd; } .wrapper > div.overlay { z-index: 10; grid-column: main-start / main-end; grid-row: hd-start / ft-end; border: 4px solid rgb(92 148 13); background-color: rgb(92 148 13 / 40%); color: rgb(92 148 13); font-size: 150%; } ``` ```html <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> <div class="overlay">Overlay</div> </div> ``` {{ EmbedLiveSample('Implicit_Grid_lines_from_named_areas', '500', '330') }} Given that we have this ability to position created lines from named areas and areas from named lines, it is worth taking a little bit of time to plan your naming strategy when starting out creating your grid layout. By selecting names that will make sense to you and your team you will help everyone to use the layouts you create more easily. ## Multiple lines with the same name with repeat() If you want to give all of the lines in your grid a unique name then you will need to write out the track definition long-hand rather than using the repeat syntax, as you need to add the name in square brackets while defining the tracks. If you do use the repeat syntax you will end up with multiple lines that have the same name, however this can be very useful too. ### Twelve-column grid using repeat() In this next example I am creating a grid with twelve equal width columns. Before defining the 1fr size of the column track I am also defining a line name of `[col-start]`. This means that we will end up with a grid that has 12 column lines all named `col-start` before a `1fr` width column. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(12, [col-start] 1fr); } ``` Once you have created the grid you can place items onto it. As we have multiple lines named `col-start` if you place an item to start after line `col-start` grid uses the first line named `col-start`, in our case that will be the far left line. To address another line use the name, plus the number for that line. To place our item from the first line named col-start to the 5th, we can use: ```css .item1 { grid-column: col-start / col-start 5; } ``` You can also use the `span` keyword here. My next item will be placed from the 7th line named `col-start` and span 3 lines. ```css .item2 { grid-column: col-start 7 / span 3; } ``` ```html <div class="wrapper"> <div class="item1">I am placed from col-start line 1 to col-start 5</div> <div class="item2">I am placed from col-start line 7 spanning 3 lines</div> </div> ``` {{ EmbedLiveSample('Twelve-column_grid_using_repeat', '500', '330') }} If you take a look at this layout in the Firefox Grid Highlighter you can see how the column lines are shown, and how our items are placed against these lines. ![The 12 column grid with items placed. The Grid Highlighter shows the position of the lines.](5_named_lines1.png) ### Defining named lines with a track list The repeat syntax can also take a track list, it doesn't just need to be a single track size that is being repeated. The code below would create an eight track grid, with a narrower `1fr` width column named `col1-start` followed by a wider `3fr` column named `col2-start`. ```css .wrapper { grid-template-columns: repeat(4, [col1-start] 1fr [col2-start] 3fr); } ``` If your repeating syntax puts two lines next to each other then they will be merged, and create the same result as giving a line multiple names in a non-repeating track definition. The following definition, creates four `1fr` tracks, which each have a start and end line. ```css .wrapper { grid-template-columns: repeat(4, [col-start] 1fr [col-end]); } ``` If we write this definition out without using repeat notation it would look like this. ```css .wrapper { grid-template-columns: [col-start] 1fr [col-end col-start] 1fr [col-end col-start] 1fr [col-end col-start] 1fr [col-end]; } ``` If you have used a track list then you can use the `span` keyword not just to span a number of lines but also to span a number of lines of a certain name. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(6, [col1-start] 1fr [col2-start] 3fr); } .item1 { grid-column: col1-start / col2-start 2; } .item2 { grid-row: 2; grid-column: col1-start 2 / span 2 col1-start; } ``` ```html <div class="wrapper"> <div class="item1"> I am placed from col1-start line 1 to col2-start line 2 </div> <div class="item2"> I am placed from col1-start line 2 spanning 2 lines named col1-start </div> </div> ``` {{ EmbedLiveSample('Defining_named_lines_with_a_track_list', '500', '330') }} ### Twelve-column grid framework Over the last three guides you have discovered that there are a lot of different ways to place items using grid. This can seem a little bit overcomplicated at first, but remember you don't need to use all of them. In practice I find that for straightforward layouts, using named template areas works well, it gives that nice visual representation of what your layout looks like, and it is then easy to move things around on the grid. If working with a strict multiple column layout for example the named lines demonstration in the last part of this guide works very well. If you consider grid systems such as those found in frameworks like Foundation or Bootstrap, these are based on a 12 column grid. The framework then imports the code to do all of the calculations to make sure that the columns add up to 100%. With grid layout the only code we need for our grid "framework" is: ```css .wrapper { display: grid; gap: 10px; grid-template-columns: repeat(12, [col-start] 1fr); } ``` We can then use that framework to lay out our page. For example, to create a three column layout with a header and footer, I might have the following markup. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > * { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <header class="main-header">I am the header</header> <aside class="side1">I am sidebar 1</aside> <article class="content">I am the main article</article> <aside class="side2">I am sidebar 2</aside> <footer class="main-footer">I am the footer</footer> </div> ``` I could then place this on my grid layout framework like this. ```css .main-header, .main-footer { grid-column: col-start / span 12; } .side1 { grid-column: col-start / span 3; grid-row: 2; } .content { grid-column: col-start 4 / span 6; grid-row: 2; } .side2 { grid-column: col-start 10 / span 3; grid-row: 2; } ``` {{ EmbedLiveSample('Twelve-column_grid_framework', '500', '330') }} Once again, the grid highlighter is helpful to show us how the grid we have placed our items on works. ![The layout with the grid highlighted.](5_named_lines2.png) That's all I need. I don't need to do any calculations, grid automatically removed my 10 pixel gutter track before assigning the space to the `1fr` column tracks. As you start to build out your own layouts, you will find that the syntax becomes more familiar and you choose the ways that work best for you and the type of projects you like to build. Try building some common patterns with these various methods, and you will soon find your most productive way to work. Then, in the next guide we will look at how grid can position items for us - without us needing to use placement properties at all!
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/realizing_common_layouts_using_grids/index.md
--- title: Realizing common layouts using grids slug: Web/CSS/CSS_grid_layout/Realizing_common_layouts_using_grids page-type: guide --- {{CSSRef}} To round off this set of guides to CSS Grid Layout, we're going to walk through a few different layouts, which demonstrate some of the different techniques you can use when designing with grid layout. We will look at an example using [grid-template-areas](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_template_areas), a typical 12-column flexible grid system, and also a product listing using auto-placement. As you can see from this set of examples, there is often more than one way to achieve the result you want with grid layout. Choose the method you find most helpful for the problems that you are solving and the designs that you need to implement. ## A responsive layout with 1 to 3 fluid columns using `grid-template-areas` Many websites are a variation of this type of layout, with content, sidebars, a header and a footer. In a responsive design, you may want to display the layout as a single column, adding a sidebar at a certain breakpoint and then bring in a three-column layout for wider screens. ![Image of the three different layouts created by redefining our grid at two breakpoints.](11-responsive-areas.png) We're going to create this layout using the _named template areas_ that we learned about in the guide _[Grid template areas](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_template_areas)_. The markup is a container with elements inside for a header, footer, main content, navigation, sidebar, and a block to place advertising. ```css hidden * { box-sizing: border-box; } .wrapper { max-width: 1024px; margin: 0 auto; font: 1.2em Helvetica, arial, sans-serif; } .wrapper > * { border: 2px solid #f08c00; background-color: #ffec99; border-radius: 5px; padding: 10px; } nav ul { list-style: none; margin: 0; padding: 0; } ``` ```html <div class="wrapper"> <header class="main-head">The header</header> <nav class="main-nav"> <ul> <li><a href="">Nav 1</a></li> <li><a href="">Nav 2</a></li> <li><a href="">Nav 3</a></li> </ul> </nav> <article class="content"> <h1>Main article area</h1> <p> In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid. </p> </article> <aside class="side">Sidebar</aside> <div class="ad">Advertising</div> <footer class="main-footer">The footer</footer> </div> ``` As we are using {{cssxref("grid-template-areas")}} to create the layout. Outside of any media queries we need to name the areas. We name areas using the {{cssxref("grid-area")}} property. ```css .main-head { grid-area: header; } .content { grid-area: content; } .main-nav { grid-area: nav; } .side { grid-area: sidebar; } .ad { grid-area: ad; } .main-footer { grid-area: footer; } ``` This will not create any layout, however the items now have names we can use to do so. Staying outside of any media queries we're now going to set up the layout for the mobile width. Here we're keeping everything in source order, trying to avoid any disconnect between the source and display as described in the guide _[Grid layout and accessibility](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_and_accessibility)_. We've not defined any column or row tracks but this layout dictates a single column, and rows will be created as needed for each of the items in the implicit grid. ```css .wrapper { display: grid; gap: 20px; grid-template-areas: "header" "nav" "content" "sidebar" "ad" "footer"; } ``` With our mobile layout in place, we can now proceed to add a [media query](/en-US/docs/Web/CSS/CSS_media_queries) to adapt this layout for bigger screens with enough real estate to display two columns. ```css @media (min-width: 500px) { .wrapper { grid-template-columns: 1fr 3fr; grid-template-areas: "header header" "nav nav" "sidebar content" "ad footer"; } nav ul { display: flex; justify-content: space-between; } } ``` You can see the layout taking shape in the value of {{cssxref("grid-template-areas")}}. The `header` spans over two column tracks, as does the `nav`. In the third row track we have the `sidebar` alongside the `content`. In the fourth row track I have chosen to place my `ad` content – so it appears under the sidebar, then the `footer` next to it under the content. We're using a flexbox on the navigation to display it in a row spaced out. We can now add a final breakpoint to move to a three-column layout. ```css @media (min-width: 700px) { .wrapper { grid-template-columns: 1fr 4fr 1fr; grid-template-areas: "header header header" "nav content sidebar" "nav content ad" "footer footer footer"; } nav ul { flex-direction: column; } } ``` The three-column layout has two `1fr` unit side columns and a middle column that has `4fr` as the track size. This means that the available space in the container is split into 6 and assigned in proportion to our three tracks – one part each to the side columns and 4 parts to the center. In this layout we're displaying the `nav` in the left column, alongside the `content`. In the right column we have the `sidebar` and underneath it the advertisements (`ad`). The `footer` now spans right across the bottom of the layout. I then use a flexbox to display the navigation as a column. {{ EmbedLiveSample('A_responsive_layout_with_1_to_3_fluid_columns_using_grid-template-areas', '800', '500') }} This is a simple example but demonstrates how we can use a grid layout to rearrange our layout for different breakpoints. In particular we're changing the location of that `ad` block, as appropriate in my different column setups. I find this named areas method very helpful at a prototyping stage, it is easy to play around with the location of elements. You could always begin to use grid in this way for prototyping, even if you can't rely on it fully in production due to the browsers that visit your site. ## A flexible 12-column layout If you have been working with one of the many frameworks or grid systems you may be accustomed to laying out your site on a 12- or 16-column flexible grid. We can create this type of system using CSS Grid Layout. As a simple example, let's create a 12-column flexible grid that has 12 `1fr`-unit column tracks, they all have a start line named `col-start`. This means that we will have twelve grid lines named `col-start`. ```css hidden .wrapper { max-width: 1024px; margin: 0 auto; font: 1.2em Helvetica, arial, sans-serif; } .wrapper > * { border: 2px solid #f08c00; background-color: #ffec99; border-radius: 5px; padding: 10px; } ``` ```css .wrapper { display: grid; grid-template-columns: repeat(12, [col-start] 1fr); gap: 20px; } ``` To demonstrate how this grid system works I have four child elements inside my wrapper. ```html <div class="wrapper"> <div class="item1">Start column line 1, span 3 column tracks.</div> <div class="item2"> Start column line 6, span 4 column tracks. 2 row tracks. </div> <div class="item3">Start row 2 column line 2, span 2 column tracks.</div> <div class="item4"> Start at column line 3, span to the end of the grid (-1). </div> </div> ``` We can then place these on the grid using the named lines, and also the span keyword. ```css .item1 { grid-column: col-start / span 3; } .item2 { grid-column: col-start 6 / span 4; grid-row: 1 / 3; } .item3 { grid-column: col-start 2 / span 2; grid-row: 2; } .item4 { grid-column: col-start 3 / -1; grid-row: 3; } ``` {{ EmbedLiveSample('A_flexible_12-column_layout', '800', '400') }} As described in the [guide to named lines](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_using_named_grid_lines), we are using the named line to place our item. As we have 12 lines all with the same name we use the name, and then the index of the line. You could also use the line index itself if you prefer and avoid using named lines at all. Rather than setting the end line number, I have chosen to say how many tracks this element should span, using the `span` keyword. I like this approach as when working with a multiple-column layout system we usually think of blocks in terms of the number of tracks of the grid they span, and adjust that for different breakpoints. To see how the blocks align themselves to the tracks, use the [Firefox Grid Inspector](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html). It clearly demonstrates how our items are placed. ![Showing the items placed on the grid with grid tracks highlighted.](11-grid-inspector-12col.png) There are some key differences with how a grid layout works over the grid systems you may have used previously. As you can see, we do not need to add any markup to create a row, grid systems need to do this to stop elements popping up into the row above. With CSS Grid Layout, we can place things into rows, with no danger of them rising up into the row above if it is left empty. Due to this _strict_ column and row placement we can also easily leave white space in our layout. We also don't need special classes to pull or push things, to indent them into the grid. All we need to do is specify the start and end line for the item. ## Building a layout using the 12-column system To see how this layout method works in practice, we can create the same layout that we created with {{cssxref("grid-template-areas")}}, this time using the 12-column grid system. Let's start with the same markup as used for the grid template areas example. ```css hidden * { box-sizing: border-box; } .wrapper { max-width: 1024px; margin: 0 auto; font: 1.2em Helvetica, arial, sans-serif; } .wrapper > * { border: 2px solid #f08c00; background-color: #ffec99; border-radius: 5px; padding: 10px; } nav ul { list-style: none; margin: 0; padding: 0; } ``` ```html <div class="wrapper"> <header class="main-head">The header</header> <nav class="main-nav"> <ul> <li><a href="">Nav 1</a></li> <li><a href="">Nav 2</a></li> <li><a href="">Nav 3</a></li> </ul> </nav> <article class="content"> <h1>Main article area</h1> <p> In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid. </p> </article> <aside class="side">Sidebar</aside> <div class="ad">Advertising</div> <footer class="main-footer">The footer</footer> </div> ``` We can then set up our grid, as for the example 12-column layout above. ```css .wrapper { display: grid; grid-template-columns: repeat(12, [col-start] 1fr); gap: 20px; } ``` We are once again going to make this a responsive layout, this time however using named lines. Every breakpoint will use a 12-column grid, however the number of tracks that items will span changes depending on the size of the screen. We start mobile first, and all we want for the narrowest screens is for the items to remain in source order, and all span right across the grid. ```css .wrapper > * { grid-column: col-start / span 12; } ``` At the next breakpoint we want to move to a two-column layout. Our header and navigation still span the full grid, so we do not need to specify any positioning for them. The sidebar is starting on the first column line named col-start, spanning 3 lines. It goes after row line 3, as the header and navigation are in the first two row tracks. The ad panel is below the sidebar, so starts at grid row line 4. Then we have the content and footer starting at col-start 4 and spanning 9 tracks taking them to the end of the grid. ```css @media (min-width: 500px) { .side { grid-column: col-start / span 3; grid-row: 3; } .ad { grid-column: col-start / span 3; grid-row: 4; } .content, .main-footer { grid-column: col-start 4 / span 9; } nav ul { display: flex; justify-content: space-between; } } ``` Finally we go to the three-column version of this layout. The header continues to span right across the grid, but now the navigation moves down to become the first sidebar, with the content and then the sidebar next to it. The footer now also spans across the full layout. ```css @media (min-width: 700px) { .main-nav { grid-column: col-start / span 2; grid-row: 2 / 4; } .content { grid-column: col-start 3 / span 8; grid-row: 2 / 4; } .side { grid-column: col-start 11 / span 2; grid-row: 2; } .ad { grid-column: col-start 11 / span 2; grid-row: 3; } .main-footer { grid-column: col-start / span 12; } nav ul { flex-direction: column; } } ``` {{ EmbedLiveSample('Building_a_layout_using_the_12-column_system', '800', '450') }} Once again the [Grid Inspector](https://firefox-source-docs.mozilla.org/devtools-user/page_inspector/how_to/examine_grid_layouts/index.html) is useful to help us see how our layout has taken shape. ![Showing the layout with grid tracks highlighted by the grid inspector.](11-grid-inspector-12col-layout.png) Something to note as we create this layout is that we haven't needed to explicitly position every element on the grid at each breakpoint. We have been able to inherit the placement set up for earlier breakpoints – an advantage of working "mobile first". We are also able to take advantage of grid auto-placement. By keeping elements in a logical order, auto-placement does quite a lot of work for us in placing items onto the grid. In the final example in this guide we will create a layout that entirely relies on auto-placement. ## A product listing with auto-placement Many layouts are essentially sets of "cards" – product listings, image galleries, and so on. A grid can make it very easy to create these listings in a way that is responsive without needing to add [media queries](/en-US/docs/Web/CSS/CSS_media_queries) to make it so. In this next example I'm combining CSS Grid and Flexbox Layouts to make a simple product listing layout. The markup for my listing is an unordered list of items. Each item contains a heading, some text of varying height, and a call to action link. ```html <ul class="listing"> <li> <h2>Item One</h2> <div class="body"> <p>The content of this listing item goes here.</p> </div> <div class="cta"> <a href="">Call to action!</a> </div> </li> <li> <h2>Item Two</h2> <div class="body"> <p>The content of this listing item goes here.</p> </div> <div class="cta"> <a href="">Call to action!</a> </div> </li> <li class="wide"> <h2>Item Three</h2> <div class="body"> <p>The content of this listing item goes here.</p> <p>This one has more text than the other items.</p> <p>Quite a lot more</p> <p>Perhaps we could do something different with it?</p> </div> <div class="cta"> <a href="">Call to action!</a> </div> </li> <li> <h2>Item Four</h2> <div class="body"> <p>The content of this listing item goes here.</p> </div> <div class="cta"> <a href="">Call to action!</a> </div> </li> <li> <h2>Item Five</h2> <div class="body"> <p>The content of this listing item goes here.</p> </div> <div class="cta"> <a href="">Call to action!</a> </div> </li> </ul> ``` ```css hidden * { box-sizing: border-box; } img { max-width: 100%; display: block; } body { font: 1.2em Helvetica, arial, sans-serif; } a:link, a:visited { text-decoration: none; color: #f08c00; } h2 { background-color: #f08c00; color: #fff; text-align: center; margin: 0; padding: 20px; } ``` We are going to create a grid with a flexible number of flexible columns. I want them never to become smaller than 200 pixels, and then to share any available remaining space equally – so we always get equal width column tracks. We achieve this with the `minmax()` function in our repeat notation for track sizing. ```css .listing { list-style: none; margin: 2em; display: grid; gap: 20px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); } ``` As soon as we add this CSS, the items start to lay out as a grid. If we make the window smaller or wider the number of column tracks changes – without us needing to add breakpoints using media queries and redefine the grid. We can then tidy up the internals of the boxes using a little touch of flexbox. I set the list item to `display: flex` and the `flex-direction` to `column`. We can then use an auto margin on the `.cta` to push this bar down to the bottom of the box. ```css .listing li { border: 1px solid #ffe066; border-radius: 5px; display: flex; flex-direction: column; } .listing .cta { margin-top: auto; border-top: 1px solid #ffe066; padding: 10px; text-align: center; } .listing .body { padding: 10px; } ``` This is really one of the key reasons someone would use flexbox rather than grid, if he/she's just aligning or distributing something in a single dimension, that's a flexbox use case. {{ EmbedLiveSample('A_product_listing_with_auto-placement', '800', '900') }} ## Preventing gaps with the dense keyword This is all looking fairly complete now, however we sometimes have these cards which contain far more content than the others. It might be nice to cause those to span two tracks, and then they won't be so tall. We have a class of `wide` on my larger item, and we add a rule {{cssxref("grid-column-end")}} with a value of `span 2`. Now when grid encounters this item, it will assign it two tracks. At some breakpoints, this means that we'll get a gap in the grid – where there isn't space to lay out a two-track item. ![The layout has gaps as there is not space to lay out a two track item.](11-grid-auto-flow-sparse.png) We can cause a grid to backfill those gaps by setting {{cssxref("grid-auto-flow")}}`: dense` on the grid container. Take care when doing this however as it does take items away from their logical source order. You should only do this if your items do not have a set order – and be aware of the [issues](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_and_accessibility#visual_not_logical_re-ordering) of the tab order following the source and not your reordered display. ```html hidden <ul class="listing"> <li> <h2>Item One</h2> <div class="body"><p>The content of this listing item goes here.</p></div> <div class="cta"><a href="">Call to action!</a></div> </li> <li> <h2>Item Two</h2> <div class="body"><p>The content of this listing item goes here.</p></div> <div class="cta"><a href="">Call to action!</a></div> </li> <li class="wide"> <h2>Item Three</h2> <div class="body"> <p>The content of this listing item goes here.</p> <p>This one has more text than the other items.</p> <p>Quite a lot more</p> <p>Perhaps we could do something different with it?</p> </div> <div class="cta"><a href="">Call to action!</a></div> </li> <li> <h2>Item Four</h2> <div class="body"><p>The content of this listing item goes here.</p></div> <div class="cta"><a href="">Call to action!</a></div> </li> <li> <h2>Item Five</h2> <div class="body"><p>The content of this listing item goes here.</p></div> <div class="cta"><a href="">Call to action!</a></div> </li> </ul> ``` ```css hidden * { box-sizing: border-box; } img { max-width: 100%; display: block; } body { font: 1.2em Helvetica, arial, sans-serif; } a:link, a:visited { text-decoration: none; color: #f08c00; } h2 { background-color: #f08c00; color: #fff; text-align: center; margin: 0; padding: 20px; } .listing li { border: 1px solid #ffe066; border-radius: 5px; display: flex; flex-direction: column; } .listing .cta { margin-top: auto; border-top: 1px solid #ffe066; padding: 10px; text-align: center; } .listing .body { padding: 10px; } ``` ```css .listing { list-style: none; margin: 2em; display: grid; gap: 20px; grid-auto-flow: dense; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); } .listing .wide { grid-column-end: span 2; } ``` {{ EmbedLiveSample('Preventing_gaps_with_the_dense_keyword', '800', '900') }} This technique of using auto-placement with some rules applied to certain items is very useful, and can help you to deal with content that is being output by a CMS for example, where you have repeated items and can perhaps add a class to certain ones as they are rendered into the HTML. ## Further exploration The best way to learn to use grid layout is to continue to build examples like the ones we have covered here. Pick something that you normally build using your framework of choice, or using floats, and see if you can build it using grid. Don't forget to find examples that are impossible to build with current methods. That might mean taking inspiration from magazines or other non-web sources. Grid Layout opens up possibilities that we have not had before, we don't need to be tied to the same old layouts to use it. - [CSS grid layout](/en-US/docs/Web/CSS/CSS_grid_layout) - [CSS Layout: Grids](/en-US/docs/Learn/CSS/CSS_layout/Grids) - [A complete guide to CSS grid](https://css-tricks.com/snippets/css/complete-guide-grid/) on CSS-Tricks (2023) - [Grid by example](https://gridbyexample.com) - [CSS grid website layout examples](https://www.quackit.com/css/grid/examples/css_grid_website_layout_examples.cfm) on quackit.com
0
data/mdn-content/files/en-us/web/css/css_grid_layout
data/mdn-content/files/en-us/web/css/css_grid_layout/relationship_of_grid_layout_with_other_layout_methods/index.md
--- title: Relationship of grid layout to other layout methods slug: Web/CSS/CSS_grid_layout/Relationship_of_grid_layout_with_other_layout_methods page-type: guide --- {{CSSRef}} CSS Grid Layout has been designed to work alongside other parts of CSS, as part of a complete system for doing the layout. In this guide, I will explain how a grid fits together with other techniques you may already be using. ## Grid and flexbox The basic difference between CSS grid layout and [CSS flexbox layout](/en-US/docs/Web/CSS/CSS_flexible_box_layout) is that flexbox was designed for layout in one dimension - either a row _or_ a column. Grid was designed for two-dimensional layout - rows, and columns at the same time. The two specifications share some common features, however, and if you have already learned how to use flexbox, the similarities should help you get to grips with Grid. ### One-dimensional versus two-dimensional layout A simple example can demonstrate the difference between one- and two-dimensional layouts. In this first example, I am using flexbox to lay out a set of boxes. I have five child items in my container, and I have given the flex properties values so that they can grow and shrink from a flex-basis of 150 pixels. I have also set the {{cssxref("flex-wrap")}} property to `wrap`, so that if the space in the container becomes too narrow to maintain the flex basis, items will wrap onto a new row. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { width: 500px; display: flex; flex-wrap: wrap; } .wrapper > div { flex: 1 1 150px; } ``` {{ EmbedLiveSample('One-dimensional_versus_two-dimensional_layout', '500', '230') }} In the image, you can see that two items have wrapped onto a new line. These items are sharing the available space and not lining up underneath the items above. This is because when you wrap flex items, each new row (or column when working by column) is an independent flex line in the flex container. Space distribution happens across the flex line. A common question then is how to make those items line up. This is where you want a two-dimensional layout method: You want to control the alignment by row and column, and this is where grid comes in. ### The same layout with CSS grids In this next example, I create the same layout using Grid. This time we have three `1fr` column tracks. We do not need to set anything on the items themselves; they will lay themselves out one into each cell of the created grid. As you can see they stay in a strict grid, lining up in rows and columns. With five items, we get a gap on the end of row two. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); } ``` {{ EmbedLiveSample('The_same_layout_with_CSS_grids', '300', '170') }} A simple question to ask yourself when deciding between grid or flexbox is: - do I only need to control the layout by row _or_ column – use a flexbox - do I need to control the layout by row _and_ column – use a grid ### Content out or layout in? In addition to the one-dimensional versus two-dimensional distinction, there is another way to decide if you should use flexbox or grid for a layout. Flexbox works from the content out. An ideal use case for flexbox is when you have a set of items and want to space them out evenly in a container. You let the size of the content decide how much individual space each item takes up. If the items wrap onto a new line, they will work out their spacing based on their size and the available space _on that line_. Grid works from the layout in. When you use CSS Grid Layout you create a layout and then you place items into it, or you allow the auto-placement rules to place the items into the grid cells according to that strict grid. It is possible to create tracks that respond to the size of the content, however, they will also change the entire track. If you are using flexbox and find yourself disabling some of the flexibility, you probably need to use CSS Grid Layout. An example would be if you are setting a percentage width on a flex item to make it line up with other items in a row above. In that case, a grid is likely to be a better choice. ### Box alignment The feature of flexbox that was most exciting to many of us was that it gave us proper alignment control for the first time. It made it easy to center a box on the page. Flex items can stretch to the height of the flex container, meaning that equal height columns were possible. These were things we have wanted to do for a very long time, and have come up with all kinds of hacks to accomplish, at least visually. The alignment properties from the flexbox specification have been added to a new specification called [Box Alignment Level 3](https://drafts.csswg.org/css-align/). This means that they can be used in other specifications, including Grid Layout. In the future, they may well apply to other layout methods as well. In a later guide in this series, I'll be taking a proper look at Box Alignment and how it works in Grid Layout. For now, here is a comparison between simple examples of flexbox and grid. In the first example, which uses flexbox, I have a container with three items inside. The wrapper {{cssxref("min-height")}} is set, so it defines the height of the flex container. I have set {{cssxref("align-items")}} on the flex container to `flex-end` so the items will line up at the end of the flex container. I have also set the {{cssxref("align-self")}} property on `box1` so it will override the default and stretch to the height of the container and on `box2` so it aligns to the start of the flex container. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> </div> ``` ```css .wrapper { display: flex; align-items: flex-end; min-height: 200px; } .box1 { align-self: stretch; } .box2 { align-self: flex-start; } ``` {{ EmbedLiveSample('Box_alignment', '300', '230') }} ### Alignment in CSS Grids This second example uses a grid to create the same layout. This time we are using the box alignment properties as they apply to a grid layout. So we align to `start` and `end` rather than `flex-start` and `flex-end`. In the case of a grid layout, we are aligning the items inside their grid area. In this case that is a single grid cell, but it could be an area made up of several grid cells. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3">Three</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); align-items: end; grid-auto-rows: 200px; } .box1 { align-self: stretch; } .box2 { align-self: start; } ``` {{ EmbedLiveSample('Alignment_in_CSS_Grids', '200', '310') }} ### The `fr` unit and `flex-basis` We have already seen how the `fr` unit works to assign a proportion of available space in the grid container to our grid tracks. The `fr` unit, when combined with the {{cssxref("minmax", "minmax()")}} function can give us very similar behavior to the `flex` properties in flexbox while still enabling the creation of a layout in two dimensions. If we look back at the example where I demonstrated the difference between one and two-dimensional layouts, you can see there is a difference between the way that the two layouts work responsively. With the flex layout, if we drag our window wider and smaller, the flexbox does a nice job of adjusting the number of items in each row according to the available space. If we have a lot of space all five items can fit on one row. If we have a very narrow container we may only have space for one. In comparison, the grid version always has three column tracks. The tracks themselves will grow and shrink, but there are always three since we asked for three when defining our grid. #### Auto-filling grid tracks We can use grid to create a similar effect to flexbox, while still keeping the content arranged in strict rows and columns, by creating our track listing using repeat notation and the `auto-fill` and `auto-fit` properties. In this next example, I have used the `auto-fill` keyword in place of an integer in the repeat notation and set the track listing to 200 pixels. This means that grid will create as many 200 pixels column tracks as will fit in the container. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(auto-fill, 200px); } ``` {{ EmbedLiveSample('Auto-filling_grid_tracks', '500', '170') }} ### A flexible number of tracks This isn't quite the same as flexbox. In the flexbox example, the items are larger than the 200 pixel basis before wrapping. We can achieve the same in grid by combining `auto-fit` and the {{cssxref("minmax", "minmax()")}} function. In this next example, I create auto filled tracks with `minmax`. I want my tracks to be a minimum of 200 pixels, so I set the maximum to be `1fr`. Once the browser has worked out how many times 200 pixels will fit into the container–also taking account of grid gaps–it will treat the `1fr` maximum as an instruction to share out the remaining space between the items. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div>One</div> <div>Two</div> <div>Three</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); } ``` {{ EmbedLiveSample('A_flexible_number_of_tracks', '500', '170') }} With grid layout, we can create a grid with a dynamic number of flexible tracks and have the items laid out on the grid aligned by rows and columns. ## Grid and absolutely positioned elements Grid interacts with absolutely positioned elements, which can be useful if you want to position an item inside a grid or grid area. The specification defines the behavior when a grid container is a containing block and a parent of the absolutely positioned item. ### A grid container as containing block To make the grid container a containing block you need to add the position property to the container with a value of relative, just as you would make a containing block for any other absolutely positioned items. Once you have done this, if you give a grid item `position: absolute` it will take as its containing block the grid container or, if the item also has a grid position, the area of the grid it is placed into. In the below example I have a wrapper containing four child items. Item three is absolutely positioned and also placed on the grid using line-based placement. The grid container has `position: relative` and so becomes the positioning context of this item. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3"> This block is absolutely positioned. In this example the grid container is the containing block and so the absolute positioning offset values are calculated in from the outer edges of the area it has been placed into. </div> <div class="box4">Four</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 200px; gap: 20px; position: relative; } .box3 { grid-column-start: 2; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; position: absolute; top: 40px; left: 40px; } ``` {{ EmbedLiveSample('A_grid_container_as_containing_block', '500', '330') }} You can see that the item is taking the area from grid column line 2 to 4, and starting after line 1. Then it is offset in that area using the top and left properties. However, it has been taken out of flow as is usual for absolutely positioned items and so the auto-placement rules now place items into the same space. The item also doesn't cause the additional row to be created to span to row line 3. If we remove `position: absolute` from the rules for `.box3` you can see how it would display without the positioning. ### A grid container as parent If the absolutely positioned child has a grid container as a parent but that container does not create a new positioning context, then it is taken out of flow as in the previous example. The positioning context will be whatever element creates a positioning context as is common to other layout methods. In our case, if we remove `position: relative` from the wrapper above, positioning context is from the viewport, as shown in this image. ![Image of grid container as parent](2_abspos_example.png) Once again the item no longer participates in the grid layout in terms of sizing or when other items are auto-placed. ### With a grid area as the parent If the absolutely positioned item is nested inside a grid area then you can create a positioning context on that area. In the below example we have our grid as before but this time I have nested an item inside `.box3` of the grid. I have given `.box3` position relative and then positioned the sub-item with the offset properties. In this case, the positioning context is the grid area. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } ``` ```html <div class="wrapper"> <div class="box1">One</div> <div class="box2">Two</div> <div class="box3"> Three <div class="abspos"> This block is absolutely positioned. In this example the grid area is the containing block and so the absolute positioning offset values are calculated in from the outer edges of the grid area. </div> </div> <div class="box4">Four</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 200px; gap: 20px; } .box3 { grid-column-start: 2; grid-column-end: 4; grid-row-start: 1; grid-row-end: 3; position: relative; } .abspos { position: absolute; top: 40px; left: 40px; background-color: rgb(255 255 255 / 50%); border: 1px solid rgb(0 0 0 / 50%); color: #000; padding: 10px; } ``` {{ EmbedLiveSample('With_a_grid_area_as_the_parent', '500', '460') }} ## Grid and display: contents A final interaction with another layout specification that is worth noting is the interaction between CSS Grid Layout and `display: contents`. The `contents` value of the display property is a new value that is described in the [Display specification](https://drafts.csswg.org/css-display/#box-generation) as follows: > "The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced with its children and pseudo-elements in the document tree." If you set an item to `display: contents`, the box it would normally create disappears and the boxes of the child elements appear as if they have risen up a level. This means that children of a grid item can become grid items. Sound odd? Here is a simple example. ### Grid layout with nested child elements In the following markup, I have a grid and the first item on the grid is set to span all three column tracks. It contains three nested items. As these items are not direct children, they don't become part of the grid layout and so display using regular block layout. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .box { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } .nested { border: 2px solid #ffec99; border-radius: 5px; background-color: #fff9db; padding: 1em; } ``` ```html <div class="wrapper"> <div class="box box1"> <div class="nested">a</div> <div class="nested">b</div> <div class="nested">c</div> </div> <div class="box box2">Two</div> <div class="box box3">Three</div> <div class="box box4">Four</div> <div class="box box5">Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: minmax(100px, auto); } .box1 { grid-column-start: 1; grid-column-end: 4; } ``` {{ EmbedLiveSample('Grid_layout_with_nested_child_elements', '400', '440') }} ### Using display: contents If I now add `display: contents` to the rules for `box1`, the box for that item vanishes and the sub-items now become grid items and lay themselves out using the auto-placement rules. ```css hidden * { box-sizing: border-box; } .wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; } .box { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; } .nested { border: 2px solid #ffec99; border-radius: 5px; background-color: #fff9db; padding: 1em; } ``` ```html <div class="wrapper"> <div class="box box1"> <div class="nested">a</div> <div class="nested">b</div> <div class="nested">c</div> </div> <div class="box box2">Two</div> <div class="box box3">Three</div> <div class="box box4">Four</div> <div class="box box5">Five</div> </div> ``` ```css .wrapper { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: minmax(100px, auto); } .box1 { grid-column-start: 1; grid-column-end: 4; display: contents; } ``` {{ EmbedLiveSample('Using_display_contents', '400', '350') }} This can be a way to get items nested into the grid to act as if they are part of the grid, and is a way around some of the issues that would be solved by subgrids once they are implemented. You can also use `display: contents` in a similar way with flexbox to enable nested items to become flex items. As you can see from this guide, CSS Grid Layout is just one part of your toolkit. Don't be afraid to mix it with other methods of doing layout to get the different effects you need. ## See also - [Flexbox guides](/en-US/docs/Learn/CSS/CSS_layout/Flexbox) - [Multiple-column layout guides](/en-US/docs/Web/CSS/CSS_multicol_layout)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_colon_only-of-type/index.md
--- title: ":only-of-type" slug: Web/CSS/:only-of-type page-type: css-pseudo-class browser-compat: css.selectors.only-of-type --- {{CSSRef}} The **`:only-of-type`** CSS [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) represents an element that has no siblings of the same type. {{EmbedInteractiveExample("pages/tabbed/pseudo-class-only-of-type.html", "tabbed-shorter")}} ## Syntax ```css :only-of-type { /* ... */ } ``` ## Examples ### Styling elements with no siblings of the same type #### HTML ```html <main> <div>I am `div` #1.</div> <p>I am the only `p` among my siblings.</p> <div>I am `div` #2.</div> <div> I am `div` #3. <i>I am the only `i` child.</i> <em>I am `em` #1.</em> <em>I am `em` #2.</em> </div> </main> ``` #### CSS ```css main :only-of-type { color: red; } ``` #### Result {{EmbedLiveSample('Styling_elements_with_no_siblings_of_the_same_type','100%',180)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{Cssxref(":only-child")}} - {{Cssxref(":first-of-type")}} - {{Cssxref(":last-of-type")}} - {{Cssxref(":nth-of-type")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_doublecolon_target-text/index.md
--- title: "::target-text" slug: Web/CSS/::target-text page-type: css-pseudo-element status: - experimental browser-compat: css.selectors.target-text --- {{CSSRef}}{{SeeCompatTable}} The **`::target-text`** [CSS](/en-US/docs/Web/CSS) [pseudo-element](/en-US/docs/Web/CSS/Pseudo-elements) represents the text that has been scrolled to if the browser supports [text fragments](/en-US/docs/Web/Text_fragments). It allows authors to choose how to highlight that section of text. ```css ::target-text { background-color: pink; } ``` ## Syntax ```css ::target-text { /* ... */ } ``` ## Examples ### Highlighting text fragments ```css ::target-text { background-color: rebeccapurple; color: white; } ``` To see this CSS in action follow the link to [scroll-to-text demo](https://mdn.github.io/css-examples/target-text/index.html#:~:text=From%20the%20foregoing%20remarks%20we%20may%20gather%20an%20idea%20of%20the%20importance). ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Text fragments](/en-US/docs/Web/Text_fragments)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/max-height/index.md
--- title: max-height slug: Web/CSS/max-height page-type: css-property browser-compat: css.properties.max-height --- {{CSSRef}} The **`max-height`** [CSS](/en-US/docs/Web/CSS) property sets the maximum height of an element. It prevents the [used value](/en-US/docs/Web/CSS/used_value) of the {{cssxref("height")}} property from becoming larger than the value specified for `max-height`. {{EmbedInteractiveExample("pages/css/max-height.html")}} `max-height` overrides {{cssxref("height")}}, but {{cssxref("min-height")}} overrides `max-height`. ## Syntax ```css /* <length> value */ max-height: 3.5em; /* <percentage> value */ max-height: 75%; /* Keyword values */ max-height: none; max-height: max-content; max-height: min-content; max-height: fit-content; max-height: fit-content(20em); /* Global values */ max-height: inherit; max-height: initial; max-height: revert; max-height: revert-layer; max-height: unset; ``` ### Values - {{cssxref("&lt;length&gt;")}} - : Defines the `max-height` as an absolute value. - {{cssxref("&lt;percentage&gt;")}} - : Defines the `max-height` as a percentage of the containing block's height. - `none` - : No limit on the size of the box. - `max-content` - : The intrinsic preferred `max-height`. - `min-content` - : The intrinsic minimum `max-height`. - `fit-content` - : Use the available space, but not more than [max-content](/en-US/docs/Web/CSS/max-content), i.e `min(max-content, max(min-content, stretch))`. - `fit-content({{cssxref("&lt;length-percentage&gt;")}})` {{Experimental_Inline}} - : Uses the `fit-content` formula with the available space replaced by the specified argument, i.e. `min(max-content, max(min-content, argument))`. ## Accessibility concerns Ensure that elements set with a `max-height` are not truncated and/or do not obscure other content when the page is zoomed to increase text size. - [MDN Understanding WCAG, Guideline 1.4 explanations](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable#guideline_1.4_make_it_easier_for_users_to_see_and_hear_content_including_separating_foreground_from_background) - [Understanding Success Criterion 1.4.4 | W3C Understanding WCAG 2.0](https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-scale.html) ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting max-height using percentage and keyword values ```css table { max-height: 75%; } form { max-height: none; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [The box model](/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model), {{cssxref("box-sizing")}} - {{Cssxref("height")}}, {{Cssxref("min-height")}} - The mapped logical properties: {{cssxref("max-inline-size")}}, {{cssxref("max-block-size")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/border-left-color/index.md
--- title: border-left-color slug: Web/CSS/border-left-color page-type: css-property browser-compat: css.properties.border-left-color --- {{CSSRef}} The **`border-left-color`** CSS property sets the color of an element's left [border](/en-US/docs/Web/CSS/border). It can also be set with the shorthand CSS properties {{cssxref("border-color")}} or {{cssxref("border-left")}}. {{EmbedInteractiveExample("pages/css/border-left-color.html")}} ## Syntax ```css /* <color> values */ border-left-color: red; border-left-color: #ffbb00; border-left-color: rgb(255 0 0); border-left-color: hsl(100deg 50% 25% / 75%); border-left-color: currentcolor; border-left-color: transparent; /* Global values */ border-left-color: inherit; border-left-color: initial; border-left-color: revert; border-left-color: revert-layer; border-left-color: unset; ``` The `border-left-color` property is specified as a single value. ### Values - {{cssxref("&lt;color&gt;")}} - : The color of the left border. ## Formal definition {{CSSInfo}} ## Formal syntax {{csssyntax}} ## Examples ### A simple div with a border #### HTML ```html <div class="mybox"> <p> This is a box with a border around it. Note which side of the box is <span class="redtext">red</span>. </p> </div> ``` #### CSS ```css .mybox { border: solid 0.3em gold; border-left-color: red; width: auto; } .redtext { color: red; } ``` #### Result {{EmbedLiveSample('A_simple_div_with_a_border')}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - The border-related CSS shorthand properties: {{Cssxref("border")}}, {{Cssxref("border-left")}}, and {{Cssxref("border-color")}}. - The color-related CSS properties for the other borders: {{Cssxref("border-right-color")}}, {{Cssxref("border-bottom-color")}}, and {{Cssxref("border-top-color")}}. - The other border-related CSS properties applying to the same border: {{cssxref("border-left-style")}} and {{cssxref("border-left-width")}}. - The default [`currentcolor`](/en-US/docs/Web/CSS/color_value#currentcolor_keyword) color value.
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/scroll-snap-type/index.md
--- title: scroll-snap-type slug: Web/CSS/scroll-snap-type page-type: css-property browser-compat: css.properties.scroll-snap-type --- {{CSSRef}} The **`scroll-snap-type`** [CSS](/en-US/docs/Web/CSS) property sets how strictly snap points are enforced on the scroll container in case there is one. {{EmbedInteractiveExample("pages/css/scroll-snap-type.html")}} Specifying any precise animations or physics used to enforce those snap points is not covered by this property but instead left up to the user agent. ## Syntax ```css /* No snapping */ scroll-snap-type: none; /* Keyword values for snap axes */ scroll-snap-type: x; scroll-snap-type: y; scroll-snap-type: block; scroll-snap-type: inline; scroll-snap-type: both; /* Optional keyword values for snap strictness */ /* mandatory | proximity */ scroll-snap-type: x mandatory; scroll-snap-type: y proximity; scroll-snap-type: both mandatory; /* Global values */ scroll-snap-type: inherit; scroll-snap-type: initial; scroll-snap-type: revert; scroll-snap-type: revert-layer; scroll-snap-type: unset; ``` ### Values - `none` - : When the visual {{Glossary("viewport")}} of this scroll container is scrolled, it must ignore snap points. - `x` - : The scroll container snaps to snap positions in its horizontal axis only. - `y` - : The scroll container snaps to snap positions in its vertical axis only. - `block` - : The scroll container snaps to snap positions in its block axis only. - `inline` - : The scroll container snaps to snap positions in its inline axis only. - `both` - : The scroll container snaps to snap positions in both of its axes independently (potentially snapping to different elements in each axis). - `mandatory` - : The visual viewport of this scroll container must snap to a snap position if it isn't currently scrolled. - `proximity` - : The visual viewport of this scroll container may snap to a snap position if it isn't currently scrolled. The user agent decides if it snaps or not based on scroll parameters. This is the default snap strictness if any snap axis is specified. > **Note:** If the content in the snap port is changed (e.g. added, moved, deleted, or resized) or the value of any scroll snap-related property (e.g. `scroll-snap-type` or `scroll-margin`) is changed, the scroll container will be [resnapped](https://drafts.csswg.org/css-scroll-snap/#re-snap) according to the latest value of `scroll-snap-type`. ## Formal definition {{CSSInfo}} ## Formal syntax {{csssyntax}} ## Examples ### Snapping in different axes #### HTML ```html <div class="holster"> <div class="container x mandatory-scroll-snapping" dir="ltr"> <div>X Mand. LTR</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <div class="container x proximity-scroll-snapping" dir="ltr"> <div>X Prox. LTR</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <div class="container y mandatory-scroll-snapping" dir="ltr"> <div>Y Mand. LTR</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <div class="container y proximity-scroll-snapping" dir="ltr"> <div>Y Prox. LTR</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <div class="container x mandatory-scroll-snapping" dir="rtl"> <div>X Mand. RTL</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <div class="container x proximity-scroll-snapping" dir="rtl"> <div>X Prox. RTL</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <div class="container y mandatory-scroll-snapping" dir="rtl"> <div>Y Mand. RTL</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <div class="container y proximity-scroll-snapping" dir="rtl"> <div>Y Prox. RTL</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> </div> ``` #### CSS ```css hidden .holster { display: flex; align-items: center; justify-content: space-between; flex-flow: column nowrap; font-family: monospace; } .container { display: flex; margin: 1em auto; outline: 1px dashed lightgray; flex: none; overflow: auto; } .container.x { width: 100%; height: 128px; flex-flow: row nowrap; overflow-y: hidden; } .container.y { width: 256px; height: 256px; flex-flow: column nowrap; overflow-x: hidden; } ``` ```css /* scroll-snap */ .x.mandatory-scroll-snapping { scroll-snap-type: x mandatory; } .x.proximity-scroll-snapping { scroll-snap-type: x proximity; } .y.mandatory-scroll-snapping { scroll-snap-type: y mandatory; } .y.proximity-scroll-snapping { scroll-snap-type: y proximity; } .container > div { text-align: center; scroll-snap-align: center; flex: none; } ``` ```css hidden .x.container > div { line-height: 128px; font-size: 64px; width: 100%; height: 128px; } .y.container > div { line-height: 256px; font-size: 128px; width: 256px; height: 100%; } /* appearance fixes */ .y.container > div:first-child { line-height: 1.3; font-size: 64px; } /* coloration */ .container > div:nth-child(even) { background-color: #87ea87; } .container > div:nth-child(odd) { background-color: #87ccea; } ``` #### Results {{EmbedLiveSample("snapping_in_different_axes", "100%", 1800)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS scroll snap](/en-US/docs/Web/CSS/CSS_scroll_snap) - [Well-controlled scrolling with CSS scroll snap](https://web.dev/articles/css-scroll-snap)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_doublecolon_-webkit-meter-bar/index.md
--- title: "::-webkit-meter-bar" slug: Web/CSS/::-webkit-meter-bar page-type: css-pseudo-element status: - non-standard browser-compat: css.selectors.-webkit-meter-bar --- {{CSSRef}}{{Non-standard_header}} The **`::-webkit-meter-bar`** [CSS](/en-US/docs/Web/CSS) [pseudo-element](/en-US/docs/Glossary/Pseudo-element) is a [WebKit extension](/en-US/docs/Web/CSS/WebKit_Extensions) that represents the meter bar in a {{HTMLElement("meter")}} element. It is used for selecting and applying styles to the container meter gauge. ## Syntax ```css ::-webkit-meter-bar { /* ... */ } ``` ## Specifications Not part of any standard. ## Examples ### HTML ```html Normal: <meter min="0" max="10" value="6">Score 6/10</meter> <br /> Styled: <meter id="styled" min="0" max="10" value="6">Score 6/10</meter> ``` ### CSS ```css body { font-family: monospace; } .safari meter { /* Reset the default appearance for Safari only */ /* .safari class is added via JavaScript */ -webkit-appearance: none; } #styled::-webkit-meter-bar { background: #eee; box-shadow: 0 2px 3px rgb(0 0 0 / 20%) inset; border-radius: 3px; } ``` ### JavaScript ```js // Safari requires <meter> elements to have an `appearance` of `none` for custom styling // using `::-webkit-meter-*` selectors, but `appearance: none` breaks rendering on Chrome. // Therefore, we must check if the browser is Safari-based. const is_safari = navigator.userAgent.includes("AppleWebKit/") && !navigator.userAgent.includes("Chrome/"); if (is_safari) { document.body.classList.add("safari"); } ``` ### Result {{ EmbedLiveSample('Examples') }} ## Browser compatibility {{Compat}} ## See also - The pseudo-elements used by WebKit/Blink to style other parts of a {{htmlelement("meter")}} element are as follows: - {{cssxref("::-webkit-meter-inner-element")}} - {{cssxref("::-webkit-meter-even-less-good-value")}} - {{cssxref("::-webkit-meter-optimum-value")}} - {{cssxref("::-webkit-meter-suboptimum-value")}} - [WebKit CSS extensions](/en-US/docs/Web/CSS/WebKit_Extensions)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_colon_nth-last-of-type/index.md
--- title: ":nth-last-of-type()" slug: Web/CSS/:nth-last-of-type page-type: css-pseudo-class browser-compat: css.selectors.nth-last-of-type --- {{CSSRef}} The **`:nth-last-of-type()`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) matches elements based on their position among siblings of the same type (tag name), counting from the end. {{EmbedInteractiveExample("pages/tabbed/pseudo-class-nth-last-of-type.html", "tabbed-shorter")}} ## Syntax The `nth-last-of-type` pseudo-class is specified with a single argument, which represents the pattern for matching elements, counting from the end. See {{Cssxref(":nth-last-child")}} for a more detailed explanation of its syntax. ```css-nolint :nth-last-of-type(<an-plus-b> | even | odd) { /* ... */ } ``` ## Examples ### HTML ```html <div> <span>This is a span.</span> <span>This is another span.</span> <em>This is emphasized.</em> <span>Wow, this span gets limed!!!</span> <del>This is struck through.</del> <span>Here is one last span.</span> </div> ``` ### CSS ```css span:nth-last-of-type(2) { background-color: lime; } ``` ### Result {{EmbedLiveSample('Examples')}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{Cssxref(":nth-last-child")}}, {{Cssxref(":nth-of-type")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/font-family/index.md
--- title: font-family slug: Web/CSS/font-family page-type: css-property browser-compat: css.properties.font-family --- {{CSSRef}} The **`font-family`** CSS property specifies a prioritized list of one or more font family names and/or generic family names for the selected element. {{EmbedInteractiveExample("pages/css/font-family.html")}} Values are separated by commas to indicate that they are alternatives. The browser will select the first font in the list that is installed or that can be downloaded using a {{CSSxRef("@font-face")}} at-rule. It is often convenient to use the shorthand property {{CSSxRef("font")}} to set `font-size` and other font related properties all at once. You should always include at least one generic family name in a `font-family` list, since there's no guarantee that any given font is available. This lets the browser select an acceptable fallback font when necessary. The `font-family` property specifies a list of fonts, from highest priority to lowest. Font selection _does not_ stop at the first font in the list that is on the user's system. Rather, font selection is done _one character at a time_, so that if an available font does not have a glyph for a needed character, the latter fonts are tried. When a font is only available in some [styles](/en-US/docs/Web/CSS/font-style), [variants](/en-US/docs/Web/CSS/font-variant), or [sizes](/en-US/docs/Web/CSS/font-size), those properties may also influence which font family is chosen. ## Syntax ```css /* A font family name and a generic family name */ font-family: "Gill Sans Extrabold", sans-serif; font-family: "Goudy Bookletter 1911", sans-serif; /* A generic family name only */ font-family: serif; font-family: sans-serif; font-family: monospace; font-family: cursive; font-family: fantasy; font-family: system-ui; font-family: ui-serif; font-family: ui-sans-serif; font-family: ui-monospace; font-family: ui-rounded; font-family: emoji; font-family: math; font-family: fangsong; /* Global values */ font-family: inherit; font-family: initial; font-family: revert; font-family: revert-layer; font-family: unset; ``` The `font-family` property lists one or more font families, separated by commas. Each font family is specified as either a `<family-name>` or a `<generic-name>` value. The example below lists two font families, the first with a `<family-name>` and the second with a `<generic-name>`: ```css font-family: "Gill Sans Extrabold", sans-serif; ``` ### Values - `<family-name>` - : The name of a font family. For example, "Times" and "Helvetica" are font families. Font family names containing whitespace should be quoted. For example: "Comic Sans MS". - `<generic-name>` - : Generic font families are a fallback mechanism, a means of preserving some of the style sheet author's intent when none of the specified fonts are available. Generic family names are keywords and must not be quoted. A generic font family should be the last item in the list of font family names. The following keywords are defined: - `serif` - : Glyphs have finishing strokes, flared or tapering ends, or have actual serifed endings. For example: Lucida Bright, Lucida Fax, Palatino, Palatino Linotype, Palladio, URW Palladio, serif. - `sans-serif` - : Glyphs have stroke endings that are plain. For example: Open Sans, Fira Sans, Lucida Sans, Lucida Sans Unicode, Trebuchet MS, Liberation Sans, Nimbus Sans L, sans-serif. - `monospace` - : All glyphs have the same fixed width. For example: Fira Mono, DejaVu Sans Mono, Menlo, Consolas, Liberation Mono, Monaco, Lucida Console, monospace. - `cursive` - : Glyphs in cursive fonts generally have either joining strokes or other cursive characteristics beyond those of italic typefaces. The glyphs are partially or completely connected, and the result looks more like handwritten pen or brush writing than printed letter work. For example: Brush Script MT, Brush Script Std, Lucida Calligraphy, Lucida Handwriting, Apple Chancery, cursive. - `fantasy` - : Fantasy fonts are primarily decorative fonts that contain playful representations of characters. For example: Papyrus, Herculanum, Party LET, Curlz MT, Harrington, fantasy. - `system-ui` - : Glyphs are taken from the default user interface font on a given platform. Because typographic traditions vary widely across the world, this generic is provided for typefaces that don't map cleanly into the other generics. - `ui-serif` - : The default user interface serif font. - `ui-sans-serif` - : The default user interface sans-serif font. - `ui-monospace` - : The default user interface monospace font. - `ui-rounded` - : The default user interface font that has rounded features. - `math` - : This is for the particular stylistic concerns of representing mathematics: superscript and subscript, brackets that cross several lines, nesting expressions, and double struck glyphs with distinct meanings. - `emoji` - : Fonts that are specifically designed to render emoji. - `fangsong` - : A particular style of Chinese characters that are between serif-style Song and cursive-style Kai forms. This style is often used for government documents. ### Valid family names Font family names must either be given quoted as strings, or unquoted as a sequence of one or more identifiers. This means that punctuation characters and digits at the start of each token must be escaped in unquoted font family names. It is a **good practice** to quote font family names that contain white space, digits, or punctuation characters other than hyphens. For example, the following declarations are valid: ```css example-good font-family: "Goudy Bookletter 1911", sans-serif; ``` The following declarations are **invalid**: ```css-nolint example-bad font-family: Goudy Bookletter 1911, sans-serif; font-family: Red/Black, sans-serif; font-family: "Lucida" Grande, sans-serif; font-family: Ahem!, sans-serif; font-family: test@foo, sans-serif; font-family: #POUND, sans-serif; font-family: Hawaii 5-0, sans-serif; ``` The following example is technically **valid** but is not recommended: ```css font-family: Gill Sans Extrabold, sans-serif; ``` ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Some common font families ```css .serif { font-family: Times, "Times New Roman", Georgia, serif; } .sansserif { font-family: Verdana, Arial, Helvetica, sans-serif; } .monospace { font-family: "Lucida Console", Courier, monospace; } .cursive { font-family: cursive; } .fantasy { font-family: fantasy; } .emoji { font-family: emoji; } .math { font-family: math; } .fangsong { font-family: fangsong; } ``` ```html hidden <div class="serif">This is an example of a serif font.</div> <div class="sansserif">This is an example of a sans-serif font.</div> <div class="monospace">This is an example of a monospace font.</div> <div class="cursive">This is an example of a cursive font.</div> <div class="fantasy">This is an example of a fantasy font.</div> <div class="math">This is an example of a math font.</div> <div class="emoji">This is an example of an emoji font.</div> <div class="fangsong">This is an example of a fangsong font.</div> ``` {{EmbedLiveSample("Some_common_font_families", 600, 220)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{cssxref("font-style")}} - {{cssxref("font-weight")}} - [Fundamental text and font styling](/en-US/docs/Learn/CSS/Styling_text/Fundamentals)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/box-orient/index.md
--- title: box-orient slug: Web/CSS/box-orient page-type: css-property status: - deprecated - non-standard browser-compat: css.properties.box-orient --- {{CSSRef}}{{Non-standard_header}}{{Deprecated_Header}} > **Warning:** This is a property of the original CSS Flexible Box Layout Module draft, and has been replaced by a newer standard. See [flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox) for information about the current standard. The **`box-orient`** [CSS](/en-US/docs/Web/CSS) property sets whether an element lays out its contents horizontally or vertically. ## Syntax ```css /* Keyword values */ box-orient: horizontal; box-orient: vertical; box-orient: inline-axis; box-orient: block-axis; /* Global values */ box-orient: inherit; box-orient: initial; box-orient: unset; ``` The `box-orient` property is specified as one of the keyword values listed below. ### Values - `horizontal` - : The box lays out its contents horizontally. - `vertical` - : The box lays out its contents vertically. - `inline-axis` (HTML) - : The box displays its children along the inline axis. - `block-axis` (HTML) - : The box displays its children along the block axis. The inline and block axes are the writing-mode dependent keywords which, in English, map to `horizontal` and `vertical` respectively. ## Description HTML DOM elements lay out their contents along the inline-axis by default. This CSS property will only apply to HTML elements with a CSS {{CSSxRef("display")}} value of `box` or `inline-box`. ## Formal definition {{cssinfo}} ## Formal syntax ```plain box-orient = horizontal | vertical | inline-axis | block-axis | inherit ``` ## Examples ### Setting horizontal box orientation Here, the `box-orient` property will cause the two {{HTMLElement("p")}} sections in the example to display in the same line. #### HTML ```html <div class="example"> <p>I will be to the left of my sibling.</p> <p>I will be to the right of my sibling.</p> </div> ``` #### CSS ```css div.example { display: -moz-box; /* Mozilla */ display: -webkit-box; /* WebKit */ display: box; /* As specified */ /* Children should be oriented vertically */ -moz-box-orient: horizontal; /* Mozilla */ -webkit-box-orient: horizontal; /* WebKit */ box-orient: horizontal; /* As specified */ } ``` #### Result {{ EmbedLiveSample('Setting horizontal box orientation', '', 100) }} ## Specifications Not part of any standard. ## Browser compatibility {{Compat}} ## See also - {{CSSxRef("box-direction")}} - {{CSSxRef("box-pack")}} - {{CSSxRef("box-align")}} - {{CSSxRef("flex-direction")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/comments/index.md
--- title: Comments slug: Web/CSS/Comments page-type: guide --- {{CSSRef}} A CSS **comment** is used to add explanatory notes to the code or to prevent the browser from interpreting specific parts of the style sheet. By design, comments have no effect on the layout of a document. ## Syntax Comments can be placed wherever white space is allowed within a style sheet. They can be used on a single line, or traverse multiple lines. ```css /* Comment */ ``` ## Examples ```css /* A one-line comment */ /* A comment which stretches over several lines */ /* The comment below is used to disable specific styling */ /* span { color: blue; font-size: 1.5em; } */ ``` ## Notes The `/* */` comment syntax is used for both single and multiline comments. There is no other way to specify comments in external style sheets. However, when using the {{htmlelement("style")}} element, you may use `<!-- -->` to hide CSS from older browsers, although this is not recommended. As with most programming languages that use the `/* */` comment syntax, comments cannot be nested. In other words, the first instance of `*/` that follows an instance of `/*` closes the comment. ## Specifications - [CSS 2.1 Syntax and basic data types #comments](https://www.w3.org/TR/CSS21/syndata.html#comments) ## See also - CSS key concepts: - [CSS syntax](/en-US/docs/Web/CSS/Syntax) - [At-rules](/en-US/docs/Web/CSS/At-rule) - [Specificity](/en-US/docs/Web/CSS/Specificity) - [Inheritance](/en-US/docs/Web/CSS/Inheritance) - [Box model](/en-US/docs/Web/CSS/CSS_box_model/Introduction_to_the_CSS_box_model) - [Layout modes](/en-US/docs/Web/CSS/Layout_mode) - [Visual formatting models](/en-US/docs/Web/CSS/Visual_formatting_model) - [Margin collapsing](/en-US/docs/Web/CSS/CSS_box_model/Mastering_margin_collapsing) - Values - [Initial values](/en-US/docs/Web/CSS/initial_value) - [Computed values](/en-US/docs/Web/CSS/computed_value) - [Used values](/en-US/docs/Web/CSS/used_value) - [Actual values](/en-US/docs/Web/CSS/actual_value) - [Value definition syntax](/en-US/docs/Web/CSS/Value_definition_syntax) - [Shorthand properties](/en-US/docs/Web/CSS/Shorthand_properties) - [Replaced elements](/en-US/docs/Web/CSS/Replaced_element)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/border-block-color/index.md
--- title: border-block-color slug: Web/CSS/border-block-color page-type: css-property browser-compat: css.properties.border-block-color --- {{CSSRef}} The **`border-block-color`** [CSS](/en-US/docs/Web/CSS) property defines the color of the logical block borders of an element, which maps to a physical border color depending on the element's writing mode, directionality, and text orientation. It corresponds to the {{cssxref("border-top-color")}} and {{cssxref("border-bottom-color")}}, or {{cssxref("border-right-color")}} and {{cssxref("border-left-color")}} property depending on the values defined for {{cssxref("writing-mode")}}, {{cssxref("direction")}}, and {{cssxref("text-orientation")}}. {{EmbedInteractiveExample("pages/css/border-block-color.html")}} The border color in the other dimension can be set with {{cssxref("border-inline-color")}} which sets {{cssxref("border-inline-start-color")}}, and {{cssxref("border-inline-end-color")}}. ## Syntax ```css border-block-color: yellow; border-block-color: #f5f6f7; /* Global values */ border-block-color: inherit; border-block-color: initial; border-block-color: revert; border-block-color: revert-layer; border-block-color: unset; ``` ### Values - {{CSSXref("&lt;color&gt;")}} - : The color of the border. ## Formal definition {{CSSInfo}} ## Formal syntax {{csssyntax}} ## Examples ### Border with vertical text #### HTML ```html <div> <p class="exampleText">Example text</p> </div> ``` #### CSS ```css div { background-color: yellow; width: 120px; height: 120px; } .exampleText { writing-mode: vertical-lr; border: 10px solid blue; border-block-color: red; } ``` #### Results {{EmbedLiveSample("Border_with_vertical_text", 140, 140)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS Logical Properties and Values](/en-US/docs/Web/CSS/CSS_logical_properties_and_values) - This property maps to the physical border properties: {{cssxref("border-top-color")}}, {{cssxref("border-right-color")}}, {{cssxref("border-bottom-color")}}, or {{cssxref("border-left-color")}}. - {{cssxref("writing-mode")}}, {{cssxref("direction")}}, {{cssxref("text-orientation")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/at-rule-functions/index.md
--- title: CSS at-rule functions slug: Web/CSS/At-rule-functions page-type: guide --- {{CSSRef}} **[CSS](/en-US/docs/Web/CSS) [at-rule](/en-US/docs/Web/CSS/At-rule) functions** are at-rule statements that represent complex rules or can invoke special data processing or calculations. ## Syntax ```css @identifier function([argument]? [, argument]!) { } ``` The syntax begins with the at symbol `@` and an at-rule identifier, such as `import`. This is followed by the **name of the at-rule function**, such as `url`, followed by a pair of opening and closing parentheses. One or more arguments are specified inside the parentheses. Some at-rule functions can take multiple arguments, which are formatted similar to CSS property values. White space is allowed, but it is optional inside the parentheses. Multiple arguments can be separated using a comma or a space. ## @import functions The {{CSSxRef("@import")}} at-rule is used to import styles from other stylesheets. - {{CSSxRef("@import", "@import url()")}} - : Imports a stylesheet file from the specified URL. - {{CSSxRef("@import", "@import src()")}} - : Imports a stylesheet file from the specified source. - {{CSSxRef("@import", "@import supports()")}} - : Imports a stylesheet file based on browser support. - {{CSSxRef("@import", "@import layer()")}} - : Imports a stylesheet file into the specified cascade layer. ## @supports functions The {{CSSxRef("@supports")}} at-rule checks for a browser's support for the specified CSS feature and then applies the CSS styling. - {{CSSxRef("@supports", "@supports selector()")}} - : Applies CSS rules after checking browser's support for the specified selector syntax. - {{CSSxRef("@supports", "@supports font-tech()")}} - : Applies CSS rules after checking browser's support for the specified font technology. - {{CSSxRef("@supports", "@supports font-format()")}} - : Applies CSS rules after checking browser's support for the specified font format. ## @namespace functions The {{CSSxRef("@namespace")}} at-rule is used to specify XML namespaces to be used in a CSS stylesheet. - {{CSSxRef("@namespace", "@namespace url()")}} - : Defines XML namespace from the specified URL. - {{CSSxRef("@namespace", "@namespace src()")}} - : Defines XML namespace from the specified source. ## @container functions The {{CSSxRef("@container")}} at-rule is used to specify styles for a containment context. - {{CSSxRef("@container", "@container style()")}} - : Defines the containment context style.
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/css_transforms/index.md
--- title: CSS transforms slug: Web/CSS/CSS_transforms page-type: css-module spec-urls: - https://drafts.csswg.org/css-transforms/ - https://drafts.csswg.org/css-transforms-2/ --- {{CSSRef}} The **CSS transforms** module defines how elements styled with CSS can be transformed in two-dimensional or three-dimensional space. ## CSS transforms in action Use the sliders in the frame below to modify the translation, rotation, scale, and skew CSS transform properties of the cube in 3D space. As you move the cube through 3D space, notice the way it interacts with the element labelled `z:0px`, which is located at the 3D position `(0, 0, 0)`. {{EmbedGHLiveSample("css-examples/modules/transforms.html", '100%', 900)}} You can also use the `perspective` slider to modify the [`perspective`](/en-US/docs/Web/CSS/perspective) property of the cube's container, which determines the distance between you and the `z=0` plane. The [`perspective-origin`](/en-US/docs/Web/CSS/perspective-origin) sliders determine where you, the viewer, are looking into the 3D space for purposes of determining the view's _vanishing point_. This vanishing point is indicated by a small red dot. You can imagine modifying these sliders as physically moving your head up, down, left, and right to see different parts of the cube without moving the cube itself. The `backface-visibility` checkbox determines whether the cube's back faces are set to `visible` or `hidden`. The cube in the above example is comprised of six `<div>` elements, all of which are styled with CSS to create the cube's faces. The cube is not drawn using a 2D or 3D canvas context, so **you can inspect the faces of the cube with your browser's developer tools as you would inspect any other DOM element**. Try using your browser's developer tools element picker to inspect different faces of the cube as you transform its position and rotation. > **Note:** The order in which transformations, including 3D rotations, are applied affects the resultant transformation. In the above example, transforms are translated, scaled, rotated, then skewed. The rotations are applied in the order X → Y → Z. You can [view this example's source on GitHub](https://github.com/mdn/css-examples/blob/main/modules/transforms.html). ## Reference ### Properties - {{cssxref("backface-visibility")}} - {{cssxref("perspective")}} - {{cssxref("perspective-origin")}} - {{cssxref("rotate")}} - {{cssxref("scale")}} - {{cssxref("transform")}} - {{cssxref("transform-box")}} - {{cssxref("transform-origin")}} - {{cssxref("transform-style")}} - {{cssxref("translate")}} ### Functions - {{cssxref("transform-function/matrix", "matrix()")}} - {{cssxref("transform-function/matrix3d", "matrix3d()")}} - {{cssxref("transform-function/perspective", "perspective()")}} - {{cssxref("transform-function/rotate", "rotate()")}} - {{cssxref("transform-function/rotate3d", "rotate3d()")}} - {{cssxref("transform-function/rotateX", "rotateX()")}} - {{cssxref("transform-function/rotateY", "rotateY()")}} - {{cssxref("transform-function/rotateZ", "rotateZ()")}} - {{cssxref("transform-function/scale", "scale()")}} - {{cssxref("transform-function/scale3d", "scale3d()")}} - {{cssxref("transform-function/scaleX", "scaleX()")}} - {{cssxref("transform-function/scaleY", "scaleY()")}} - {{cssxref("transform-function/scaleZ", "scaleZ()")}} - {{cssxref("transform-function/skew", "skew()")}} - {{cssxref("transform-function/skewX", "skewX()")}} - {{cssxref("transform-function/skewY", "skewY()")}} - {{cssxref("transform-function/translate", "translate()")}} - {{cssxref("transform-function/translate3d", "translate()")}} - {{cssxref("transform-function/translateX", "translateX()")}} - {{cssxref("transform-function/translateY", "translateY()")}} - {{cssxref("transform-function/translateZ", "translateZ()")}} ### Data types - {{cssxref("&lt;transform-function&gt;")}} ## Guides - [Using CSS transforms](/en-US/docs/Web/CSS/CSS_transforms/Using_CSS_transforms) - : Step-by-step tutorial about how to transform elements styled with CSS. - [Coordinate systems](/en-US/docs/Web/CSS/CSSOM_view/Coordinate_systems) - : Describes the way pixel locations are defined in the CSS object model. - [Performance fundamentals: Use CSS transforms](/en-US/docs/Web/Performance/Fundamentals#use_css_transforms) - : An overview of web performance fundamentals, including how CSS transforms can improve performance. - [Matrix math for the web](/en-US/docs/Web/API/WebGL_API/Matrix_math_for_the_web) - : Describes how object transformations can be represented by mathematical matrices. ## Related concepts - CSS Properties: - [`animation`](/en-US/docs/Web/CSS/animation) - [`background-position`](/en-US/docs/Web/CSS/background-position) - [`clip`](/en-US/docs/Web/CSS/clip) - [`clip-path`](/en-US/docs/Web/CSS/clip-path) - [`contain`](/en-US/docs/Web/CSS/contain) - [`content-visibility`](/en-US/docs/Web/CSS/content-visibility) - [`isolation`](/en-US/docs/Web/CSS/isolation) - [`mask`](/en-US/docs/Web/CSS/mask) - [`mask-border-source`](/en-US/docs/Web/CSS/mask-border-source) - [`mask-image`](/en-US/docs/Web/CSS/mask-image) - [`mix-blend-mode`](/en-US/docs/Web/CSS/mix-blend-mode) - [`opacity`](/en-US/docs/Web/CSS/opacity) - [`overflow`](/en-US/docs/Web/CSS/overflow) - [`transition`](/en-US/docs/Web/CSS/transition) - [`visibility`](/en-US/docs/Web/CSS/visibility) - Data types: - [`<angle>`](/en-US/docs/Web/CSS/angle) - {{cssxref("length-percentage")}} - {{cssxref("length")}} - [`<number>`](/en-US/docs/Web/CSS/number) - {{cssxref("percentage")}} - [`<position>`](/en-US/docs/Web/CSS/position_value) - Glossary terms: - [Interpolation](/en-US/docs/Glossary/Interpolation) - [Stacking context](/en-US/docs/Glossary/Stacking_context) - [SVG](/en-US/docs/Web/SVG) concepts: - [`<animate>`](/en-US/docs/Web/SVG/Element/animate) element - [`<animateTransform>`](/en-US/docs/Web/SVG/Element/animateTransform) element - [`<set>`](/en-US/docs/Web/SVG/Element/set) element - [`transform`](/en-US/docs/Web/SVG/Attribute/transform) element ## Specifications {{Specifications}} ## See also - [Basic SVG transformations](/en-US/docs/Web/SVG/Tutorial/Basic_Transformations) tutorial - [CSS animations](/en-US/docs/Web/CSS/CSS_animations) module - [CSS transitions](/en-US/docs/Web/CSS/CSS_transitions) module
0
data/mdn-content/files/en-us/web/css/css_transforms
data/mdn-content/files/en-us/web/css/css_transforms/using_css_transforms/index.md
--- title: Using CSS transforms slug: Web/CSS/CSS_transforms/Using_CSS_transforms page-type: guide --- {{CSSRef}} By modifying the coordinate space, **CSS transforms** change the shape and position of the affected content without disrupting the normal document flow. This guide provides an introduction to using transforms. CSS transforms are implemented using a set of CSS properties that let you apply affine linear transformations to HTML elements. These transformations include rotation, skewing, scaling, and translation both in the plane and in the 3D space. > **Warning:** Only transformable elements can be `transform`ed; that is, all elements whose layout is governed by the CSS [box model](/en-US/docs/Web/CSS/CSS_box_model) except for: [non-replaced inline boxes](/en-US/docs/Web/CSS/Visual_formatting_model#inline-level_elements_and_inline_boxes), [table-column boxes](/en-US/docs/Web/HTML/Element/col), and [table-column-group boxes](/en-US/docs/Web/HTML/Element/colgroup). ## CSS transforms properties Two major properties are used to define CSS transforms: {{cssxref("transform")}} (or the individual {{cssxref('translate')}}, {{cssxref('rotate')}}, and {{cssxref('scale')}} properties) and {{cssxref("transform-origin")}}. - {{cssxref("transform-origin")}} - : Specifies the position of the origin. By default, it is at the center of the element and can be moved. It is used by several transforms, like rotations, scaling or skewing, that need a specific point as a parameter. - {{cssxref("transform")}} - : Specifies the transforms to apply to the element. It is a space-separated list of transforms, which are applied one after the other, as requested by the composition operation. Composite transforms are effectively applied in order from right to left. ## Examples Here is an unaltered image of the MDN logo: ![MDN Logo](logo.png) ### Rotating Here is the MDN logo rotated 90 degrees from its bottom-left corner. ```html <img style="rotate: 90deg; transform-origin: bottom left;" src="logo.png" alt="MDN Logo" /> ``` {{EmbedLiveSample('Rotating', 'auto', 240) }} ### Skewing and translating Here is the MDN logo, skewed by 10 degrees and translated by 150 pixels on the X-axis. ```html <img style="transform: skewX(10deg) translateX(150px); transform-origin: bottom left;" src="logo.png" alt="MDN logo" /> ``` {{EmbedLiveSample('Skewing_and_translating') }} ## 3D specific CSS properties Performing CSS transformations in 3D space is a bit more complex. You have to start by configuring the 3D space by giving it a perspective, then you have to configure how your 2D elements will behave in that space. ### Perspective The first element to set is the {{cssxref("perspective")}}. The perspective is what gives us the 3D impression. The farther from the viewer the elements are, the smaller they are. #### Setting perspective This example shows a cube with the perspective set at different positions. How quick the cube shrinks is defined by the {{ cssxref("perspective") }} property. The smaller its value is, the deeper the perspective is. ##### HTML The HTML below creates four copies of the same box, with the perspective set at different values. ```html <table> <tbody> <tr> <th><code>perspective: 250px;</code></th> <th><code>perspective: 350px;</code></th> </tr> <tr> <td> <div class="container"> <div class="cube pers250"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </td> <td> <div class="container"> <div class="cube pers350"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </td> </tr> <tr> <th><code>perspective: 500px;</code></th> <th><code>perspective: 650px;</code></th> </tr> <tr> <td> <div class="container"> <div class="cube pers500"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </td> <td> <div class="container"> <div class="cube pers650"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </td> </tr> </tbody> </table> ``` ##### CSS The CSS establishes classes that can be used to set the perspective to different distances. It also includes classes for the container box and the cube itself, as well as each of its faces. ```css /* Shorthand classes for different perspective values */ .pers250 { perspective: 250px; } .pers350 { perspective: 350px; } .pers500 { perspective: 500px; } .pers650 { perspective: 650px; } /* Define the container div, the cube div, and a generic face */ .container { width: 200px; height: 200px; margin: 75px 0 0 75px; border: none; } .cube { width: 100%; height: 100%; perspective-origin: 150% 150%; transform-style: preserve-3d; } .face { display: block; position: absolute; width: 100px; height: 100px; border: none; line-height: 100px; font-family: sans-serif; font-size: 60px; color: white; text-align: center; backface-visibility: visible; } /* Define each face based on direction */ .front { background: rgb(0 0 0 / 30%); transform: translateZ(50px); } .back { background: rgb(0 255 0 / 100%); color: black; transform: rotateY(180deg) translateZ(50px); } .right { background: rgb(196 0 0 / 70%); transform: rotateY(90deg) translateZ(50px); } .left { background: rgb(0 0 196 / 70%); transform: rotateY(-90deg) translateZ(50px); } .top { background: rgb(196 196 0 / 70%); transform: rotateX(90deg) translateZ(50px); } .bottom { background: rgb(196 0 196 / 70%); transform: rotateX(-90deg) translateZ(50px); } /* Make the table a little nicer */ th, p, td { background-color: #eeeeee; padding: 10px; font-family: sans-serif; text-align: left; } ``` ##### Result {{EmbedLiveSample('Setting_perspective', 660, 700)}} The second element to configure is the position of the viewer, with the {{ cssxref("perspective-origin") }} property. By default the perspective is centered on the viewer, which is not always adequate. #### Changing the perspective origin This example shows cubes with popular `perspective-origin` values. ##### HTML ```html <section> <figure> <figcaption><code>perspective-origin: top left;</code></figcaption> <div class="container"> <div class="cube potl"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: top;</code></figcaption> <div class="container"> <div class="cube potm"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: top right;</code></figcaption> <div class="container"> <div class="cube potr"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: left;</code></figcaption> <div class="container"> <div class="cube poml"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: 50% 50%;</code></figcaption> <div class="container"> <div class="cube pomm"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: right;</code></figcaption> <div class="container"> <div class="cube pomr"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: bottom left;</code></figcaption> <div class="container"> <div class="cube pobl"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: bottom;</code></figcaption> <div class="container"> <div class="cube pobm"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: bottom right;</code></figcaption> <div class="container"> <div class="cube pobr"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: -200% -200%;</code></figcaption> <div class="container"> <div class="cube po200200neg"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: 200% 200%;</code></figcaption> <div class="container"> <div class="cube po200200pos"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> <figure> <figcaption><code>perspective-origin: 200% -200%;</code></figcaption> <div class="container"> <div class="cube po200200"> <div class="face front">1</div> <div class="face back">2</div> <div class="face right">3</div> <div class="face left">4</div> <div class="face top">5</div> <div class="face bottom">6</div> </div> </div> </figure> </section> ``` ##### CSS ```css /* perspective-origin values (unique per example) */ .potl { perspective-origin: top left; } .potm { perspective-origin: top; } .potr { perspective-origin: top right; } .poml { perspective-origin: left; } .pomm { perspective-origin: 50% 50%; } .pomr { perspective-origin: right; } .pobl { perspective-origin: bottom left; } .pobm { perspective-origin: bottom; } .pobr { perspective-origin: bottom right; } .po200200neg { perspective-origin: -200% -200%; } .po200200pos { perspective-origin: 200% 200%; } .po200200 { perspective-origin: 200% -200%; } /* Define the container div, the cube div, and a generic face */ .container { width: 100px; height: 100px; margin: 24px; border: none; } .cube { width: 100%; height: 100%; perspective: 300px; transform-style: preserve-3d; } .face { display: block; position: absolute; width: 100px; height: 100px; border: none; line-height: 100px; font-family: sans-serif; font-size: 60px; color: white; text-align: center; backface-visibility: visible; } /* Define each face based on direction */ .front { background: rgb(0 0 0 / 30%); transform: translateZ(50px); } .back { background: rgb(0 255 0 / 100%); color: black; transform: rotateY(180deg) translateZ(50px); } .right { background: rgb(196 0 0 / 70%); transform: rotateY(90deg) translateZ(50px); } .left { background: rgb(0 0 196 / 70%); transform: rotateY(-90deg) translateZ(50px); } .top { background: rgb(196 196 0 / 70%); transform: rotateX(90deg) translateZ(50px); } .bottom { background: rgb(196 0 196 / 70%); transform: rotateX(-90deg) translateZ(50px); } /* Make the layout a little nicer */ section { background-color: #eee; padding: 10px; font-family: sans-serif; text-align: left; display: grid; grid-template-columns: repeat(3, 1fr); } ``` ##### Result {{EmbedLiveSample('Changing_the_perspective_origin', '100%', 700)}} Once you have done this, you can work on the element in the 3D space. ## See also - The [CSS `transform` property](/en-US/docs/Web/CSS/transform) and the [CSS `<transform-function>` data types](/en-US/docs/Web/CSS/transform-function) - The individual transforms properties: {{cssxref('translate')}}, {{cssxref('rotate')}}, and {{cssxref('scale')}} (There is no `skew` property) - [Using device orientation with 3D Transforms](/en-US/docs/Web/API/Device_orientation_events/Using_device_orientation_with_3D_transforms) - [Intro to CSS 3D transforms](https://3dtransforms.desandro.com/) (Blog post by David DeSandro) - [CSS Transform Playground](https://css-transform.moro.es/) (Online tool to visualize CSS Transform functions)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/counter/index.md
--- title: counter() slug: Web/CSS/counter page-type: css-function browser-compat: css.types.counter --- {{CSSRef}} The **`counter()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) returns a string representing the current value of the named counter, if there is one. The `counter()` function is generally used within [pseudo-element](/en-US/docs/Web/CSS/Pseudo-elements) through the {{cssxref("content")}} property but, theoretically, it can be used wherever a [`<string>`](/en-US/docs/Web/CSS/string) value is supported. {{EmbedInteractiveExample("pages/tabbed/function-counter.html", "tabbed-shorter")}} ## Syntax ```css /* Simple usage */ counter(countername); /* changing the counter display */ counter(countername, upper-roman) ``` [Counters](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) have no visible effect by themselves. The `counter()` and {{cssxref("counters", "counters()")}} functions are what make counters useful by returning developer-defined strings (or images). ### Values The `counter()` function accepts up to two parameters. The first parameter is the `<counter-name>`. The optional second parameter is the `<counter-style>`. - `<counter-name>` - : A {{cssxref("&lt;custom-ident&gt;")}} identifying the counter, which is the same case-sensitive name used with the {{cssxref("counter-reset")}} and {{cssxref("counter-increment")}} property values. The counter name cannot start with two dashes and can't be `none`, `unset`, `initial`, or `inherit`. - `<counter-style>` - : A {{cssxref("&lt;list-style-type&gt;")}} name, {{cssxref("&lt;@counter-style&gt;")}} name or {{cssxref("symbols", "symbols()")}} function, where a counter style name is a `numeric`, `alphabetic`, or `symbolic` simple predefined counter style, a complex longhand east Asian or Ethiopic predefined counter style, or other [predefined counter style](/en-US/docs/Web/CSS/CSS_counter_styles). If omitted, the counter-style defaults to `decimal`. > **Note:** To join the counter values when nesting counters, use the {{cssxref("counters", "counters()")}} function, which provides an additional {{cssxref("string")}} parameter. ### Formal syntax {{CSSSyntax}} ## Examples ### lower-roman compared to lower-alpha In this example, we display a counter using `lower-roman` and `lower-alpha` list styles. #### HTML ```html <ol> <li></li> <li></li> <li></li> </ol> ``` #### CSS ```css-nolint ol { counter-reset: count; } li { counter-increment: count; } li::after { content: "[" counter(count, lower-roman) "] == [" counter(count, lower-alpha) "]"; } ``` #### Result {{EmbedLiveSample("lower-roman compared to lower-alpha", "100%", 150)}} ### Displaying a counter using three styles In this example, we use the `counter()` function three times. #### HTML ```html <ol> <li></li> <li></li> <li></li> </ol> ``` #### CSS We include the `counter()` function with three different counter styles, including the default decimal value. We've added padding to the list to provide space for the long `::marker` string. ```css-nolint ol { counter-reset: listCounter; padding-left: 5em; } li { counter-increment: listCounter; } li::marker { content: "Item #" counter(listCounter) " is: "; } li::after { content: "[" counter(listCounter, decimal-leading-zero) "] == [" counter(listCounter, upper-roman) "]"; } ``` #### Result {{EmbedLiveSample("Displaying a counter using three styles", "100%", 150)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Using CSS Counters](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) - {{cssxref("counter-reset")}} - {{cssxref("counter-set")}} - {{cssxref("counter-increment")}} - {{cssxref("@counter-style")}} - CSS [`counters()`](/en-US/docs/Web/CSS/counters) function - [CSS lists and counters](/en-US/docs/Web/CSS/CSS_lists) module - [CSS counter styles](/en-US/docs/Web/CSS/CSS_counter_styles) module - [CSS generated content](/en-US/docs/Web/CSS/CSS_generated_content) module
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/contain-intrinsic-inline-size/index.md
--- title: contain-intrinsic-inline-size slug: Web/CSS/contain-intrinsic-inline-size page-type: css-property browser-compat: css.properties.contain-intrinsic-inline-size --- {{CSSRef}} The **`contain-intrinsic-inline-size`** [CSS](/en-US/docs/Web/CSS) [logical property](/en-US/docs/Web/CSS/CSS_logical_properties_and_values) defines the inline-size of an element that a browser can use for layout when the element is subject to [size containment](/en-US/docs/Web/CSS/CSS_containment#size_containment). Inline-size is the size of the element in the dimension parallel to the flow of text within a line. In a horizontal [writing mode](/en-US/docs/Web/CSS/writing-mode) like standard English, inline size is the horizontal dimension (width); for a vertical writing mode, inline size is the vertical dimension. ## Syntax ```css /* Keyword values */ contain-intrinsic-inline-size: none; /* <length> values */ contain-intrinsic-inline-size: 1000px; contain-intrinsic-inline-size: 10rem; /* auto <length> */ contain-intrinsic-inline-size: auto 300px; /* Global values */ contain-intrinsic-inline-size: inherit; contain-intrinsic-inline-size: initial; contain-intrinsic-inline-size: revert; contain-intrinsic-inline-size: revert-layer; contain-intrinsic-inline-size: unset; ``` ### Values The following values can be specified for the intrinsic inline size of an element: - `none` - : The element has no intrinsic inline-size. - `<length>` - : The element has the specified inline-size ({{cssxref("&lt;length&gt;")}}). - `auto <length>` - : When the element is in size containment and skipping its contents (for example, when it is offscreen and `content-visibility: auto` is set) the inline size is remembered from the actual size of the element when it was last able to render its child elements. If the element has never rendered its child elements and hence has no remembered value for the normally rendered element size, or if it is not skipping its contents, the inline size is the specified `<length>`. ## Description The property is commonly applied alongside elements that can trigger size containment, such as [`contain: size`](/en-US/docs/Web/CSS/contain) and [`content-visibility`](/en-US/docs/Web/CSS/content-visibility). Size containment allows a user agent to lay out an element as though it had a fixed size, preventing unnecessary reflows by avoiding the re-rendering of child elements to determine the actual size (thereby improving user experience). By default, size containment treats elements as though they had no contents, and may collapse the layout in the same way as if the contents had no width or height. The `contain-intrinsic-inline-size` property allows authors to specify an appropriate value to be used as the inline-size for layout. The `auto <length>` value allows the inline-size of the element to be stored if the element is ever "normally rendered" (with its child elements), and then used instead of the specified value when the element is skipping its contents. This allows offscreen elements with [`content-visibility: auto`](/en-US/docs/Web/CSS/content-visibility) to benefit from size containment without developers having to be as precise in their estimates of element size. The remembered value is not used if the child elements are being rendered (if size containment is enabled, the `<length>` will be used). ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting the intrinsic inline size The HTML below defines an element "contained_element" that will be subject to size constraint, and which contains a child element. ```html <div id="contained_element"> <div class="child_element"></div> </div> ``` The CSS below sets the [`content-visibility`](/en-US/docs/Web/CSS/content-visibility) of `contained_element` to `auto`, so if the element is hidden it will be size constrained. The intrinsic block size and inline size that are used when it is size constrained are set at the same time using `contain-intrinsic-block-size` and `contain-intrinsic-inline-size`, respectively. ```css #contained_element { border: 2px solid green; inline-size: 151px; content-visibility: auto; contain-intrinsic-inline-size: 152px; contain-intrinsic-block-size: 52px; } .child_element { border: 1px solid red; background: blue; block-size: 50px; inline-size: 150px; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [content-visibility: the new CSS property that boosts your rendering performance](https://web.dev/articles/content-visibility) (web.dev) - {{CSSxRef("contain-intrinsic-block-size")}} - {{CSSxRef("contain-intrinsic-size")}} - {{CSSxRef("contain-intrinsic-width")}} - {{CSSxRef("contain-intrinsic-height")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/flex_value/index.md
--- title: <flex> slug: Web/CSS/flex_value page-type: css-type browser-compat: css.types.flex --- {{CSSRef}} The **`<flex>`** [CSS](/en-US/docs/Web/CSS) [data type](/en-US/docs/Web/CSS/CSS_Types) denotes a flexible length within a grid container. It is used in {{cssxref("grid-template-columns")}}, {{cssxref("grid-template-rows")}} and other related properties. ## Syntax The `<flex>` data type is specified as a {{cssxref("&lt;number&gt;")}} followed by the unit `fr`. The `fr` unit represents a fraction of the leftover space in the grid container. As with all CSS dimensions, there is no space between the unit and the number. ## Examples ### Examples of correct values for the fr data type ```plain 1fr /* Using an integer value */ 2.5fr /* Using a float value */ ``` ### Example of use in a tracklisting for CSS Grid layout ```css .grid { display: grid; grid-template-columns: 1fr 1fr 2.5fr 1.5fr; } ``` ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS Grid Layout](/en-US/docs/Web/CSS/CSS_grid_layout)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/_colon_indeterminate/index.md
--- title: ":indeterminate" slug: Web/CSS/:indeterminate page-type: css-pseudo-class browser-compat: css.selectors.indeterminate --- {{CSSRef}} The **`:indeterminate`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) represents any form element whose state is indeterminate, such as checkboxes that have been set to an [`indeterminate`](/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes) state with JavaScript, radio buttons which are members of a group in which all radio buttons are unchecked, and {{HTMLElement("progress")}} elements with no `value` attribute. ```css /* Selects any <input> whose state is indeterminate */ input:indeterminate { background: lime; } ``` Elements targeted by this selector are: - [`<input type="checkbox">`](/en-US/docs/Web/HTML/Element/input/checkbox) elements whose `indeterminate` property is set to `true` by [JavaScript](/en-US/docs/Web/JavaScript) - [`<input type="radio">`](/en-US/docs/Web/HTML/Element/input/radio) elements, when all radio buttons with the same `name` value in the form are unchecked - {{HTMLElement("progress")}} elements in an indeterminate state ## Syntax ```plain :indeterminate ``` ## Examples ### Checkbox & radio button This example applies special styles to the labels associated with indeterminate form fields. #### HTML ```html <fieldset> <legend>Checkbox</legend> <div> <input type="checkbox" id="checkbox" /> <label for="checkbox">This checkbox label starts out lime.</label> </div> </fieldset> <fieldset> <legend>Radio</legend> <div> <input type="radio" id="radio1" name="radioButton" value="val1" /> <label for="radio1">First radio label starts out lime.</label> </div> <div> <input type="radio" id="radio2" name="radioButton" value="val2" /> <label for="radio2">Second radio label also starts out lime.</label> </div> </fieldset> ``` #### CSS ```css input:indeterminate + label { background: lime; } ``` ```css hidden fieldset { padding: 1em 0.75em; } fieldset:first-of-type { margin-bottom: 1.5rem; } fieldset:not(:first-of-type) > div:not(:last-child) { margin-bottom: 0.5rem; } ``` #### JavaScript ```js const inputs = document.getElementsByTagName("input"); for (let i = 0; i < inputs.length; i++) { inputs[i].indeterminate = true; } ``` #### Result {{EmbedLiveSample('Checkbox_radio_button', 'auto', 230)}} ### Progress bar #### HTML ```html <progress></progress> ``` #### CSS ```css progress { margin: 4px; } progress:indeterminate { width: 80vw; height: 20px; } ``` #### Result {{EmbedLiveSample('Progress_bar', 'auto', 30)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Web forms — Working with user data](/en-US/docs/Learn/Forms) - [Styling web forms](/en-US/docs/Learn/Forms/Styling_web_forms) - The [`<input type="checkbox">`](/en-US/docs/Web/HTML/Element/input/checkbox) element's [`indeterminate`](/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes) property - {{HTMLElement("input")}} and the {{domxref("HTMLInputElement")}} interface which implements it. - The {{cssxref(":checked")}} CSS selector lets you style checkboxes based on whether they're checked or not
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/mask-image/index.md
--- title: mask-image slug: Web/CSS/mask-image page-type: css-property browser-compat: css.properties.mask-image --- {{CSSRef}} The **`mask-image`** [CSS](/en-US/docs/Web/CSS) property sets the image that is used as mask layer for an element. By default this means the alpha channel of the mask image will be multiplied with the alpha channel of the element. This can be controlled with the {{cssxref("mask-mode")}} property. ## Syntax ```css /* Keyword value */ mask-image: none; /* <mask-source> value */ mask-image: url(masks.svg#mask1); /* <image> values */ mask-image: linear-gradient(rgb(0 0 0 / 100%), transparent); mask-image: image(url(mask.png), skyblue); /* Multiple values */ mask-image: image(url(mask.png), skyblue), linear-gradient(rgb(0 0 0 / 100%), transparent); /* Global values */ mask-image: inherit; mask-image: initial; mask-image: revert; mask-image: revert-layer; mask-image: unset; ``` ### Values - `none` - : This keyword is interpreted as an opaque white image layer. - `<mask-source>` - : A {{cssxref("url","url()")}} reference to a {{SVGElement("mask")}} or to a CSS image. - {{cssxref("&lt;image&gt;")}} - : An image value used as mask image layer. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting a mask image with a URL {{EmbedGHLiveSample("css-examples/masking/mask-image.html", '100%', 560)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [Clipping and Masking in CSS](https://css-tricks.com/clipping-masking-css/) - [Apply effects to images with CSS's mask-image property](https://web.dev/articles/css-masking)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/color_value/index.md
--- title: <color> slug: Web/CSS/color_value page-type: css-type browser-compat: css.types.color --- {{CSSRef}} The **`<color>`** [CSS](/en-US/docs/Web/CSS) [data type](/en-US/docs/Web/CSS/CSS_Types) represents a color. A `<color>` may also include an [alpha-channel](https://en.wikipedia.org/wiki/Alpha_compositing) _transparency value_, indicating how the color should [composite](https://www.w3.org/TR/compositing-1/#simplealphacompositing) with its background. > **Note:** Although `<color>` values are precisely defined, their actual appearance may vary (sometimes significantly) from device to device. This is because most devices are not calibrated, and some browsers do not support output devices' [color profiles](https://en.wikipedia.org/wiki/ICC_profile). ## Syntax ```css /* Named colors */ rebeccapurple aliceblue /* RGB Hexadecimal */ #f09 #ff0099 /* RGB (Red, Green, Blue) */ rgb(255 0 153) rgb(255 0 153 / 80%) /* HSL (Hue, Saturation, Lightness) */ hsl(150 30% 60%) hsl(150 30% 60% / 80%) /* HWB (Hue, Whiteness, Blackness) */ hwb(12 50% 0%) hwb(194 0% 0% / 0.5) /* LAB (Lightness, A-axis, B-axis) */ lab(50% 40 59.5) lab(50% 40 59.5 / 0.5) /* LCH (Lightness, Chroma, Hue) */ lch(52.2% 72.2 50) lch(52.2% 72.2 50 / 0.5) /* Oklab (Lightness, A-axis, B-axis) */ oklab(59% 0.1 0.1) oklab(59% 0.1 0.1 / 0.5) /* Oklch (Lightness, Chroma, Hue) */ oklch(60% 0.15 50) oklch(60% 0.15 50 / 0.5) /* light-dark */ light-dark(white, black) light-dark(rgb(255 255 255), rgb(0 0 0)) ``` A `<color>` value can be specified using one of the methods listed below: - By keywords: {{CSSXref("&lt;named-color&gt;")}} (such as `blue` or `pink`), {{CSSXref("&lt;system-color&gt;")}}, and [`currentcolor`](#currentcolor_keyword). - By hexadecimal notations: {{CSSXref("&lt;hex-color&gt;")}} (such as `#ff0000`). - By parameters in a color space using functional notations: - [sRGB](https://en.wikipedia.org/wiki/SRGB) color space: {{CSSXref("color_value/hsl", "hsl()")}}, {{CSSXref("color_value/hwb", "hwb()")}}, {{CSSXref("color_value/rgb", "rgb()")}}; - [CIELAB](https://en.wikipedia.org/wiki/CIELAB_color_space) color space: {{CSSXref("color_value/lab", "lab()")}}, {{CSSXref("color_value/lch", "lch()")}}; - [Oklab](https://bottosson.github.io/posts/oklab/) color space: {{CSSXref("color_value/oklab", "oklab()")}}, {{CSSXref("color_value/oklch", "oklch()")}}; - Other color spaces: {{CSSXref("color_value/color", "color()")}}. - By mixing two colors: {{CSSXref("color_value/color-mix", "color-mix()")}}. - By specifying two colors, the first used for light color-schemes and the second used for dark color-schemes: {{CSSXref("color_value/light-dark", "light-dark()")}}. ### currentcolor keyword The `currentcolor` keyword represents the value of an element's {{Cssxref("color")}} property. This lets you use the `color` value on properties that do not receive it by default. If `currentcolor` is used as the value of the `color` property, it instead takes its value from the inherited value of the `color` property. ```html <div style="color: blue; border: 1px dashed currentcolor;"> The color of this text is blue. <div style="background: currentcolor; height:9px;"></div> This block is surrounded by a blue border. </div> ``` {{EmbedLiveSample("currentcolor_keyword", "100%", 80)}} ### Missing color components Each component of any CSS color functions - except for those using the legacy comma-separated syntax - can be specified as the keyword `none` to be a missing component. Explicitly specifying missing components is useful in [color interpolation](#interpolation_with_missing_components) for cases where you would like to interpolate some color components but not others. For all other purposes, a missing component will effectively have a zero value in an appropriate unit: `0`, `0%`, or `0deg`. For example, the following colors are equivalent when used outside of interpolation: ```css /* These are equivalent */ color: oklab(50% none -0.25); color: oklab(50% 0 -0.25); /* These are equivalent */ background-color: hsl(none 100% 50%); background-color: hsl(0deg 100% 50%); ``` ## Interpolation Color interpolation happens with [gradients](/en-US/docs/Web/CSS/gradient), [transitions](/en-US/docs/Web/CSS/CSS_transitions/Using_CSS_transitions), and [animations](/en-US/docs/Web/CSS/CSS_animations/Using_CSS_animations). When interpolating `<color>` values, they are first converted to a given color space, and then each component of the [computed values](/en-US/docs/Web/CSS/computed_value) are interpolated linearly, with interpolation's speed being determined by the [easing function](/en-US/docs/Web/CSS/easing-function) in transitions and animations. The interpolation color space defaults to Oklab, but can be overridden through {{CSSXref("&lt;color-interpolation-method&gt;")}} in some color-related functional notations. ### Interpolation with missing components #### Interpolating colors in the same space When interpolating colors that are exactly in the interpolation color space, missing components from one color are replaced with existing values of the same components from the other color. For example, the following two expressions are equivalent: ```css color-mix(in oklch, oklch(none 0.2 10), oklch(60% none 30)) color-mix(in oklch, oklch(60% 0.2 10), oklch(60% 0.2 30)) ``` > **Note:** If a component is missing from both colors, this component will be missing after the interpolation. #### Interpolating colors from different spaces: analogous components If any color to be interpolated is not in the interpolation color space, its missing components are transferred into the converted color based on **analogous components** of the same category as described in the following table: | Category | Analogous components | | ------------ | -------------------- | | Reds | `R`, `X` | | Greens | `G`, `Y` | | Blues | `B`, `Z` | | Lightness | `L` | | Colorfulness | `C`, `S` | | Hue | `H` | | a | `a` | | b | `b` | For example: - `X` (`0.2`) in `color(xyz 0.2 0.1 0.6)` is analogous to `R` (`50%`) in `rgb(50% 70% 30%)`. - `H` (`0deg`) in `hsl(0deg 100% 80%)` is analogous to `H` (`140`) in `oklch(80% 0.1 140)`. Using Oklch as the interpolation color space and the two colors below as an example: ```css lch(80% 30 none) color(display-p3 0.7 0.5 none) ``` the preprocessing procedure is: 1. Replace the missing components in both colors with a zero value: ```css lch(80% 30 0) color(display-p3 0.7 0.5 0) ``` 2. Convert both colors into the interpolation color space: ```css oklch(83.915% 0.0902 0.28) oklch(63.612% 0.1522 78.748) ``` 3. If any component of the converted colors is analogous to a missing component in the corresponding original color, reset it as a missing component: ```css oklch(83.915% 0.0902 none) oklch(63.612% 0.1522 78.748) ``` 4. Replace any missing component with the same component from the other converted color: ```css oklch(83.915% 0.0902 78.748) oklch(63.612% 0.1522 78.748) ``` ## Accessibility concerns Some people have difficulty distinguishing colors. The [WCAG 2.2](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Use_of_color) recommendation strongly advises against using color as the only means of conveying a specific message, action, or result. See [color and color contrast](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Color_contrast) for more information. ## Formal syntax {{csssyntax}} ## Examples ### Color value tester In this example we provide a `<div>` and a text input. Entering a valid color into the input causes the `<div>` to adopt that color, allowing you to test our color values. #### HTML ```html <div></div> <hr /> <label for="color">Enter a valid color value:</label> <input type="text" id="color" /> ``` ```css hidden div { height: 200px; width: 200px; } ``` ```js hidden const inputElem = document.querySelector("input"); const divElem = document.querySelector("div"); function validTextColor(stringToTest) { if (stringToTest === "inherit" || stringToTest === "transparent") { return false; } const div = document.createElement("div"); div.style.color = stringToTest; return !!div.style.color; } inputElem.addEventListener("input", () => { if (validTextColor(inputElem.value)) { divElem.style.backgroundColor = inputElem.value; divElem.textContent = ""; } else { divElem.removeAttribute("style"); divElem.textContent = "Invalid color value"; } }); ``` #### Result {{EmbedLiveSample("color_value_tester", "100%", 300)}} ### Fully saturated sRGB colors This example shows fully saturated sRGB colors in the sRGB color space. #### HTML ```html <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> ``` #### CSS ```css hidden body { display: flex; flex-wrap: wrap; } div { height: 80px; margin: 10px; width: 80px; } ``` ```css div:nth-child(1) { background-color: hsl(0 100% 50%); } div:nth-child(2) { background-color: hsl(30 100% 50%); } div:nth-child(3) { background-color: hsl(60 100% 50%); } div:nth-child(4) { background-color: hsl(90 100% 50%); } div:nth-child(5) { background-color: hsl(120 100% 50%); } div:nth-child(6) { background-color: hsl(150 100% 50%); } div:nth-child(7) { background-color: hsl(180 100% 50%); } div:nth-child(8) { background-color: hsl(210 100% 50%); } div:nth-child(9) { background-color: hsl(240 100% 50%); } div:nth-child(10) { background-color: hsl(270 100% 50%); } div:nth-child(11) { background-color: hsl(300 100% 50%); } div:nth-child(12) { background-color: hsl(330 100% 50%); } ``` #### Result {{EmbedLiveSample("fully_saturated_srgb_colors", "100%", 200)}} ### Reds of different shades This example shows reds of different shades in the sRGB color space. #### HTML ```html <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> ``` #### CSS ```css hidden body { display: flex; flex-wrap: wrap; } div { box-sizing: border-box; height: 80px; margin: 10px; width: 80px; } ``` ```css div:nth-child(1) { background-color: hsl(0 100% 0%); } div:nth-child(2) { background-color: hsl(0 100% 20%); } div:nth-child(3) { background-color: hsl(0 100% 40%); } div:nth-child(4) { background-color: hsl(0 100% 60%); } div:nth-child(5) { background-color: hsl(0 100% 80%); } div:nth-child(6) { background-color: hsl(0 100% 100%); border: solid; } ``` #### Result {{EmbedLiveSample("reds_of_different_shades", "100%", 150)}} ### Reds of different saturations This example shows reds of different saturations in the sRGB color space. #### HTML ```html <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> ``` #### CSS ```css hidden body { display: flex; flex-wrap: wrap; } div { height: 80px; margin: 10px; width: 80px; } ``` ```css div:nth-child(1) { background-color: hsl(0 0% 50%); } div:nth-child(2) { background-color: hsl(0 20% 50%); } div:nth-child(3) { background-color: hsl(0 40% 50%); } div:nth-child(4) { background-color: hsl(0 60% 50%); } div:nth-child(5) { background-color: hsl(0 80% 50%); } div:nth-child(6) { background-color: hsl(0 100% 50%); } ``` #### Result {{EmbedLiveSample("reds_of_different_saturations", "100%", 150)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{CSSXref("opacity")}}: the property defining transparency at the element level - {{CSSXref("&lt;hue&gt;")}}: the data type representing the hue angle of a color - {{CSSXref("color")}}, {{CSSXref("background-color")}}, {{CSSXref("border-color")}}, {{CSSXref("box-shadow")}}, {{CSSXref("outline-color")}}, {{CSSXref("text-shadow")}}: common properties that use `<color>` - [Applying color to HTML elements using CSS](/en-US/docs/Web/CSS/CSS_colors/Applying_color) - [New functions, gradients, and hues in CSS colors (Level 4)](/en-US/blog/css-color-module-level-4/) on MDN blog (2023)
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/color/index.md
--- title: color() slug: Web/CSS/color_value/color page-type: css-function browser-compat: css.types.color.color --- {{CSSRef}} The **`color()`** functional notation allows a color to be specified in a particular, specified colorspace rather than the implicit sRGB colorspace that most of the other color functions operate in. Support for a particular colorspace can be detected with the [`color-gamut`](/en-US/docs/Web/CSS/@media/color-gamut) CSS media feature. ## Syntax ```css color(display-p3 1 0.5 0); color(display-p3 1 0.5 0 / .5); ``` ### Values Functional notation: `color(colorspace c1 c2 c3[ / A])` - `colorspace` - : An {{CSSXref("&lt;ident&gt;")}} denoting one of the predefined color spaces: `srgb`, `srgb-linear`, `display-p3`, `a98-rgb`, `prophoto-rgb`, `rec2020`, `xyz`, `xyz-d50`, and `xyz-d65`. - `c1`, `c2`, `c3` - : {{CSSXref("number")}} between 0 and 1, a {{CSSXref("percentage")}} or the keyword `none`, which provide the component values in the color space. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Examples ### Using predefined colorspaces with color() The following example shows the effect of varying the lightness, a-axis, and b-axis values of the `color()` function. #### HTML ```html <div data-color="red-a98-rgb"></div> <div data-color="red-prophoto-rgb"></div> <div data-color="green-srgb-linear"></div> <div data-color="green-display-p3"></div> <div data-color="blue-rec2020"></div> <div data-color="blue-srgb"></div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } ``` ```css [data-color="red-a98-rgb"] { background-color: color(a98-rgb 1 0 0); } [data-color="red-prophoto-rgb"] { background-color: color(prophoto-rgb 1 0 0); } [data-color="green-srgb-linear"] { background-color: color(srgb-linear 0 1 0); } [data-color="green-display-p3"] { background-color: color(display-p3 0 1 0); } [data-color="blue-rec2020"] { background-color: color(rec2020 0 0 1); } [data-color="blue-srgb"] { background-color: color(srgb 0 0 1); } ``` #### Result {{EmbedLiveSample("using_predefined_colorspaces_with_color")}} ### Using the xyz colorspace with color() The following example shows how to use the `xyz` colorspace to specify a color. #### HTML ```html <div data-color="red"></div> <div data-color="green"></div> <div data-color="blue"></div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } ``` ```css [data-color="red"] { background-color: color(xyz 45 20 0); } [data-color="green"] { background-color: color(xyz-d50 0.3 80 0.3); } [data-color="blue"] { background-color: color(xyz-d65 5 0 50); } ``` #### Result {{EmbedLiveSample("using_the_xyz_colorspace_with_color")}} ### Using color-gamut media queries with color() This example shows how to use the [`color-gamut`](/en-US/docs/Web/CSS/@media/color-gamut) media query to detect support for a particular colorspace and use that colorspace to specify a color. #### HTML ```html <div></div> <div></div> <div></div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } ``` ```css @media (color-gamut: p3) { div { background-color: color(display-p3 1 0 0); } } @media (color-gamut: srgb) { div:nth-child(2) { background-color: color(srgb 1 0 0); } } @media (color-gamut: rec2020) { div:nth-child(3) { background-color: color(rec2020 1 0 0); } } ``` #### Result {{EmbedLiveSample("using_color-gamut_media_queries_with_color")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [The `<color>` data type](/en-US/docs/Web/CSS/color_value) for a list of all color notations - [`color-gamut`](/en-US/docs/Web/CSS/@media/color-gamut) media feature - [Wide Gamut Color in CSS with Display-p3](https://webkit.org/blog/10042/wide-gamut-color-in-css-with-display-p3/)
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/lab/index.md
--- title: lab() slug: Web/CSS/color_value/lab page-type: css-function browser-compat: css.types.color.lab --- {{CSSRef}} The **`lab()`** functional notation expresses a given color in the CIE L\*a\*b\* color space. Lab represents the entire range of color that humans can see. ## Syntax ```css lab(29.2345% 39.3825 20.0664); lab(52.2345% 40.1645 59.9971); lab(52.2345% 40.1645 59.9971 / .5); ``` ### Values Functional notation: `lab(L a b[ / A])` - `L` - : A {{CSSXref("&lt;number&gt;")}} between `0` and `100`, a {{CSSXref("&lt;percentage&gt;")}} between `0%` and `100%`, or the keyword `none`, which specifies the CIE Lightness. Here the number `0` corresponds to `0%` (black) and the number `100` corresponds to `100%` (white). - `a` - : A {{CSSXref("&lt;number&gt;")}} between `-125` and `125`, a {{CSSXref("&lt;percentage&gt;")}} between `-100%` and `100%`, or the keyword `none`, which specifies the distance along the `a` axis in the CIELAB colorspace, that is how green/red the color is. - `b` - : A {{CSSXref("&lt;number&gt;")}} between `-125` and `125`, a {{CSSXref("&lt;percentage&gt;")}} between `-100%` and `100%`, or the keyword `none`, which specifies the distance along the `b` axis in the CIELAB colorspace, that is how blue/yellow the color is. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** Usually when percentage values have a numeric equivalent in CSS, `100%` is equal to the number `1`. > This case is notable where `100%` is equal to the number `100` for the `L` value and `125` for the `a` and `b` values. > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Examples ### Adjusting lightness and color axes with lab() The following example shows the effect of varying the lightness, a-axis, and b-axis values of the `lab()` function. #### HTML ```html <div data-color="red"></div> <div data-color="red-a"></div> <div data-color="green"></div> <div data-color="green-b"></div> <div data-color="blue"></div> <div data-color="blue-light"></div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } ``` ```css [data-color="red"] { background-color: lab(100 125 125); } [data-color="red-a"] { background-color: lab(100 110 125); } [data-color="green"] { background-color: lab(75% -120 125); } [data-color="green-b"] { background-color: lab(75% -120 10); } [data-color="blue"] { background-color: lab(0 -120 -120); } [data-color="blue-light"] { background-color: lab(70 -120 -120); } ``` #### Result {{EmbedLiveSample("Adjusting_lightness_and_color_axes_with_lab")}} ### Adjusting opacity with lab() The following example shows the effect of varying the `A` (alpha) value of the `lab()` functional notation. The `red` and `red-alpha` elements overlap the `#background-div` element to demonstrate the effect of opacity. Giving `A` a value of `0.4` makes the color 40% opaque. #### HTML ```html <div id="background-div"> <div data-color="red"></div> <div data-color="red-alpha"></div> </div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } #background-div { background-color: lightblue; width: 150px; height: 40px; } ``` ```css [data-color="red"] { background-color: lab(100 125 125); } [data-color="red-alpha"] { background-color: lab(100 125 125 / 0.4); } ``` #### Result {{EmbedLiveSample('Adjusting_opacity_with_lab')}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - The [`<color>` data type](/en-US/docs/Web/CSS/color_value) for a list of all color notations - [LCH colors in CSS: what, why, and how?](https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/) - [Safari Technology Preview 122 release notes](https://webkit.org/blog/11577/release-notes-for-safari-technology-preview-122/): includes `lab()` and {{cssxref("color_value/lch",'lch()')}} colors
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/rgb/index.md
--- title: rgb() slug: Web/CSS/color_value/rgb page-type: css-function browser-compat: css.types.color.rgb --- {{CSSRef}} The **`rgb()`** functional notation expresses a color according to its red, green, and blue components. An optional alpha component represents the color's transparency. > **Note:** The legacy `rgba()` syntax is an alias for `rgb()`, accepting the same parameters and behaving in the same way. {{EmbedInteractiveExample("pages/css/function-rgb.html")}} ## Syntax ```css rgb(255 255 255) rgb(255 255 255 / 50%) ``` The function also accepts a legacy syntax in which all values are separated with commas. ### Values Functional notation: `rgb(R G B[ / A])` - `R`, `G`, `B` - : Each as a {{CSSXref("&lt;number&gt;")}} between `0` and `255`, a {{CSSXref("&lt;percentage&gt;")}} between `0%` and `100%`, or the keyword `none`, which represent the red, green, and blue channels, respectively. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** This functional notation serializes to sRGB values, and the values of the red, green, blue components may be rounded in serialization. > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Examples ### Legacy syntax: comma-separated values For legacy reasons, the `rgb()` function accepts a form in which all values are separated using commas. #### HTML ```html <div class="space-separated"></div> <div class="comma-separated"></div> ``` #### CSS ```css div { width: 100px; height: 50px; margin: 1rem; } div.space-separated { background-color: rgb(255 0 0 / 50%); } div.comma-separated { background-color: rgb(255, 0, 0, 0.5); } ``` #### Result {{EmbedLiveSample('Legacy syntax: comma-separated values', '100%', '150px')}} ### Legacy syntax: rgba() The legacy `rgba()` syntax is an alias for `rgb()`. #### HTML ```html <div class="rgb"></div> <div class="rgba"></div> ``` #### CSS ```css div { width: 100px; height: 50px; margin: 1rem; } div.rgb { background-color: rgb(255 0 0 / 50%); } div.rgba { background-color: rgba(255 0 0 / 0.5); } ``` #### Result {{EmbedLiveSample('Legacy syntax: rgba()', '100%', '150px')}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - The {{CSSXref("&lt;color&gt;")}} data type for a list of all color notations
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/oklch/index.md
--- title: oklch() slug: Web/CSS/color_value/oklch page-type: css-function browser-compat: css.types.color.oklch --- {{CSSRef}} The **`oklch()`** functional notation expresses a given color in the Oklch color space. It has the same L axis as {{cssxref("color_value/oklab","oklab()")}}, but uses polar coordinates C (Chroma) and H (Hue). ## Syntax ```css oklch(40.1% 0.123 21.57) oklch(59.69% 0.156 49.77) oklch(59.69% 0.156 49.77 / .5) ``` ### Values Functional notation: `oklch(L C H[ / A])` - `L` - : A {{CSSXref("&lt;number&gt;")}} between `0` and `1`, a {{CSSXref("&lt;percentage&gt;")}} between `0%` and `100%`, or the keyword `none`, where the number `0` corresponds to `0%` (black) and the number `1` corresponds to `100%` (white). `L` specifies the perceived lightness. - `C` - : A {{CSSXref("&lt;number&gt;")}}, a {{CSSXref("&lt;percentage&gt;")}}, or the keyword `none`, where `0%` is `0` and `100%` is the number `0.4`. It is a measure of the chroma (roughly representing the "amount of color"). Its minimum useful value is `0`, while the maximum is theoretically unbounded (but in practice does not exceed `0.5`). - `H` - : A {{CSSXref("&lt;number&gt;")}}, an {{CSSXref("&lt;angle&gt;")}}, or the keyword `none`, which represents the hue angle. More details on this type can be found on the {{CSSXref("&lt;hue&gt;")}} reference. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Examples ### Adjusting the lightness, chroma, and hue of a color The following example shows the effect of varying the `L` (lightness), `C` (chroma), and `H` (hue) values of the `oklch()` color function. #### HTML ```html <div data-color="blue"></div> <div data-color="blue-light"></div> <div data-color="red"></div> <div data-color="red-chroma"></div> <div data-color="green"></div> <div data-color="green-hue"></div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } ``` ```css [data-color="blue"] { background-color: oklch(60% 0.4 240); } [data-color="blue-light"] { background-color: oklch(90% 0.4 240); } [data-color="red"] { background-color: oklch(100% 0.4 30); } [data-color="red-chroma"] { background-color: oklch(100% 0.3 40); } [data-color="green"] { background-color: oklch(60% 0.57 161); } [data-color="green-hue"] { background-color: oklch(60% 0.57 181); } ``` #### Result {{EmbedLiveSample("adjusting_the_lightness_chroma_and_hue_of_a_color")}} ### Adjusting the alpha value of a color The following example shows the effect of varying the `A` (alpha) value of the `oklch()` color function. The `red` and `red-alpha` elements overlap the `#background-div` element to demonstrate the effect of opacity. Giving `A` a value of `0.4` makes the color 40% opaque. #### HTML ```html <div id="background-div"> <div data-color="red"></div> <div data-color="red-alpha"></div> </div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } #background-div { background-color: oklch(100% 0.57 217); width: 150px; height: 40px; } ``` ```css [data-color="red"] { background-color: oklch(50% 0.5 20); } [data-color="red-alpha"] { background-color: oklch(50% 0.5 20 / 0.4); } ``` #### Result {{EmbedLiveSample("adjusting_the_alpha_value_of_a_color")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [List of all color notations](/en-US/docs/Web/CSS/color_value) - {{CSSXref("&lt;hue&gt;")}} data type - [A perceptual color space for image processing](https://bottosson.github.io/posts/oklab/) - [OKLCH in CSS](https://evilmartians.com/chronicles/oklch-in-css-why-quit-rgb-hsl)
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/hwb/index.md
--- title: hwb() slug: Web/CSS/color_value/hwb page-type: css-function browser-compat: css.types.color.hwb --- {{CSSRef}} The **`hwb()`** functional notation expresses an {{glossary("RGB", "sRGB")}} color according to its hue, whiteness, and blackness. An optional alpha component represents the color's transparency. {{EmbedInteractiveExample("pages/css/function-hwb.html")}} ## Syntax ```css hwb(194 0% 0%) /* #00c3ff */ hwb(194 0% 0% / .5) /* #00c3ff with 50% opacity */ ``` ### Values Functional notation: `hwb(H W B[ / A])` - `H` - : A {{CSSXref("&lt;number&gt;")}}, an {{CSSXref("&lt;angle&gt;")}}, or the keyword `none`, which represents the hue angle. More details on this type can be found on the {{CSSXref("&lt;hue&gt;")}} reference. - `W`, `B` - : Each as a {{CSSXref("&lt;percentage&gt;")}} or the keyword `none`, which represent whiteness and blackness, respectively. They specify the amount of white and black to mix in, from `0%` (no whiteness or blackness) to `100%` (full whiteness or blackness). If `W + B = 100%`, it defines some shade of gray. If `W + B > 100%`, `W` and `B` are effectively normalized as `W / (W + B)` and `B / (W + B)`, respectively. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** This functional notation serializes to sRGB values, and the values of the red, green, blue components may be rounded in serialization. > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{CSSXref("&lt;color&gt;")}}: For a list of all color notations - {{CSSXref("&lt;hue&gt;")}}: the data type representing a hue angle of a color
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/oklab/index.md
--- title: oklab() slug: Web/CSS/color_value/oklab page-type: css-function browser-compat: css.types.color.oklab --- {{CSSRef}} The **`oklab()`** functional notation expresses a given color in the Oklab color space, which attempts to mimic how color is perceived by the human eye. The `oklab()` works with a Cartesian coordinate system on the Oklab color space, the a- and b-axes. If you want a polar color system, chroma and hue, use {{cssxref("color_value/oklch", "oklch()")}}. Oklab is a perceptual color space and is useful to: - Transform an image to grayscale, without changing its lightness. - Modify the saturation of colors, while keeping user perception of hue and lightness - Create smooth and uniform gradients of colors (when interpolated manually, for example, in a {{HTMLElement("canvas")}} element). The function `oklab()` can represent any color from the Oklab color space that is wider than RGB and include wide gamut and P3 colors. ## Syntax ```css oklab(40.1% 0.1143 0.045); oklab(59.69% 0.1007 0.1191); oklab(59.69% 0.1007 0.1191 / 0.5); ``` ### Values Functional notation: `oklab(L a b[ / A])` - `L` - : A {{CSSXref("&lt;number&gt;")}} between `0` and `1`, a {{CSSXref("&lt;percentage&gt;")}} between `0%` and `100%`, or the keyword `none`, where the number `0` corresponds to `0%` (black) and the number `1` corresponds to `100%` (white). `L` specifies the perceived lightness. - `a` - : A {{CSSXref("&lt;number&gt;")}} between `-0.4` and `0.4`, a {{CSSXref("&lt;percentage&gt;")}} between `-100%` and `100%`, or the keyword `none`. It specifies the distance along the `a` axis in the Oklab colorspace, that is, how green or red the color is. - `b` - : A {{CSSXref("&lt;number&gt;")}} between `-0.4` and `0.4`, a {{CSSXref("&lt;percentage&gt;")}} between `-100%` and `100%`, or the keyword `none`. It specifies the distance along the `b` axis in the Oklab colorspace, that is, how blue or yellow the color is. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Examples ### Adjusting the lightness and axes with oklab() The following example shows the effect of varying the lightness, a-axis, and b-axis values of the `oklab()` function. #### HTML ```html <div data-color="blue"></div> <div data-color="blue-light"></div> <div data-color="red"></div> <div data-color="red-a"></div> <div data-color="green"></div> <div data-color="green-b"></div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } ``` ```css [data-color="blue"] { background-color: oklab(0.5 -0.3 -0.4); } [data-color="blue-light"] { background-color: oklab(0.7 -0.3 -0.4); } [data-color="red"] { background-color: oklab(100% 0.4 0.4); } [data-color="red-a"] { background-color: oklab(100% 0.2 0.4); } [data-color="green"] { background-color: oklab(100% -100% 0.4); } [data-color="green-b"] { background-color: oklab(100% -100% 0.6); } ``` #### Result {{EmbedLiveSample("Adjusting_the_lightness_and_axes", "100%", 155)}} ### Adjusting opacity with oklab() The following example shows the effect of varying the `A` (alpha) value of the `oklab()` function. The `red` and `red-alpha` elements overlap the `#background-div` element to demonstrate the effect of opacity. Giving the `red-alpha` element an opacity of `0.4` makes it appear more transparent than the `red` element. #### HTML ```html <div id="background-div"> <div data-color="red"></div> <div data-color="red-alpha"></div> </div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } #background-div { background-color: lightblue; width: 150px; height: 40px; } ``` ```css [data-color="red"] { background-color: oklab(0.628 0.225 0.126); } [data-color="red-alpha"] { background-color: oklab(0.628 0.225 0.126 / 0.4); } ``` #### Result {{EmbedLiveSample("Adjusting_opacity_with_oklab", "100%", 155)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - The [`<color>` data type](/en-US/docs/Web/CSS/color_value) for a list of all color notations - {{cssxref("color_value/oklch",'oklch()')}}: Another functional notation using the same color space as `oklab()` but in a polar coordinate system - [A perceptual color space for image processing](https://bottosson.github.io/posts/oklab/) on bottosson.github.io (2023) - [OKLAB color wheel](https://observablehq.com/@shan/oklab-color-wheel) on observablehq.com
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/lch/index.md
--- title: lch() slug: Web/CSS/color_value/lch page-type: css-function browser-compat: css.types.color.lch --- {{CSSRef}} The **`lch()`** functional notation expresses a given color in the LCH color space. It has the same L axis as {{cssxref("color_value/lab","lab()")}}, but uses polar coordinates C (Chroma) and H (Hue). ## Syntax ```css lch(29.2345% 44.2 27); lch(52.2345% 72.2 56.2); lch(52.2345% 72.2 56.2 / .5); ``` ### Values Functional notation: `lch(L C H[ / A])` - `L` - : A {{CSSXref("&lt;number&gt;")}} between `0` and `100`, a {{CSSXref("&lt;percentage&gt;")}} between `0%` and `100%`, or the keyword `none`, which specifies the CIE Lightness. Here the number `0` corresponds to `0%` (black) and the number `100` corresponds to `100%` (white). - `C` - : A {{CSSXref("&lt;number&gt;")}}, a {{CSSXref("&lt;percentage&gt;")}}, or the keyword `none`, where `0%` is `0` and `100%` is the number `150`. It is a measure of the chroma (roughly representing the "amount of color"). Its minimum useful value is `0`, while its maximum is theoretically unbounded (but in practice does not exceed `230`). - `H` - : A {{CSSXref("&lt;number&gt;")}}, an {{CSSXref("&lt;angle&gt;")}}, or the keyword `none`, which represents the hue angle. More details on this type can be found on the {{CSSXref("&lt;hue&gt;")}} reference. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** Usually when percentage values have a numeric equivalent in CSS, `100%` is equal to the number `1`. > This case is notable where `100%` is equal to the number `100` for the `L` value and `150` for the `C` value. > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Examples ### Adjusting lightness, chroma, and hue with lch() The following example shows the effect of varying the `L` (lightness), `C` (chroma), and `H` (hue) values of the `lch()` functional notation. #### HTML ```html <div data-color="blue"></div> <div data-color="blue-light"></div> <div data-color="red"></div> <div data-color="red-chroma"></div> <div data-color="green"></div> <div data-color="green-hue"></div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } ``` ```css [data-color="blue"] { background-color: lch(0% 100 240); } [data-color="blue-light"] { background-color: lch(100% 100 240); } [data-color="red"] { background-color: lch(50% 130 20); } [data-color="red-chroma"] { background-color: lch(100% 30 20); } [data-color="green"] { background-color: lch(50% 132 130); } [data-color="green-hue"] { background-color: lch(50% 132 180); } ``` #### Result {{EmbedLiveSample("adjusting_lightness_chroma_and_hue_with_lch")}} ### Adjusting opacity with lch() The following example shows the effect of varying the `A` (alpha) value of the `lch()` functional notation. The `red` and `red-alpha` elements overlap the `#background-div` element to demonstrate the effect of opacity. Giving `A` a value of `0.4` makes the color 40% opaque. #### HTML ```html <div id="background-div"> <div data-color="red"></div> <div data-color="red-alpha"></div> </div> ``` #### CSS ```css hidden div { width: 50px; height: 50px; padding: 5px; margin: 5px; display: inline-block; border: 1px solid black; } #background-div { background-color: lch(100% 100 240); width: 150px; height: 40px; } ``` ```css [data-color="red"] { background-color: lch(50% 130 20); } [data-color="red-alpha"] { background-color: lch(50% 130 20 / 0.4); } ``` #### Result {{EmbedLiveSample("adjusting_opacity_with_lch")}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [List of all color notations](/en-US/docs/Web/CSS/color_value) - {{CSSXref("&lt;hue&gt;")}} data type - [LCH colors in CSS: what, why, and how?](https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/)
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/light-dark/index.md
--- title: light-dark() slug: Web/CSS/color_value/light-dark page-type: css-function browser-compat: css.types.color.light-dark --- {{CSSRef}} The **`light-dark()`** [CSS](/en-US/docs/Web/CSS) [`<color>` function](/en-US/docs/Web/CSS/CSS_Functions#color_functions) enables setting two colors for a property - returning one of the two colors options by detecting if the developer has set a light or dark color scheme or the user has requested light or dark color theme - without needing to encase the theme colors within a [`prefers-color-scheme`](/en-US/docs/Web/CSS/@media/prefers-color-scheme) [media feature](/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries#targeting_media_features) query. Users are able to indicate their color-scheme preference through their operating system settings (e.g. light or dark mode) or their user agent settings. The `light-dark()` function enables providing two color values where any `<color>` value is accepted. The `light-dark()` CSS color function returns the first value if the user's preference is set to `light` or if no preference is set and the second value if the user's preference is set to `dark`. To enable support for the `light-dark()` color function, the {{CSSXref("color-scheme")}} must have a value of `light dark`, usually set on the {{CSSXref(":root")}} [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes). ```css :root { color-scheme: light dark; } body { color: light-dark(#333b3c, #efefec); background-color: light-dark(#efedea, #223a2c); } ``` ## Syntax ```css /* Named color values */ color: light-dark(black, white); /* RGB color values */ color: light-dark(rgb(0 0 0), rgb(255 255 255)); /* Custom properties */ color: light-dark(var(--light), var(--dark)); ``` ### Values Functional notation: `light-dark(light-color, dark-color)` - `light-color` - : {{CSSXref("&lt;color&gt;")}} value to be set for light {{CSSXref("color-scheme")}}. - `dark-color` - : {{CSSXref("&lt;color&gt;")}} value to be set for dark {{CSSXref("color-scheme")}}. ### Formal syntax {{csssyntax}} ## Example ### Setting colors based on color scheme By default, in supporting browsers, the color returned by the `light-dark()` color function depends on the user preference set through an operating system's settings (e.g., light or dark mode) or from a user agent setting. You can also change this setting in the browser's {{glossary("developer tools")}}. #### HTML We include three sections to enable targeting light colors, dark colors, and the colors selected based on the user's preferred color scheme. ```html <h1><code>light-dark()</code> CSS function</h1> <section> <h2>Automatic</h2> <p>This section will react to the users system or user agent setting.</p> </section> <section class="light"> <h2>Light</h2> <p> This section will be light due to the <code>color-scheme: light;</code>. </p> </section> <section class="dark"> <h2>Dark</h2> <p>This section will be dark due to the <code>color-scheme: dark;</code>.</p> </section> ``` #### CSS We include colors for both light and dark themes. We also define `color-scheme` for the document on the `:root` to enable the `light-dark()` color function for the entire document. ```css-nolint :root { /* this has to be set to switch between light or dark */ color-scheme: light dark; --light-bg: ghostwhite; --light-color: darkslategray; --light-code: tomato; --dark-bg: darkslategray; --dark-color: ghostwhite; --dark-code: gold; } * { background-color: light-dark(var(--light-bg), var(--dark-bg)); color: light-dark(var(--light-color), var(--dark-color)); } code { color: light-dark(var(--light-code), var(--dark-code)); } ``` In addition to enabling the `light-dark()` function, the `color-scheme` property enables overriding a user's color scheme for document sections. Forcing a page section to only use a light or dark color scheme can be done by setting the `color-scheme` property to `light` or `dark`. > **Note:** Generally this should not be done, we are using it here for demonstation purposes. If the user has made a preference, you generally should not override their preferences. ```css .light { /* forces light color-scheme */ color-scheme: light; } .dark { /* forces dark color-scheme */ color-scheme: dark; } ``` ```css hidden section { padding: 0.8rem; } ``` #### Result {{EmbedLiveSample("setting_colors_based_on_color_scheme", "100%", 500)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{CSSXref("color-scheme")}} - {{CSSXref("&lt;color&gt;")}} - [CSS color](/en-US/docs/Web/CSS/CSS_colors) module
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/device-cmyk/index.md
--- title: device-cmyk() slug: Web/CSS/color_value/device-cmyk page-type: css-function browser-compat: css.types.color.device-cmyk spec-urls: https://drafts.csswg.org/css-color-5/#device-cmyk --- {{CSSRef}} The **`device-cmyk()`** functional notation is used to express CMYK colors in a device dependent way, specifying the cyan, magenta, yellow, and black components. This approach to color is useful when creating material to be output to a particular printer, when the output for particular ink combinations is known. CSS processors may attempt to approximate the color, however the end result is likely to be different to the printed result. ## Syntax ```css device-cmyk(0 81% 81% 30%); device-cmyk(0 81% 81% 30% / .5); device-cmyk(0 81% 81% 30% / .5, rgb(178 34 34)); ``` ### Values Functional notation: `device-cmyk(C M Y K[ / A][, color])` - `C`, `M`, `Y`, `K` - : {{CSSXref("number")}} or {{CSSXref("percentage")}} values providing the cyan, magenta, yellow, and black components of CMYK color. - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}}, where the number `1` corresponds to `100%` (full opacity). - `color` {{optional_inline}} - : An optional fallback {{CSSXref("&lt;color&gt;")}} to use if the user agent does not know how to translate the CMYK color to RGB. ### Formal syntax {{csssyntax}} ## Specifications {{Specifications}} ## Browser compatibility There is no browser implementing this feature.
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/color-contrast/index.md
--- title: color-contrast() slug: Web/CSS/color_value/color-contrast page-type: css-function status: - experimental browser-compat: css.types.color.color-contrast --- {{CSSRef}}{{SeeCompatTable}} The **`color-contrast()`** functional notation takes a {{cssxref("color_value","color")}} value and compares it to a list of other {{cssxref("color_value","color")}} values, selecting the one with the highest contrast from the list. ## Syntax ```css color-contrast(wheat vs tan, sienna, #d2691e) color-contrast(#008080 vs olive, var(--myColor), #d2691e) ``` ### Values Functional notation: `color-contrast(color vs color-list)` - `color` - : Any valid {{CSSXref("&lt;color&gt;")}}. - `vs` - : A literal token as a component of the syntax. - `color-list` - : A comma-separated list of at least two color values to compare with the first value. ## Specifications {{Specifications}} ## Browser compatibility {{Compat}}
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/color-mix/index.md
--- title: color-mix() slug: Web/CSS/color_value/color-mix page-type: css-function browser-compat: css.types.color.color-mix --- {{CSSRef}} The **`color-mix()`** functional notation takes two {{cssxref("&lt;color&gt;")}} values and returns the result of mixing them in a given colorspace by a given amount. ## Syntax ```css color-mix(in lch, plum, pink); color-mix(in lch, plum 40%, pink); color-mix(in srgb, #34c9eb 20%, white); color-mix(in hsl longer hue, hsl(120 100% 50%) 20%, white); ``` ### Values Functional notation: `color-mix(method, color1[ p1], color2[ p2])` - `method` - : A {{CSSXref("&lt;color-interpolation-method&gt;")}} specifying the interpolation color space. - `color1`, `color2` - : {{CSSXref("&lt;color&gt;")}} values to mix. - `p1`, `p2` {{optional_inline}} - : {{CSSXref("&lt;percentage&gt;")}} values between `0%` and `100%`, specifying the amount of each color to mix. They are normalized as follows: - If both `p1` and `p2` are omitted, then `p1 = p2 = 50%`. - If `p1` is omitted, then `p1 = 100% - p2`. - If `p2` is omitted, then `p2 = 100% - p1`. - If `p1 = p2 = 0%`, the function is invalid. - If `p1 + p2 ≠ 100%`, then `p1' = p1 / (p1 + p2)` and `p2' = p2 / (p1 + p2)`, where `p1'` and `p2'` are the normalization results. ### Formal syntax {{csssyntax}} ## Examples ### Mixing two colors In a supporting browser, the items have more blue, and therefore less white, as a higher percentage of `#34c9eb` is mixed in. When no value is given, the percentage defaults to 50%. #### HTML ```html <ul> <li>0%</li> <li>25%</li> <li>50%</li> <li>75%</li> <li>100%</li> <li></li> </ul> ``` #### CSS ```css hidden ul { display: flex; list-style-type: none; font-size: 150%; gap: 10px; border: 2px solid; padding: 10px; } li { padding: 10px; flex: 1; box-sizing: border-box; font-family: monospace; outline: 1px solid #34c9eb; text-align: center; } ``` ```css li:nth-child(1) { background-color: color-mix(in srgb, #34c9eb 0%, white); } li:nth-child(2) { background-color: color-mix(in srgb, #34c9eb 25%, white); } li:nth-child(3) { background-color: color-mix(in srgb, #34c9eb 50%, white); } li:nth-child(4) { background-color: color-mix(in srgb, #34c9eb 75%, white); } li:nth-child(5) { background-color: color-mix(in srgb, #34c9eb 100%, white); } li:nth-child(6) { background-color: color-mix(in srgb, #34c9eb, white); } ``` #### Result {{EmbedLiveSample("mixing_two_colors", "100%", 150)}} ### Using hue interpolation in color-mix() When using shorter hue interpolation, the resulting hue angle is halfway between the input angles when taking the shortest route around the color wheel. The longer hue interpolation method results in a hue angle which is the midpoint when taking the longer route around the color wheel. For more information, see {{cssxref("&lt;hue-interpolation-method&gt;")}}. ```html <div class="color-one">color one</div> <div class="color-two">color two</div> <div class="shorter">mixed shorter</div> <div class="longer">mixed longer</div> ``` #### CSS ```css hidden body { display: flex; flex-wrap: wrap; } div { border: 1px solid; font: bold 150% monospace; height: 100px; margin: 10px 5%; width: 30%; } ``` ```css .color-one { background-color: hsl(10 100% 50%); } .color-two { background-color: hsl(60 100% 50%); } .shorter { background-color: color-mix( in hsl shorter hue, hsl(10 100% 50%), hsl(60 100% 50%) ); } .longer { background-color: color-mix( in hsl longer hue, hsl(10 100% 50%), hsl(60 100% 50%) ); } ``` #### Result {{EmbedLiveSample("using_hue_interpolation_in_color_mix", "100%", 250)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{CSSXref("&lt;color&gt;")}} - {{CSSXref("&lt;color-interpolation-method&gt;")}} - {{cssxref("&lt;hue&gt;")}}
0
data/mdn-content/files/en-us/web/css/color_value
data/mdn-content/files/en-us/web/css/color_value/hsl/index.md
--- title: hsl() slug: Web/CSS/color_value/hsl page-type: css-function browser-compat: css.types.color.hsl --- {{CSSRef}} The **`hsl()`** functional notation expresses an {{glossary("RGB", "sRGB")}} color according to its _hue_, _saturation_, and _lightness_ components. An optional _alpha_ component represents the color's transparency. > **Note:** The legacy `hsla()` syntax is an alias for `hsl()`, accepting the same parameters and behaving in the same way. {{EmbedInteractiveExample("pages/css/function-hsl.html")}} Defining _complementary colors_ with `hsl()` can be done with a single formula, as they are positioned on the same diameter of the {{glossary("color wheel")}}. If `θ` is the hue angle of a color, its complementary one will have `180deg - θ` as its hue angle. ## Syntax ```css hsl(120deg 75% 25%) hsl(120deg 75% 25% / 60%) ``` The function also accepts a legacy syntax in which all values are separated with commas. ### Values Functional notation: `hsl(H S L[ / A])` - `H` - : A {{CSSXref("&lt;number&gt;")}}, an {{CSSXref("&lt;angle&gt;")}}, or the keyword `none`, which represents the hue angle. More details on this type can be found on the {{CSSXref("&lt;hue&gt;")}} reference. - `S` - : A {{CSSXref("&lt;percentage&gt;")}} or the keyword `none`, which represents saturation. Here `100%` is completely saturated, while `0%` is completely unsaturated (gray). - `L` - : A {{CSSXref("&lt;percentage&gt;")}} or the keyword `none`, which represents lightness. Here `100%` is white, `0%` is black, and `50%` is "normal". - `A` {{optional_inline}} - : An {{CSSXref("&lt;alpha-value&gt;")}} or the keyword `none`, where the number `1` corresponds to `100%` (full opacity). > **Note:** This functional notation serializes to sRGB values, and the values of the red, green, blue components may be rounded in serialization. > **Note:** See [Missing color components](/en-US/docs/Web/CSS/color_value#missing_color_components) for the effect of `none`. ### Formal syntax {{csssyntax}} ## Examples ### Using hsl() with conic-gradient() The `hsl()` function works well with [`conic-gradient()`](/en-US/docs/Web/CSS/gradient/conic-gradient) as both deal with angles. ```html hidden <div></div> ``` #### CSS ```css div { width: 100px; height: 100px; background: conic-gradient( hsl(360 100% 50%), hsl(315 100% 50%), hsl(270 100% 50%), hsl(225 100% 50%), hsl(180 100% 50%), hsl(135 100% 50%), hsl(90 100% 50%), hsl(45 100% 50%), hsl(0 100% 50%) ); clip-path: circle(closest-side); } ``` #### Result {{EmbedLiveSample("using_hsl_with_conic-gradient", "100%", 140)}} ### Legacy syntax: comma-separated values For legacy reasons, the `hsl()` function accepts a form in which all values are separated using commas. #### HTML ```html <div class="space-separated"></div> <div class="comma-separated"></div> ``` #### CSS ```css div { width: 100px; height: 50px; margin: 1rem; } div.space-separated { background-color: hsl(0 100% 50% / 50%); } div.comma-separated { background-color: hsl(0, 100%, 50%, 0.5); } ``` #### Result {{EmbedLiveSample("legacy_syntax_comma-separated_values", "100%", 150)}} ### Legacy syntax: hsla() The legacy `hsla()` syntax is an alias for `hsl()`. #### HTML ```html <div class="hsl"></div> <div class="hsla"></div> ``` #### CSS ```css div { width: 100px; height: 50px; margin: 1rem; } div.hsl { background-color: hsl(0 100% 50% / 50%); } div.hsla { background-color: hsla(0, 100%, 50%, 0.5); } ``` #### Result {{EmbedLiveSample("legacy_syntax_hsla", "100%", 150)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [List of all color notations](/en-US/docs/Web/CSS/color) - {{CSSXref("&lt;hue&gt;")}} data type - [Color picker tool](/en-US/docs/Web/CSS/CSS_colors/Color_picker_tool) on MDN - [Color picker](https://colorjs.io/apps/picker/) by Lea Verou
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/log/index.md
--- title: log() slug: Web/CSS/log page-type: css-function browser-compat: css.types.log --- {{CSSRef}} The **`log()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) is an exponential function that returns the logarithm of a number. [Logarithm](https://en.wikipedia.org/wiki/Logarithm) is the inverse of exponentiation. It is the number that a fixed base has to be raised to in order to yield the number passed as the first parameter. In CSS, when a single parameter is passed, the natural logarithm `e`, or approximately `2.7182818`, is used, though the base can be set to any value with an optional second parameter. ## Syntax ```css /* A <number> value */ width: calc(100px * log(7.389)); /* 200px */ width: calc(100px * log(8, 2)); /* 300px */ width: calc(100px * log(625, 5)); /* 400px */ ``` ### Parameters The `log(value [, base]?)` function accepts two comma-separated values as its parameters. - `value` - : A calculation which resolves to a {{cssxref("&lt;number&gt;")}} greater than or equal to 0. Representing the value to be logarithmed. - `base` - : Optional. A calculation which resolves to a {{cssxref("&lt;number&gt;")}} greater than or equal to 0. Representing the base of the logarithm. If not defined, the default logarithmic base `e` is used. ### Return value The logarithm of `value`, when `base` is defined. The natural logarithm (base `e`) of `value`, when `base` is not defined. ### Formal syntax {{CSSSyntax}} ## Examples ### Using the `log()` function on a logarithmic scale This example illustrates how the `log()` function can be used to visualize data values by using a [logarithmic scale](https://en.wikipedia.org/wiki/Logarithmic_scale). The width of each bar in this example is relative to its data value on a logarithmic scale with base 10. On each element, its value is assigned to a [CSS custom property](/en-US/docs/Web/CSS/Using_CSS_custom_properties) named `--value`, which is then used by the `.bar` class to calculate its width. #### HTML ```html <div class="bar" style="--value: 50">50</div> <div class="bar" style="--value: 100">100</div> <div class="bar" style="--value: 500">500</div> <div class="bar" style="--value: 10000">10,000</div> <div class="bar" style="--value: 2000000">2,000,000</div> ``` #### CSS ```css .bar { width: calc(log(var(--value), 10) * 2em); } ``` ```css hidden .bar { height: 2em; background-color: tomato; color: white; display: flex; align-items: center; justify-content: center; margin: 0.25em 0; } ``` #### Result {{EmbedLiveSample('Using the log() function on a logarithmic scale', '100%', '200px')}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - {{CSSxRef("pow")}} - {{CSSxRef("sqrt")}} - {{CSSxRef("hypot")}} - {{CSSxRef("exp")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/padding-block-end/index.md
--- title: padding-block-end slug: Web/CSS/padding-block-end page-type: css-property browser-compat: css.properties.padding-block-end --- {{CSSRef}} The **`padding-block-end`** [CSS](/en-US/docs/Web/CSS) property defines the logical block end padding of an element, which maps to a physical padding depending on the element's writing mode, directionality, and text orientation. {{EmbedInteractiveExample("pages/css/padding-block-end.html")}} ## Syntax ```css /* <length> values */ padding-block-end: 10px; /* An absolute length */ padding-block-end: 1em; /* A length relative to the text size */ /* <percentage> value */ padding-block-end: 5%; /* A padding relative to the block container's width */ /* Global values */ padding-block-end: inherit; padding-block-end: initial; padding-block-end: revert; padding-block-end: revert-layer; padding-block-end: unset; ``` ### Values - {{cssxref("&lt;length&gt;")}} - : The size of the padding as a fixed value. Must be nonnegative. - {{cssxref("&lt;percentage&gt;")}} - : The size of the padding as a percentage, relative to the inline size (_width_ in a horizontal language, defined by {{cssxref("writing-mode")}}) of the [containing block](/en-US/docs/Web/CSS/Containing_block). Must be nonnegative. ## Description The `padding-block-end` property is defined in the specification as taking the same values as the {{cssxref("padding-top")}} property. However, the physical property it maps to depends on the values set for {{cssxref("writing-mode")}}, {{cssxref("direction")}}, and {{cssxref("text-orientation")}}. Therefore, it could map to {{cssxref("padding-bottom")}}, {{cssxref("padding-right")}}, or {{cssxref("padding-left")}}. It relates to {{cssxref("padding-block-start")}}, {{cssxref("padding-inline-start")}}, and {{cssxref("padding-inline-end")}}, which define the other paddings of the element. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting block end padding for vertical text #### HTML ```html <div> <p class="exampleText">Example text</p> </div> ``` #### CSS ```css div { background-color: yellow; width: 120px; height: 120px; } .exampleText { writing-mode: vertical-lr; padding-block-end: 20px; background-color: #c8c800; } ``` #### Result {{EmbedLiveSample("Setting_block_end_padding_for_vertical_text", 140, 140)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS Logical Properties and Values](/en-US/docs/Web/CSS/CSS_logical_properties_and_values) - The mapped physical properties: {{cssxref("padding-top")}}, {{cssxref("padding-right")}}, {{cssxref("padding-bottom")}}, and {{cssxref("padding-left")}} - {{cssxref("writing-mode")}}, {{cssxref("direction")}}, {{cssxref("text-orientation")}}
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/css_compositing_and_blending/index.md
--- title: CSS compositing and blending slug: Web/CSS/CSS_compositing_and_blending page-type: css-module spec-urls: - https://drafts.fxtf.org/compositing/ - https://www.w3.org/TR/compositing-1/ --- {{CSSRef}} The **CSS compositing and blending** module defines how an element's background layers can be blended together, how an element can be blended with its container, and whether the element must create a new [stacking context](/en-US/docs/Web/CSS/CSS_positioned_layout/Understanding_z-index/Stacking_context). The properties in this CSS module can be used to define the blending mode that should be used, if any, to blend an element's background images and colors into a single background image. This module provides 16 blending modes. You can also define how an element's borders, background, and content, including text, emojis, and images, should be blended with the background of its container. ### Compositing and blending in action In this example, each box has a border, two striped background images, and a solid color background. The common background for all the boxes contains a pattern of circles. The three boxes in the second row are set to blend with the background of the container. {{EmbedGHLiveSample("css-examples/modules/compositing.html", '100%', 460)}} Notice how the background, border, and the content are all impacted as a result of the blending. To see the code for this sample, [view the source on GitHub](https://github.com/mdn/css-examples/blob/main/modules/compositing.html). ## Reference ### Properties - {{cssxref("background-blend-mode")}} - {{cssxref("isolation")}} - {{cssxref("mix-blend-mode")}} ## Related concepts - {{cssxref("blend-mode")}} data type - {{cssxref("backdrop-filter")}} CSS property - {{cssxref("filter")}} CSS property - {{cssxref("mask-composite")}} CSS property - {{cssxref("background-color")}} CSS property - {{cssxref("background-image")}} CSS property - {{glossary("stacking context")}} glossary term - {{ SVGElement("feBlend")}} SVG filter primitive - {{ SVGElement("feComposite")}} SVG filter primitive ## Specifications {{Specifications}} ## See also - Properties in the [CSS filter effects](/en-US/docs/Web/CSS/CSS_filter_effects) module enable applying filters effects, such as blurring and changing color intensity, to images, backgrounds, and borders. - [Compositing And Blending In CSS](https://www.sarasoueidan.com/blog/compositing-and-blending-in-css/) (2015) - [Editing Images in CSS: Blend Modes](https://code.tutsplus.com/tutorials/editing-images-in-css-blend-modes--cms-26058) (2022) - [web.dev: blend modes](https://web.dev/learn/css/blend-modes) (2021)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/scroll-padding-top/index.md
--- title: scroll-padding-top slug: Web/CSS/scroll-padding-top page-type: css-property browser-compat: css.properties.scroll-padding-top --- {{CSSRef}} The **`scroll-padding-top`** property defines offsets for the top of the _optimal viewing region_ of the scrollport: the region used as the target region for placing things in view of the user. This allows the author to exclude regions of the scrollport that are obscured by other content (such as fixed-positioned toolbars or sidebars) or to put more breathing room between a targeted element and the edges of the scrollport. {{EmbedInteractiveExample("pages/css/scroll-padding-top.html")}} ## Syntax ```css /* Keyword values */ scroll-padding-top: auto; /* <length> values */ scroll-padding-top: 10px; scroll-padding-top: 1em; scroll-padding-top: 10%; /* Global values */ scroll-padding-top: inherit; scroll-padding-top: initial; scroll-padding-top: revert; scroll-padding-top: revert-layer; scroll-padding-top: unset; ``` ### Values - `{{cssxref("&lt;length-percentage&gt;")}}` - : An inwards offset from the top edge of the scrollport, as a valid length or a percentage. - `auto` - : The offset is determined by the user agent. This will generally be 0px, but a user agent is able to detect and do something else if a non-zero value is more appropriate. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - [CSS scroll snap](/en-US/docs/Web/CSS/CSS_scroll_snap) - [Well-controlled scrolling with CSS scroll snap](https://web.dev/articles/css-scroll-snap)
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/inset/index.md
--- title: inset slug: Web/CSS/inset page-type: css-shorthand-property browser-compat: css.properties.inset --- {{CSSRef}} The **`inset`** [CSS](/en-US/docs/Web/CSS) property is a shorthand that corresponds to the {{cssxref("top")}}, {{cssxref("right")}}, {{cssxref("bottom")}}, and/or {{cssxref("left")}} properties. It has the same multi-value syntax of the {{cssxref("margin")}} shorthand. {{EmbedInteractiveExample("pages/css/inset.html")}} While part of the _CSS Logical Properties_ specification, it does not define _logical_ offsets. It defines _physical_ offsets, regardless of the element's writing mode, directionality, and text orientation. ## Constituent properties This property is a shorthand for the following CSS properties: - {{Cssxref("top")}} - {{Cssxref("right")}} - {{Cssxref("bottom")}} - {{Cssxref("left")}} ## Syntax ```css /* <length> values */ inset: 10px; /* value applied to all edges */ inset: 4px 8px; /* top/bottom left/right */ inset: 5px 15px 10px; /* top left/right bottom */ inset: 2.4em 3em 3em 3em; /* top right bottom left */ /* <percentage>s of the width (left/right) or height (top/bottom) of the containing block */ inset: 10% 5% 5% 5%; /* Keyword value */ inset: auto; /* Global values */ inset: inherit; inset: initial; inset: revert; inset: revert-layer; inset: unset; ``` ### Values The `inset` property takes the same values as the {{cssxref("left")}} property. ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Setting offsets for an element #### HTML ```html <div> <span class="exampleText">Example text</span> </div> ``` #### CSS ```css div { background-color: yellow; width: 150px; height: 120px; position: relative; } .exampleText { writing-mode: sideways-rl; position: absolute; inset: 20px 40px 30px 10px; background-color: #c8c800; } ``` #### Result {{EmbedLiveSample("Setting_offsets_for_an_element", 140, 140)}} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - The longhand box offset properties: {{cssxref("top")}}, {{cssxref("right")}}, {{cssxref("bottom")}}, and {{cssxref("left")}}. - The mapped logical shorthands: {{cssxref("inset-block")}} and {{cssxref("inset-inline")}} - The {{cssxref("margin")}} shorthand multi-value syntax.
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/css_scroll_snap/index.md
--- title: CSS scroll snap slug: Web/CSS/CSS_scroll_snap page-type: css-module spec-urls: https://drafts.csswg.org/css-scroll-snap/ --- {{CSSRef}} The **CSS scroll snap** module provides properties that let you control the panning and scrolling behavior by defining snap positions. Content can be snapped into position as the user scrolls overflowing content within a {{Glossary("scroll container")}}, providing paging and scroll positioning. This module includes the scroll container scroll-padding properties to adjust the optimal viewing region of paging during scroll-into-view operations. It also includes scroll-margin and scroll-alignment, set on the scroll container's children, to adjust the children's visual area when that child is scrolled into view, as well as a property to force scrolling to stop on individual children. ## Scroll snap in action To view scroll snapping in the box below, scroll up-and-down and left-and-right through the grid of 45 numbered boxes in the scrollable viewport. {{EmbedGHLiveSample("css-examples/modules/scroll_snap.html", '100%', 250)}} With scroll snap, one of the numbered boxes that you scroll to will snap into place. The initial CSS makes the numbered box snap into the center of the viewport. Use the sliders to change the block and inline snap positions. Using snap properties, you can allow or block the scrolling past an element, a numbered box in this case. Select the "Prevent scrolling past boxes" checkbox to force all scrolling actions to be limited to scrolling to an adjacent box. To compare scroll snapping to regular scrolling, check the "disable snapping" checkbox and try scrolling again. To see the code for this example, [view the source on GitHub](https://github.com/mdn/css-examples/blob/main/modules/scroll_snap.html). ## Reference ### Properties on containers - {{cssxref("scroll-snap-type")}} - {{cssxref("scroll-padding")}} - {{cssxref("scroll-padding-top")}} - {{cssxref("scroll-padding-right")}} - {{cssxref("scroll-padding-bottom")}} - {{cssxref("scroll-padding-left")}} - {{cssxref("scroll-padding-inline")}} - {{cssxref("scroll-padding-inline-start")}} - {{cssxref("scroll-padding-inline-end")}} - {{cssxref("scroll-padding-block")}} - {{cssxref("scroll-padding-block-start")}} - {{cssxref("scroll-padding-block-end")}} ### Properties on children - {{cssxref("scroll-snap-align")}} - {{cssxref("scroll-margin")}} - {{cssxref("scroll-margin-top")}} - {{cssxref("scroll-margin-right")}} - {{cssxref("scroll-margin-bottom")}} - {{cssxref("scroll-margin-left")}} - {{cssxref("scroll-margin-inline")}} - {{cssxref("scroll-margin-inline-start")}} - {{cssxref("scroll-margin-inline-end")}} - {{cssxref("scroll-margin-block")}} - {{cssxref("scroll-margin-block-start")}} - {{cssxref("scroll-margin-block-end")}} - {{cssxref("scroll-snap-stop")}} ## Guides - [Basic concepts of CSS scroll snap](/en-US/docs/Web/CSS/CSS_scroll_snap/Basic_concepts) - : An overview and examples of CSS scroll snap features. ## Related concepts - {{cssxref(":target")}} pseudo-class - {{cssxref("overflow")}} CSS property - Element {{domxref("Element.scroll", "scroll()")}} method - Element {{domxref("Element.scrollBy", "scrollBy()")}} method - Element {{domxref("Element.scrollIntoView", "scrollIntoView()")}} method - Element {{domxref("Element.scrollTo", "scrollTo()")}} method - Document {{domxref("Document.scroll_event", "scroll")}} event - [`scrollbar`](/en-US/docs/Web/Accessibility/ARIA/Roles/scrollbar_role) ARIA role - {{Glossary("Scroll container")}} glossary term ## Specifications {{Specifications}} ## See also - [CSS overflow](/en-US/docs/Web/CSS/CSS_overflow) module - [CSS scrollbars styling](/en-US/docs/Web/CSS/CSS_scrollbars_styling) module - [Keyboard-only scrolling areas](https://adrianroselli.com/2022/06/keyboard-only-scrolling-areas.html) on adrianroselli.com (2022) - [Scroll snap examples](https://codepen.io/collection/KpqBGW) on Codepen (2022) - [Well-controlled scrolling with CSS scroll snap](https://web.dev/articles/css-scroll-snap) on web.dev (2021) - [Practical CSS scroll snapping/](https://css-tricks.com/practical-css-scroll-snapping/) on CSS-Tricks (2020) - [CSS scroll snap](https://12daysofweb.dev/2022/css-scroll-snap/) on 12 Days of Web (2019)
0
data/mdn-content/files/en-us/web/css/css_scroll_snap
data/mdn-content/files/en-us/web/css/css_scroll_snap/basic_concepts/index.md
--- title: Basic concepts of scroll snap slug: Web/CSS/CSS_scroll_snap/Basic_concepts page-type: guide --- {{CSSRef}} The properties in the [CSS scroll snap](/en-US/docs/Web/CSS/CSS_scroll_snap) module enable you to define how scrolling snaps to specific points as a user scrolls through a document. The scroll snap feature lets you define the snap positions where a [scroll container](/en-US/docs/Glossary/Scroll_container)'s scrollport may end or "snap to" after a scrolling operation has completed. ## Key properties for CSS scroll snap Before you can define scroll snapping, you need to enable scrolling on a scroll container. You can do this by ensuring that the scroll container has a defined size and that it has {{cssxref("overflow")}} enabled. You can then define scroll snapping on the scroll container by using the following two key properties: - {{cssxref("scroll-snap-type")}}: Using this property, you can define whether or not the scrollable viewport can be snapped to, whether snapping is required or optional, and the axis on which the snapping should occur. - {{cssxref("scroll-snap-align")}}: This property is set on every child of the scroll container and you can use it to define each child's snap position or lack thereof. - {{cssxref("scroll-snap-stop")}}: This property ensures that a child is snapped to during scrolling and not passed over. - {{cssxref("scroll-margin")}}: This property can be set on child elements that are snapped to during scrolling to create an outset from the defined box. - {{cssxref("scroll-padding")}}: This property can be set on the scroll container to create a snapping offset. The example below demonstrates scroll snapping along the vertical axis, which is defined by `scroll-snap-type`. Additionally, `scroll-snap-align` applies on all the children of the `<section>` element, dictating the point where the scrolling of each child should stop. {{EmbedGHLiveSample("css-examples/scroll-snap/mandatory-y.html", '100%', 700)}} ## Using scroll-snap-type The {{CSSxRef("scroll-snap-type")}} property needs to know the direction in which scroll snapping happens. This can be `x`, `y`, or the logical mappings `block` or `inline`. You can also use the keyword `both` to have scroll snapping work along both axes. You can also pass in the keywords `mandatory` or `proximity`. The `mandatory` keyword tells the browser whether the content _has_ to snap to a certain point, no matter where the scroll is. The `proximity` keyword means that the content may snap to the point, but does not have to. Using `mandatory` creates a very consistent scrolling experience — you know the browser will always snap to each defined point. This means that you can be confident that something you expect to be at the top of the screen will be there when scrolling finishes. However, it can cause problems if the content is larger than you expect — users may find themselves in the frustrating position of never being able to scroll and view a certain point in the content. Therefore, the use of `mandatory` should be carefully considered and only used in situations where you know how much content is on the screen or scrollable section at any time. > **Note:** Never use `mandatory` if the content inside one of your child elements will overflow the parent container because user will not be able to scroll the overflowing content into view. The `proximity` value only snaps child elements to a position when it is close by, with the browsers determining the exact distance. In the example below, you can change the value between `mandatory` and `proximity` to see the effect this has on the scroll experience. {{EmbedGHLiveSample("css-examples/scroll-snap/mandatory-proximity.html", '100%', 700)}} In the above example, both {{cssxref("height", "height: 300px;")}} and {{cssxref("overflow-y", "overflow-y: scroll;")}} are set on the scroll container. If the content doesn't overflow its container, there is nothing to scroll. ## Using scroll-snap-align The valid values for the {{CSSxRef("scroll-snap-align")}} property include `start`, `end`, `center`, and `none`. These values are used to indicate the point in the scroll container to which the content should snap. In the example below, you can change the value of `scroll-snap-align` to see how this changes the scroll behavior. {{EmbedGHLiveSample("css-examples/scroll-snap/align.html", '100%', 700)}} If `scroll-snap-type` is `mandatory` and `scroll-snap-align` on a child is either set to `none` or not set (in which case, it defaults to `none`), the user will be unable to scroll that element into view. ## Using scroll-padding When using `start` or `end`, if you do not want the content to snap right to the edge of the scroll container, or if you want the snap position to be slightly offset from center when using `center`, use the {{CSSxRef("scroll-padding")}} property or its equivalent longhand values to add some padding. In the example below, `scroll-padding` is set to `40px`. When the content snaps to the start of the second and third sections, scrolling stops 40 pixels away from the start of the section. Try changing the `scroll-padding` value to see how this changes the distance. {{EmbedGHLiveSample("css-examples/scroll-snap/scroll-padding.html", '100%', 700)}} This is potentially useful if you have a [fixed](/en-US/docs/Web/CSS/position#fixed_positioning) element such as a navigation bar, which could end up overlapping scrolled content. By using `scroll-padding`, you can reserve space for the fixed element, as shown in the example below, where the `<h1>` element remains on screen as the content scrolls beneath it. Without padding, the heading would overlap some of the content when snapping happens. {{EmbedGHLiveSample("css-examples/scroll-snap/scroll-padding-sticky.html", '100%', 700)}} ## Using scroll-margin The {{CSSxRef("scroll-margin")}} property or the longhand scroll margin values can be set on child elements, defining an outset from the defined box. This allows for different amounts of space for different child elements and can be used in conjunction with `scroll-padding` on the parent. Try this in the example below. {{EmbedGHLiveSample("css-examples/scroll-snap/scroll-margin.html", '100%', 700)}} ## Using scroll-snap-stop Using the {{CSSxRef("scroll-snap-stop")}} property, you can specify whether the scrolling must snap to the defined snap points. In the above examples, this would mean that the scrolling would stop at the start of each section or be able to skip past sections. With this property definition, you can ensure that users see each section of the scroller and don't accidentally scroll past them. However, this setting can also negatively affect user experience by preventing the user from quickly scrolling to their desired content. ## See also - [CSS scroll snap](/en-US/docs/Web/CSS/CSS_scroll_snap) module - [Well-controlled scrolling with CSS scroll snap](https://web.dev/articles/css-scroll-snap) on web.dev (2021) - [Practical CSS scroll snapping](https://css-tricks.com/practical-css-scroll-snapping/) on CSS-Tricks (2020) - [CSS scroll snap](https://12daysofweb.dev/2022/css-scroll-snap/) on 12 Days of Web (2019) - [Scroll snap examples](https://codepen.io/collection/KpqBGW) on Codepen
0
data/mdn-content/files/en-us/web/css
data/mdn-content/files/en-us/web/css/order/index.md
--- title: order slug: Web/CSS/order page-type: css-property browser-compat: css.properties.order --- {{CSSRef}} The **`order`** [CSS](/en-US/docs/Web/CSS) property sets the order to lay out an item in a flex or grid container. Items in a container are sorted by ascending `order` value and then by their source code order. Items not given an explicit `order` value are assigned the default value of `0`. {{EmbedInteractiveExample("pages/css/order.html")}} In the above demo, select the options on the left-hand side to change the value of the purple box's `order` property. The blue boxes have been given fixed `order` values. Bear in mind the effect of source order. When `order: 2;` is selected for example, the purple box is placed before the two blue boxes with `order: 2;`. This is because the purple box appears before the blue boxes in the source code. ## Syntax ```css /* <integer> values */ order: 5; order: -5; /* Global values */ order: inherit; order: initial; order: revert; order: revert-layer; order: unset; ``` Since `order` is only meant to affect the _visual order_ of elements and not their logical or tab order. `order` must not be used on non-visual media such as [speech](/en-US/docs/Web/CSS/@media#speech). ### Values - {{cssxref("&lt;integer&gt;")}} - : Represents the ordinal group to be used by the item. ## Accessibility concerns Using the `order` property will create a disconnect between the visual presentation of content and DOM order. This will adversely affect users experiencing low vision navigating with the aid of assistive technology such as a screen reader. If the visual (css) order is important, then screen reader users will not have access to the correct reading order. - [Flexbox & the keyboard navigation disconnect — Tink](https://tink.uk/flexbox-the-keyboard-navigation-disconnect/) - [Source Order Matters | Adrian Roselli](https://adrianroselli.com/2015/09/source-order-matters.html) - [MDN Understanding WCAG, Guideline 1.3 explanations](/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable#guideline_1.3_%e2%80%94_create_content_that_can_be_presented_in_different_ways) - [Understanding Success Criterion 1.3.2 | W3C Understanding WCAG 2.0](https://www.w3.org/TR/UNDERSTANDING-WCAG20/content-structure-separation-sequence.html) ## Formal definition {{cssinfo}} ## Formal syntax {{csssyntax}} ## Examples ### Ordering items in a flex container This example uses CSS to create a classic two-sidebar layout surrounding a content block. The Flexible Box Layout Module automatically creates blocks of equal vertical size and uses as much horizontal space as available. #### HTML ```html <header>…</header> <main> <article>Article</article> <nav>Nav</nav> <aside>Aside</aside> </main> <footer>…</footer> ``` #### CSS ```css main { display: flex; text-align: center; } main > article { flex: 1; order: 2; } main > nav { width: 200px; order: 1; } main > aside { width: 200px; order: 3; } ``` #### Result {{ EmbedLiveSample('Ordering_items_in_a_flex_container') }} ## Specifications {{Specifications}} ## Browser compatibility {{Compat}} ## See also - CSS Flexbox Guide: _[Basic Concepts of Flexbox](/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox)_ - CSS Flexbox Guide: _[Ordering flex items](/en-US/docs/Web/CSS/CSS_flexible_box_layout/Ordering_flex_items)_ - CSS Grid Guide: _[CSS Grid Layout and accessibility](/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_and_accessibility)_
0