<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: R. Maulana Citra</title>
    <description>The latest articles on Forem by R. Maulana Citra (@rizkimcitra).</description>
    <link>https://forem.com/rizkimcitra</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F589673%2Fb4acb181-6a91-406a-abe0-0b333f1d2d6b.jpg</url>
      <title>Forem: R. Maulana Citra</title>
      <link>https://forem.com/rizkimcitra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rizkimcitra"/>
    <language>en</language>
    <item>
      <title>A Powerful CSS Pseudo Class, Let's Take a Look at CSS :has()</title>
      <dc:creator>R. Maulana Citra</dc:creator>
      <pubDate>Sun, 18 Dec 2022 17:23:29 +0000</pubDate>
      <link>https://forem.com/rizkimcitra/a-powerful-css-pseudo-class-lets-take-a-look-at-css-has-alh</link>
      <guid>https://forem.com/rizkimcitra/a-powerful-css-pseudo-class-lets-take-a-look-at-css-has-alh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Well, since CSS got upgraded to version 3 in 1998, A lot of things happened, and until now, CSS has already got many features that we can use to enhance our website to be more beautiful and interactive.&lt;/p&gt;

&lt;p&gt;Web Developer will never leave their CSS behind because it's one of the cores of the technology behind the web, current CSS version let us use our creativity by crafting modern and accessible web applications.&lt;/p&gt;

&lt;p&gt;But more features mean we need more time to think about how it works &lt;em&gt;(again)&lt;/em&gt;, although most modern web apps use CSS framework for managing their stylesheet, enhancing our fundamentals about the core of the CSS framework itself is necessary.&lt;/p&gt;

&lt;p&gt;CSS has never been easy, not a few people understand how CSS works &lt;em&gt;(including me)&lt;/em&gt;, but there is a person out there ready to help you fall in love with CSS, &lt;a href="https://www.youtube.com/kevinpowell" rel="noopener noreferrer"&gt;check out his YouTube channel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One of the problems is that CSS is not a programming language, so it's very hard to debug when something goes wrong, and you might think &lt;em&gt;" CSS is quite easy, we change something, the UI reflected, what's the hard part?"&lt;/em&gt;. That's what I thought when I was using &lt;a href="https://getboostrap.com" rel="noopener noreferrer"&gt;Bootstrap&lt;/a&gt;, but the hard part is when you do all the stuff and some elements didn't quite fit with your expectation, like setting how to set &lt;em&gt;this style&lt;/em&gt; to a specific element that contains &lt;em&gt;this&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Well if you back to 1998 to do all those stuff, it's going to be challenging, but don't worry we now have the feature to do all those stuff.&lt;/p&gt;

&lt;p&gt;It's a &lt;strong&gt;CSS pseudo-class&lt;/strong&gt; that will handle all of those for you. Just imagine you want to target an element that contains something and also the element shouldn't contain something again, it's like a condition in a programming language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;somethingOk&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;somethingOkToo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// do the stuff&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty cool right? now before we talk about this feature, let's talk a little bit about what &lt;strong&gt;CSS pseudo-class&lt;/strong&gt; is.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is CSS pseudo-class?
&lt;/h3&gt;

&lt;p&gt;In CSS, A pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, the pseudo-class &lt;code&gt;:hover&lt;/code&gt; can be used to select a button when a user's pointer hovers over the button and this selected button can then be styled.&lt;/p&gt;

&lt;p&gt;I'm not going to talk much about what pseudo-class is, but here's a simple explanation of it, we will have a button with a black background color, and the text color will be white, I'm also going to add some padding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-btn" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-btn" alt="result-1" width="313" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a nice button there, now let's add interaction, such as color change when the user hovers over the element with &lt;code&gt;:hover&lt;/code&gt; pseudo-class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.button&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the button would look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-btn-hover" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-btn-hover" alt="button with hover" width="313" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After I added the &lt;code&gt;:hover&lt;/code&gt; pseudo-class, the button will change it's color if I hover over the element, that's my simple explanation about the pseudo-class.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is CSS :has() pseudo-class?
&lt;/h2&gt;

&lt;p&gt;The functional &lt;code&gt;:has()&lt;/code&gt; CSS pseudo-class represents an element if any of the relative selectors that are passed as an argument match at least one element when anchored against this element. This pseudo-class presents a way of selecting a parent element or a previous sibling element concerning a reference element by taking a forgiving relative selector list as an argument.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Q:&lt;/strong&gt; Why it's called a functional?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A:&lt;/strong&gt; Because this type of pseudo-class accepts parameters rather than just calling it like &lt;code&gt;:hover&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CSS &lt;code&gt;:has()&lt;/code&gt; pseudo-class will accept &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Selector_list#forgiving_selector_list" rel="noopener noreferrer"&gt;forgiving relative element&lt;/a&gt; and will apply a style if the parameters are valid as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity" rel="noopener noreferrer"&gt;specificity&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;CSS &lt;code&gt;:has()&lt;/code&gt; pseudo-class often described as &lt;em&gt;"Parent Selector"&lt;/em&gt; because a lot of people use it to target the parent element but then they actually want to select a specific element that matches with the condition.&lt;/p&gt;

&lt;p&gt;As such, you might want to chill and understand how &lt;code&gt;:has()&lt;/code&gt; works, this might break your mental model about the CSS itseld, now to simplify how &lt;code&gt;:has()&lt;/code&gt; works, take a look at this CSS snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* Matches &amp;lt;a&amp;gt; elements that contain an &amp;lt;span&amp;gt; child with a class colored */&lt;/span&gt;
&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="nc"&gt;.colored&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* Matches &amp;lt;a&amp;gt; elements that directly contain an &amp;lt;img&amp;gt; child */&lt;/span&gt;
&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* Matches &amp;lt;section&amp;gt; elements that DON'T contain any heading elements: */&lt;/span&gt;
&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;h4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;h5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;h6&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CSS :has() examples
&lt;/h3&gt;

&lt;p&gt;Let's take a look at these example of how the CSS pseudo-class &lt;code&gt;:has()&lt;/code&gt; works&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;main&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Punctuation&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;A simple punctuation will never arrive at a dragon's house tonight.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"vergil"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Punctuation&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hi Andrew can I borrow your pen?&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Lorem ipsum dolor sit amet constectum adpisicing elit.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="nf"&gt;#vergil&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is going to look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-example" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-example" alt="css has example" width="600" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what is happening? as you can see, I'm targetting &lt;strong&gt;the section element&lt;/strong&gt; that has an &lt;code&gt;h1&lt;/code&gt; element with an &lt;strong&gt;id&lt;/strong&gt; of &lt;strong&gt;vergil&lt;/strong&gt;, and then &lt;strong&gt;the whole section&lt;/strong&gt; will have a text color of blue.&lt;/p&gt;

&lt;p&gt;It's kinda confusing if you read this pseudo-class for the first time, but you should feel comfortable later.&lt;/p&gt;

&lt;p&gt;Another example is when your website has a lot of images and some of the images will have a &lt;code&gt;figcaption&lt;/code&gt; in them. This way you can use a different style to the image that has a &lt;code&gt;figcaption&lt;/code&gt; by using CSS &lt;code&gt;:has()&lt;/code&gt; pseudo-class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/mr-crab-dank.jpg"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"Mr. Crab"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/your-ms-puff.jpg"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"Ms. Puff"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;figcaption&amp;gt;&lt;/span&gt;Ms. Puff waving at you&lt;span class="nt"&gt;&amp;lt;/figcaption&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also with a little bit of &lt;em&gt;base styling&lt;/em&gt; with this CSS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding-top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.875rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;91%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;524px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;gap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.765rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="nt"&gt;figure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;figure&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result will look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Ftr%3Aw-768%2Ch-582%2Fcss-has%2Fmr-crab-ms-puff" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Ftr%3Aw-768%2Ch-582%2Fcss-has%2Fmr-crab-ms-puff" alt="mr crab &amp;amp; ms puff" width="768" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have 2 images that contain Mr. Crab and Ms. Puff, but you as can see between the images, only Ms. Puff who have a&lt;br&gt;
descriptive &lt;code&gt;figcaption&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's style an image that contains &lt;code&gt;figcaption&lt;/code&gt; to be different from the other images. I'm going to style it to have padding and box-shadow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;figure&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;figcaption&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.75rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the result will be like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fmr-crab-ms-puff-edited" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fmr-crab-ms-puff-edited" alt="mr crab ms puff edit" width="666" height="619"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I simply add some padding with a box-shadow to the &lt;code&gt;&amp;lt;figure&amp;gt;&lt;/code&gt; element, if the &lt;code&gt;&amp;lt;figure&amp;gt;&lt;/code&gt; element has a child that is an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element but also has &lt;code&gt;&amp;lt;figcaption&amp;gt;&lt;/code&gt; element that is a sibling of the &lt;code&gt;&amp;lt;img /&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I hope I explained well with that sentence 😅&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But the point is I can style the &lt;code&gt;&amp;lt;figure&amp;gt;&lt;/code&gt; element but with a condition if the element has a certain element or is followed by another selector by using CSS &lt;code&gt;:has()&lt;/code&gt; pseudo-class.&lt;/p&gt;

&lt;p&gt;CSS &lt;code&gt;:has()&lt;/code&gt; can also be used along with another pseudo-class, for example using &lt;code&gt;:nth-child(n)&lt;/code&gt; to manage grid column. This can be useful if you don't want to use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries" rel="noopener noreferrer"&gt;Media Queries&lt;/a&gt; that much.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* This will apply if a container has 4 children or more */&lt;/span&gt;
&lt;span class="nc"&gt;.container&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;...;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* This will apply if a container has 4 children or more */&lt;/span&gt;
&lt;span class="nc"&gt;.container&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;10&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;...;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above snippet can be used to modify our grid column to be 1 when the container has 5 or more items, but when the container has 10 or more items, a different style can be applied, for example, styling a grid column to be 2.&lt;/p&gt;

&lt;h3&gt;
  
  
  Browser Compatibility
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-compatibility" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fik.imagekit.io%2Fmlnzyx%2Fcss-has%2Fcss-has-compatibility" alt="cross browser" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CSS pseudo-class &lt;code&gt;:has()&lt;/code&gt; is now already supported on many browsers, including &lt;strong&gt;Chrome&lt;/strong&gt;, and &lt;strong&gt;Edge&lt;/strong&gt;, but not for Internet Explorer and Firefox.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.wired.com/story/microsoft-internet-explorer-is-finally-really-fully-dead/," rel="noopener noreferrer"&gt;Internet Explorer is already dead&lt;/a&gt; anyway so this is not a problem because so many people already use a modern web browser like Edge or Chrome.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But be careful, CSS &lt;code&gt;:has()&lt;/code&gt; is not supported on Firefox, but it can be enabled, so you need to be aware of this one, as of today, there are a lot of people still using FireFox.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Support Fallback
&lt;/h4&gt;

&lt;p&gt;As on firefox, CSS &lt;code&gt;:has()&lt;/code&gt; pseudo-class is not enabled by default, you might need to think about the fallback, fortunately, we have &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@supports" rel="noopener noreferrer"&gt;CSS at-rule&lt;/a&gt; to handle if the browser support some rules or style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@supports&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* Supported! */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will seek if the browser supports the CSS &lt;code&gt;:has()&lt;/code&gt; pseudo-class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;And that's it, folks, We're reaching the end of this post, thank you so much to read the blog till the last part. I really appreciate and I hope this post is useful for you.&lt;/p&gt;

</description>
      <category>monitoring</category>
      <category>discuss</category>
      <category>productivity</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
