-
Notifications
You must be signed in to change notification settings - Fork 22.8k
Editorial review: Document CSS if() function #39516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Preview URLs
Flaws (4)Note! 1 document with no flaws that don't need to be listed. 🎉 URL:
URL:
(comment last updated: 2025-07-01 18:29:47) |
Looks good. I don't have permission to click "Approve" here, though. |
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heading to the airport, so will finish later.
files/en-us/web/css/if/index.md
Outdated
} | ||
``` | ||
|
||
In this case, we are setting a different {{cssxref("background-image")}} {{cssxref("linear-gradient()")}} value on {{htmlelement("div")}} elements depending on whether a `--scheme` [custom property](/en-US/docs/Web/CSS/--*) is set to `ice` or `fire`. If neither value is set, the `else` value comes into play, and the `background-image` property is set to `none`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we are setting a different {{cssxref("background-image")}} {{cssxref("linear-gradient()")}} value on {{htmlelement("div")}} elements depending on whether a `--scheme` [custom property](/en-US/docs/Web/CSS/--*) is set to `ice` or `fire`. If neither value is set, the `else` value comes into play, and the `background-image` property is set to `none`. | |
In this case, we are setting a different {{cssxref("linear-gradient()")}} values as the {{cssxref("background-image")}} on {{htmlelement("div")}} elements, depending on whether a `--scheme` [custom property](/en-US/docs/Web/CSS/--*) is set to `ice` or `fire`. If `--scheme` doesn't exist, or it exists and is set to any other value, the `else` value comes into play, and the `background-image` property is set to `none`. |
First suggestion is just so the two links aren't next to each other without content between them (cognitive: clearer that they are separate links)
second suggestion is to kind of indicate that a non-existent custom prop won't invalidate the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable. I used it with a couple of tweaks:
In this case, we are setting a different {{cssxref("linear-gradient()")}} as the {{cssxref("background-image")}} on {{htmlelement("div")}} elements, depending on whether a
--scheme
custom property is set toice
orfire
. If--scheme
doesn't exist, or it exists and is set to any other value, theelse
value comes into play, and thebackground-image
property is set tonone
.
files/en-us/web/css/if/index.md
Outdated
> [!WARNING] | ||
> There must be no space between the `if` and the opening parenthesis (`(`). If there is, the whole declaration is invalid. | ||
|
||
If a single `<if-condition>` or `<value>` is invalid, it does not invalidate the entire `if()` function; instead, the parser moves on to the next `<if-condition> : <value>` pair. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a single `<if-condition>` or `<value>` is invalid, it does not invalidate the entire `if()` function; instead, the parser moves on to the next `<if-condition> : <value>` pair. | |
If a single `<if-condition>` or `<value>` is invalid, it does not invalidate the entire `if()` function; instead, the parser moves on to the next `<if-condition> : <value>` pair. If no `<if-condition>` nor `<value>` valid, the function returns a guaranteed-invalid. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly used, with a couple of tweaks:
If a single
<if-condition>
or<value>
is invalid, it does not invalidate the entireif()
function; instead, the parser moves on to the next<if-condition> : <value>
pair. If no<if-condition>
nor<value>
is valid, the function returns {{glossary("guaranteed_invalid_value", "guaranteed-invalid")}}.
files/en-us/web/css/if/index.md
Outdated
|
||
You can include multiple `else : <value>` pairs inside an `if()` function, in any position. However, in most cases, a single `else : <value>` pair at the end of the semi-colon-separated list is used to provide the default value that is always returned if none of the `<if-test>`s evaluate to true. | ||
|
||
If you include an `else : <value>` pair before any `<if-test> : <value>` pairs, the later conditions are not evaluated because the `else` always evaluates to `true`. The following `if()` therefore always returns `none`, and the two `<if-test> : <value>` pairs are never evaluated: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you include an `else : <value>` pair before any `<if-test> : <value>` pairs, the later conditions are not evaluated because the `else` always evaluates to `true`. The following `if()` therefore always returns `none`, and the two `<if-test> : <value>` pairs are never evaluated: | |
If you include an `else : <value>` pair before any `<if-test> : <value>` pairs, the later conditions are not evaluated because `else` evaluates to `true`. The following `if()` therefore always returns `none`, and the two `<if-test> : <value>` pairs are never evaluated: |
If you don't go with this edit, we should still get rid of "the" unless you add "keyword"
should "later" be "latter"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just removed the "the". I think it sounds better and more definite with "always" included. "latter" is not quite right because I want to include all of the values after the else : <value>
pair; I think "latter" implies just the ones at the end.
I thought about it a bit, and went with "the conditions that follow it":
If you include an
else : <value>
pair before any<if-test> : <value>
pairs, the conditions that follow it are not evaluated becauseelse
always evaluates totrue
. The followingif()
therefore always returnsnone
, and the two<if-test> : <value>
pairs are never evaluated:
WDYT?
files/en-us/web/css/if/index.md
Outdated
} | ||
``` | ||
|
||
One circumstance in which you might want to put an `else : <value>` in a position other than the end of the values list is when a value is not behaving as expected, and you are trying to debug it. In the following example, we are trying to work out whether the first `<if-test> : <value>` pair is working properly. If it isn't, the `else : <value>` pair returns a value of `url("debug.png")` to display an image indicating that the first `<if-test> : <value>` pair needs fixing. The last two `<if-test> : <value>` pairs are again never evaluated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One circumstance in which you might want to put an `else : <value>` in a position other than the end of the values list is when a value is not behaving as expected, and you are trying to debug it. In the following example, we are trying to work out whether the first `<if-test> : <value>` pair is working properly. If it isn't, the `else : <value>` pair returns a value of `url("debug.png")` to display an image indicating that the first `<if-test> : <value>` pair needs fixing. The last two `<if-test> : <value>` pairs are again never evaluated. | |
Debugging a value is not behaving as expected is a case in which you might want to put an `else : <value>` in a position other than the end of the values list. In the following example, we are trying to work out whether the first `<if-test> : <value>` pair is working properly. If it isn't, the `else : <value>` pair returns a value of `url("debug.png")` to display an image indicating that the first `<if-test> : <value>` pair needs fixing. The last two `<if-test> : <value>` pairs are again never evaluated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I played with this a bit, and ended up with:
Debugging a value that is not behaving as expected is one case where you might want to put an
else : <value>
in a position other than the end of the value list. In the following example, we are trying to work out whether the first<if-test> : <value>
pair is working properly. If it isn't, theelse : <value>
pair returns a value ofurl("debug.png")
to display an image indicating that the first<if-test> : <value>
pair needs fixing. The last two<if-test> : <value>
pairs are again never evaluated.
files/en-us/web/css/if/index.md
Outdated
|
||
### Nesting if() functions | ||
|
||
Because an `if()` function can take the place of whole property values or individual components, it is possible to nest multiple `if()` functions, and nest `if()` functions inside other functions such as {{cssxref("calc()")}}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because an `if()` function can take the place of whole property values or individual components, it is possible to nest multiple `if()` functions, and nest `if()` functions inside other functions such as {{cssxref("calc()")}}. | |
Because an `if()` function can take the place of whole property values or individual components, it is possible to nest `if()` functions within other `if()` functions, and to nest `if()` functions inside other functions such as {{cssxref("calc()")}}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was mostly good; I tightened it up a bit to:
Because an
if()
function can take the place of whole property values or individual components, it is possible to nestif()
functions within otherif()
functions, and inside other functions such as {{cssxref("calc()")}}.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a couple of the edits are suggetions to get the reader excited. all suggestions are minor.
|
||
Our HTML contains an {{htmlelement("article")}} element with some content inside it — a top-level heading, a couple of {{htmlelement("p")}} elements, and an {{htmlelement("aside")}}. Below that we include a {{htmlelement("form")}} containing a {{htmlelement("select")}} drop-down that allows you to select a color scheme. | ||
|
||
```html-nolint live-sample___color-scheme |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the nolint so ipsum isn't spell-checked?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it was just because prettier made the code layout a bit weird.
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
Description
Chrome supports the CSS
if()
function from version 137. See https://chromestatus.com/feature/6313805904347136.This PR documents the new function. Specifically, it:
if()
reference page (nearly done, just needs examples)if()
to the CSS values and units module pageif()
to the CSS value functions ref page.Motivation
Additional details
Related issues and pull requests