<?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>DEV Community: Imesh Chandrasiri</title>
    <description>The latest articles on DEV Community by Imesh Chandrasiri (@dimalchandrasiri).</description>
    <link>https://dev.to/dimalchandrasiri</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%2F1143301%2F1ab040ed-a6ea-48bf-bc9b-f2416fad489a.jpg</url>
      <title>DEV Community: Imesh Chandrasiri</title>
      <link>https://dev.to/dimalchandrasiri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dimalchandrasiri"/>
    <language>en</language>
    <item>
      <title>Animated Input : Pure CSS</title>
      <dc:creator>Imesh Chandrasiri</dc:creator>
      <pubDate>Thu, 24 Aug 2023 21:33:26 +0000</pubDate>
      <link>https://dev.to/dimalchandrasiri/animated-input-pure-css-37p5</link>
      <guid>https://dev.to/dimalchandrasiri/animated-input-pure-css-37p5</guid>
      <description>&lt;p&gt;Today I would love to show how we could do an animated &lt;code&gt;input&lt;/code&gt; using just plain CSS and HTML. This would be a rather small tutorial for the new comers. I strongly believe, the first approach to a UI component should plain old &lt;code&gt;html + css&lt;/code&gt;. If what we desire is not achievable with that; only we should look through scripting options. &lt;/p&gt;

&lt;p&gt;Well I've been a front-end developer for quite some time now and the technologies have improved immensely. Alright, we'll straightaway jump to the how to section. &lt;/p&gt;

&lt;h2&gt;
  
  
  Experience
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pIxN5kbX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5f9o6sl5pe61bqenwjl9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pIxN5kbX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5f9o6sl5pe61bqenwjl9.gif" alt="Image description" width="450" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="login-box"&amp;gt;
    &amp;lt;form action="" method="post" class="form form-login"&amp;gt;
        &amp;lt;div class="form-field"&amp;gt;
            &amp;lt;i class="fa-solid fa-user"&amp;gt;&amp;lt;/i&amp;gt;
            &amp;lt;div class="field-element"&amp;gt;
                &amp;lt;input
                    id="login-username"
                    type="text"
                    class="form-input"
                    required
                    placeholder=""
                    /&amp;gt;
                &amp;lt;label class="user" for="login-username"
                    &amp;gt;&amp;lt;span class="hidden"&amp;gt;Username&amp;lt;/span&amp;gt;&amp;lt;/label
                    &amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;input class="submit" type="submit" value="Login" /&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I've used a simple form and an isolated input element to achieve the above experience. &lt;/p&gt;

&lt;h2&gt;
  
  
  Styles
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
   font-family: 'Nunito', sans-serif;
   height: 100vh;
   display: flex;
   justify-content: center;
   align-items: center;
   background-color: #1c1e25;
}

.login-box {
   background-color: #252933;
   border-radius: 2em;
   padding: 1.5em;
   max-width: 350px;
   min-width: 350px;
   display: flex;
   flex-direction: column;
   color: #5c5e65;
   position: relative;
}

.form-field {
   background-color: #1c1e25;
   display: flex;
   flex-direction: row;
   padding: 1em;
   border-radius: 1.5em;
}

.form-field:focus-within {
   border: 1px solid #497be5;
}

.form-field:first-of-type {
   margin-bottom: 1em;
}

.form-field:last-of-type {
   margin-bottom: 2em;
}

.form-field i {
   padding: 1em;
   margin-right: 1em;
   background-color: #282a30;
   border-radius: 2em;
   color: #ccc;
}

.form-field .field-element {
   display: flex;
   flex-direction: column;
   width: 100%;
   position: relative;
   justify-content: center;
   top: 3px;
}

.form-field .field-element input {
   padding: 0.5em;
   appearance: none;
   border: none;
   background: none;
   border-bottom: 1px solid #497ae552;
   outline: none;
   color: #fff;
}

.form-field .field-element label {
   position: absolute;
   left: 0;
   top: 10px;
   color: #999;
   z-index: 10;
   transition: transform 150ms ease-out, font-size 150ms ease-out;
}

input:focus+label {
   transform: translateY(-80%);
   font-size: 0.75em;
}

input:not(:placeholder-shown)+label {
   transform: translateY(-80%);
   font-size: 0.75em;
}

.submit {
   position: absolute;
   bottom: -8%;
   left: 50%;
   transform: translateX(-50%);
   width: 200px;
   height: 45px;
   cursor: pointer;

   background-color: #497be5;
   border: none;
   border-radius: 2em;
   color: #fff;
   font-size: 1em;
}

.submit:hover {
   background-color: #426fce;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well it's a bit of a lengthy css but I would rather focus on the following two rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input:focus+label {
   transform: translateY(-80%);
   font-size: 0.75em;
}

input:not(:placeholder-shown)+label {
   transform: translateY(-80%);
   font-size: 0.75em;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above will make the label move when the input is focussed. Now the second problem I came across was, the label moves back to the original position once the focus is left from the input. This was rectified using the pseudo class &lt;code&gt;:placeholder-shown&lt;/code&gt; which is triggered if the input placeholder is shown. &lt;/p&gt;

&lt;p&gt;That's all that needs to be done to get a beautiful animated input just with HTML and CSS. I would love to hear your thoughts and improvements on any aspect. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>tutorial</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
