<main>
<a href="#">
<figure>
<img src='photo-1659259906921-aa24f5ce7ddf.jpg' alt=''>
<figcaption>This image is wrapped in a figure element.</figcaption>
</figure>
</a>
<a href="#">
<img src='photo-1659259906921-aa24f5ce7ddf.jpg' alt=''>
</a>
</main>
<small>This demo <a href="https://caniuse.com/css-has" target="_blank">requires a browser that supports <code>:has()</code></a>.</small>
a:has(img) {
border: 10px solid darkolivegreen;
background: palegoldenrod;
color: saddlebrown;
}
a:has(> img) {
border: 10px solid chocolate;
background: pink;
}
a {
display: block;
text-decoration: none;
}
img {
max-width: 100%;
display: block;
}
body {
background-color: antiquewhite;
}
p {
margin: 0.5rem 0 0;
}
main {
margin: 1.5rem 1rem;
display: grid;
grid-template-columns: 155px 155px;
gap: 1rem;
font-family: Avenir, Helvetica;
font-size: 0.8rem;
align-items: start;
}
figure {
margin: 0;
}
figcaption {
padding: 0.5rem;
}
/* Warning message
about support for :has() */
@supports selector(:has(figcaption)) {
body small {
display: none;
}
}
small {
background: crimson;
color: white;
padding: 1rem;
display: block;
font-family: Helvetica;
font-weight: 600;
font-size: 1rem;
max-width: max-content;
margin-bottom: 2rem;
position: absolute;
top: 1rem;
left: 1rem;
}
small code {
font-family: courier;
font-size: 1.2rem;
background: rgba(255, 255, 255, 0.33);
}
small a {
color: white;
display: inline;
padding: 0;
background: none;
}
small a:hover {
color: rgba(255, 255, 255, 0.8);
}