The :has() pseudo-class selects an element based on what it contains or what follows it. It finally gives CSS a parent selector. Here are practical examples you can use today.

Style a card only when it has an image

css">.lotgrafix-card:has( img ) {
    display: grid;
    grid-template-columns: 200px 1fr;
}

Highlight a field wrapper when the input is focused

.lotgrafix-field:has( input:focus ) {
    border-color: orangered;
    box-shadow: 0 0 0 3px rgba( 255, 69, 0, 0.2 );
}

Show an error style when the input is invalid

.lotgrafix-field:has( input:user-invalid ) label {
    color: red;
}

Style a label when its checkbox is checked

.lotgrafix-option:has( input:checked ) {
    background: #fff3ee;
    border-color: orangered;
}

Lock page scroll when a modal is open

body:has( dialog[open] ) {
    overflow: hidden;
}

Change layout based on the number of children

.lotgrafix-grid:has( .item:nth-child( 4 ) ) {
    grid-template-columns: repeat( 2, 1fr );
}

Select the previous sibling

Combine :has() with the next sibling combinator to style an element based on the one after it.

.lotgrafix-item:has( + .lotgrafix-item:hover ) {
    opacity: 0.6;
}

Hide an empty section

.lotgrafix-section:not( :has( *:not( h2 ) ) ) {
    display: none;
}
«
»