Day 6: the :has() pseudo-class

It’s time to get me up on speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve started #100DaysOfMoreOrLessModernCSS. Why more or less modern CSS? Because some topics will be about cutting-edge features, while other stuff has been around for quite a while already, but I just have little to no experience with it.

:has() allows you to check whether a parent element contains specific children.

In the following example, each .form-item that contains/has a child with the aria-invalid attribute set to “true” displays text in red color. (currently only in Chrome/Edge 105+ and Safari 15.4+)

<form>
<div class="form-item">
<label for="name">Name</label><br>
<input type="text" id="name" required aria-invalid="true">
</div>

<div class="form-item">
<label for="email">E-Mail</label><br>
<input type="text" id="email">
</div>
</form>

.form-item {
--color: #000;

/* The default color is #000 */
color: var(--color);
}

input {
/* The default border-color is #000 */
border: 1px solid var(--color);
}

/* If the .form-item contains an element with [aria-invalid="true"],
the text and border color changes to #F00 */

.form-item:has([aria-invalid="true"]) {
--color: #F00;
}

It’s time to get me up on speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve started #100DaysOfMoreOrLessModernCSS. Why more or less modern CSS? Because some topics will be about cutting-edge features, while other stuff has been around for quite a while already, but I just have little to no experience with it.

:has() allows you to check whether a parent element contains specific children.

In the following example, each .form-item that contains/has a child with the aria-invalid attribute set to “true” displays text in red color. (currently only in Chrome/Edge 105+ and Safari 15.4+)



<form>
<div class="form-item">
<label for="name">Name</label><br>
<input type="text" id="name" required aria-invalid="true">
</div>

<div class="form-item">
<label for="email">E-Mail</label><br>
<input type="text" id="email">
</div>
</form>

.form-item {
--color: #000;

/* The default color is #000 */
color: var(--color);
}

input {
/* The default border-color is #000 */
border: 1px solid var(--color);
}

/* If the .form-item contains an element with [aria-invalid="true"],
the text and border color changes to #F00 */

.form-item:has([aria-invalid="true"]) {
--color: #F00;
}

Print Share Comment Cite Upload Translate
APA
Manuel Matuzović | Sciencx (2024-05-17T17:18:12+00:00) » Day 6: the :has() pseudo-class. Retrieved from https://www.scien.cx/2022/10/03/day-6-the-has-pseudo-class/.
MLA
" » Day 6: the :has() pseudo-class." Manuel Matuzović | Sciencx - Monday October 3, 2022, https://www.scien.cx/2022/10/03/day-6-the-has-pseudo-class/
HARVARD
Manuel Matuzović | Sciencx Monday October 3, 2022 » Day 6: the :has() pseudo-class., viewed 2024-05-17T17:18:12+00:00,<https://www.scien.cx/2022/10/03/day-6-the-has-pseudo-class/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 6: the :has() pseudo-class. [Internet]. [Accessed 2024-05-17T17:18:12+00:00]. Available from: https://www.scien.cx/2022/10/03/day-6-the-has-pseudo-class/
CHICAGO
" » Day 6: the :has() pseudo-class." Manuel Matuzović | Sciencx - Accessed 2024-05-17T17:18:12+00:00. https://www.scien.cx/2022/10/03/day-6-the-has-pseudo-class/
IEEE
" » Day 6: the :has() pseudo-class." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/10/03/day-6-the-has-pseudo-class/. [Accessed: 2024-05-17T17:18:12+00:00]
rf:citation
» Day 6: the :has() pseudo-class | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/10/03/day-6-the-has-pseudo-class/ | 2024-05-17T17:18:12+00:00
https://github.com/addpipe/simple-recorderjs-demo