{"id":375761,"date":"2022-12-19T06:58:00","date_gmt":"2022-12-19T14:58:00","guid":{"rendered":"https:\/\/css-tricks.com\/?page_id=375761"},"modified":"2023-01-17T06:03:31","modified_gmt":"2023-01-17T14:03:31","slug":"has","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/h\/has\/","title":{"rendered":":has()"},"content":{"rendered":"\n
The CSS This example selects an element with the This is incredibly useful to write styles for components that may or may not contain certain elements inside, such as a card grid where a card item always has a paragraph, but might not have an image that accompanies it.<\/p>\n\n\n\n:has()<\/code> pseudo-class selects elements that contain other elements that match the selector passed into its arguments. It’s often referred to as “the parent selector” because of its ability to select a parent element based on the child elements it contains and apply styles to the parent.<\/p>\n\n\n\n
\/* Select the .card element when it \n contains a <figure> immediately\n followed by a paragraph. *\/\n.card:has(figure + p) {\n flex-direction: row;\n}<\/code><\/pre>\n\n\n\n
.card<\/code> class when it contains a
<figure><\/code> element that is immediately followed by a
<p><\/code> element:<\/p>\n\n\n\n
<article class=\"card\">\n <figure><\/figure>\n <p><\/p>\n<\/article><\/code><\/pre>\n\n\n\n