<?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: Juan Pablo Guisasola</title>
    <description>The latest articles on Forem by Juan Pablo Guisasola (@jpguisa).</description>
    <link>https://forem.com/jpguisa</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%2F219001%2F8f512050-1ad0-41d0-b308-5e6cb82afa2f.jpg</url>
      <title>Forem: Juan Pablo Guisasola</title>
      <link>https://forem.com/jpguisa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jpguisa"/>
    <language>en</language>
    <item>
      <title>Short example of :has selector</title>
      <dc:creator>Juan Pablo Guisasola</dc:creator>
      <pubDate>Mon, 06 Feb 2023 19:41:32 +0000</pubDate>
      <link>https://forem.com/jpguisa/short-example-of-has-47h7</link>
      <guid>https://forem.com/jpguisa/short-example-of-has-47h7</guid>
      <description>&lt;h2&gt;
  
  
  The idea
&lt;/h2&gt;

&lt;p&gt;Make a label transition from above the input to its initial position when you focus the input using :has selector&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxlsn8h8l888f3zv9zzi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxlsn8h8l888f3zv9zzi.gif" alt="Image description" width="200" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Form
&lt;/h2&gt;

&lt;p&gt;Let's put a basic form just as an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action=""&amp;gt;
    &amp;lt;div class="form-field"&amp;gt;
        &amp;lt;label for=""&amp;gt;Name&amp;lt;/label&amp;gt;
        &amp;lt;div class="field"&amp;gt;
            &amp;lt;input type="text"&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Basic Style
&lt;/h2&gt;

&lt;p&gt;I'm going to add some padding to the input, and put the label above the input&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    input{
        padding: 5px 20px;
    }
    label{
        color:#666;
        transform:translate(.8rem, 1.5rem);
        transition:all .3s ease;
        display: block;
        pointer-events: none; 
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are going to add &lt;code&gt;pointer-events: none;&lt;/code&gt; to the label aswell to allow focus on the input since the label messes with the focus interactions&lt;/p&gt;

&lt;p&gt;Now it looks like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxpyy27ew02thnpa9hsm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxpyy27ew02thnpa9hsm.png" alt="Image description" width="245" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;:has&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;First we need to target any &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; sibling of &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we can use the &lt;code&gt;~&lt;/code&gt; sibling combinator which targets any sibling, no matter if it is a previous or next one and along with that we can check in &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; has an &lt;code&gt;input:focus&lt;/code&gt; as child&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    label:has(~ div input:focus){
        transform: translate(0, 0);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Final result&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    input{
        padding: 5px 20px;
    }
    label{
        color:#666;
        transform:translate(.8rem, 1.5rem);
        transition:all .3s ease;
        display: block;
        pointer-events: none;
    }
    label:has(~ div input:focus){
        transform: translate(0, 0);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you know more about this selector and have recommendations for compatibility let me know in the comments&lt;/p&gt;

&lt;p&gt;Some sources:&lt;br&gt;
&lt;a href="https://css-tricks.com/child-and-sibling-selectors/"&gt;https://css-tricks.com/child-and-sibling-selectors/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://ishadeed.com/article/css-has-parent-selector/"&gt;https://ishadeed.com/article/css-has-parent-selector/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
    </item>
  </channel>
</rss>
