Alternate Grid Layouts for a Hero Component

Explore the :has() selector for versatile header styling, adapting efficiently to the presence of an <img> element for both text-only and image scenarios.

Hero is a term borrowed from newsprint to describe the header section that introduces a page template. Using :has(), we can create a flexible component dependent only on the presence or lack of an <img> element.

The two header variations we’ll explore have the following HTML structure. The possibility of an image is anticipated in the HTML comment, where it would be the first child of the <header> element:

Get hands-on with 1200+ tech skills courses.