Skip to content

Instantly share code, notes, and snippets.

@helabenkhalfallah
Created March 24, 2024 12:48
Show Gist options
  • Save helabenkhalfallah/a133b80ce7381be3a5d174516f81ff3b to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/a133b80ce7381be3a5d174516f81ff3b to your computer and use it in GitHub Desktop.
CSS has Selector
// Syntax: <target>:has(<condition>) { <styles> }
/* Select the .card element when it
contains a <figure> immediately
followed by a paragraph. */
.card:has(figure + p) {
flex-direction: row;
}
/* Selects an h1 heading with a
paragraph element that immediately follows
the h1 and applies the style to h1 */
h1:has(+ p) {
margin-bottom: 0;
}
body:has(video, audio) {
/* styles to apply if the content contains audio OR video */
}
body:has(video):has(audio) {
/* styles to apply if the content contains both audio AND video */
}
/* This doesn't do anything because `::-scoobydoo`
is an invalid selector */
a, a::-scoobydoo {
color: green;
}
/* Matches any <section> element that contains
anything that’s not a heading element. */
section:has(:not(h1, h2, h3, h4, h5, h6))
/* When a list item is being hovered,
select list items not hovered, or before/after hover */
ul:has(> :hover) li:not(:hover, :has(+ :hover), li:hover + *) {
/* ...modify scale and opacity */
}
.container:has(a:hover){
border: 2px solid pink;
}
.card:has(.card__banner) {
grid-row: 1;
grid-column: 1 / -1;
max-inline-size: 100%;
grid-template-columns: 1fr 1fr;
border-left-width: var(--size-4);
}
.form-group:has(:invalid) {
--color: var(--invalid);
}
.form-group:has(:focus) {
--color: var(--focus);
}
.form-group:has(:valid) {
--color: var(--valid);
}
.form-group:has(:placeholder-shown) {
--color: var(--blur);
}
.form-group:has(:invalid:not(:focus)) .form-group__error {
display: block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment