<?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: Alexander Rengkat</title>
    <description>The latest articles on Forem by Alexander Rengkat (@rengkatalex).</description>
    <link>https://forem.com/rengkatalex</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%2F1008377%2F9ed09623-2a86-49a8-b4ec-78f936a507fb.jpg</url>
      <title>Forem: Alexander Rengkat</title>
      <link>https://forem.com/rengkatalex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rengkatalex"/>
    <language>en</language>
    <item>
      <title>How to Customize the File Input in CSS</title>
      <dc:creator>Alexander Rengkat</dc:creator>
      <pubDate>Mon, 27 Mar 2023 10:53:49 +0000</pubDate>
      <link>https://forem.com/rengkatalex/how-to-customize-the-file-input-in-css-49f7</link>
      <guid>https://forem.com/rengkatalex/how-to-customize-the-file-input-in-css-49f7</guid>
      <description>&lt;p&gt;HTML elements typically come with default behaviors that can sometimes be frustrating. However, these behaviors are inherent to their design and may include padding, margins, icons, borders, outlines, and more. Fortunately, CSS allows for the customization, manipulation, and removal of many default behaviors.&lt;/p&gt;

&lt;p&gt;This article will specifically address the file input, which is an input element with a "file" type. Here is an example of what it looks like:&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;input type="file" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file input always display two different default features.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z74okovt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5vmckuik40saysdyv9j.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z74okovt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5vmckuik40saysdyv9j.PNG" alt="Image description" width="604" height="272"&gt;&lt;/a&gt;&lt;br&gt;
The first default feature is the "chose file" while the second features is the "No file chosen" text. These features can be manipulated and customized with text or icons like the file input in Instagram or LinkedIn:&lt;/p&gt;

&lt;p&gt;Instagram file input:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o7d-QeKI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybiqgiojt4cn2t6yxnz6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o7d-QeKI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybiqgiojt4cn2t6yxnz6.PNG" alt="Image description" width="596" height="274"&gt;&lt;/a&gt;&lt;br&gt;
LinkedIn file input&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--43J8XIs9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mirwlgg4zdn0r3reztq.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--43J8XIs9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mirwlgg4zdn0r3reztq.PNG" alt="Image description" width="694" height="57"&gt;&lt;/a&gt;&lt;br&gt;
To customize the file input above, there are tow different ways to go about it.&lt;/p&gt;
&lt;h2&gt;
  
  
  First Method
&lt;/h2&gt;

&lt;p&gt;In my opinion, this method is inefficient as it does not allow for complete customization. While it is possible to remove the "choose file" feature, it is not possible to remove the "no file chosen" feature. This method utilizes the pseudo element &lt;code&gt;::after&lt;/code&gt;, &lt;code&gt;position:absolute&lt;/code&gt;, and &lt;code&gt;visibility:hidden&lt;/code&gt; to hide the "choose file" feature. Below is the code:&lt;/p&gt;

&lt;p&gt;HTML:&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;input class="custom-file-input" type="file" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.custom-file-input {
  border: 1px solid red;
}
.custom-file-input::-webkit-file-upload-button {
  visibility: hidden;
}
.custom-file-input::before {
  content: "Select some files";
  display: inline-block;
  border: 1px solid #999;
  border-radius: 3px;
  padding: 5px 8px;
  outline: none;
  white-space: nowrap;
  -webkit-user-select: none;
  cursor: pointer;
  text-shadow: 1px 1px #fff;
  font-weight: 700;
  font-size: 10pt;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output pf the code above will give this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9mKIMsoc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4gg6kkgt9c28qwnfgan.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9mKIMsoc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4gg6kkgt9c28qwnfgan.PNG" alt="Image description" width="391" height="98"&gt;&lt;/a&gt;&lt;br&gt;
From the output, the "no file chosen" is still visible.&lt;/p&gt;
&lt;h2&gt;
  
  
  Second Method
&lt;/h2&gt;

&lt;p&gt;In my opinion, this method is superior because it allows for complete customization. To further enhance this method, it would be beneficial to place the input within a container alongside the accompanying text or icon to be displayed.&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;div class="container"&amp;gt;
      &amp;lt;input type="file" /&amp;gt;
      &amp;lt;p&amp;gt;Select a file or use icon&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is to style the container and position it relative while all the contents i.e. the input and &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag absolute. The code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container {
  border: 1px solid red;
  width: 20rem;
  height: 5vh;
  position: relative;
  display: flex;
  align-items: center;
}
input {
  position: absolute;
  inset: 0;
  z-index: 2;
  opacity: 0;
}
p {
  position: absolute;
  text-align: center;
  top: -10;
  left: 40%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this method, the input element is assigned a higher &lt;code&gt;z-index&lt;/code&gt;value than the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag to ensure that it always appears on top of it. Then, its opacity is set to 0, allowing it to remain present but invisible. This allows the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag to remain visible underneath the input element. Finally, the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag can be centered according to your desired style. See the output below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FpbzktsO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j89fva4r1ufkzbb7yw6z.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FpbzktsO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j89fva4r1ufkzbb7yw6z.PNG" alt="Image description" width="525" height="71"&gt;&lt;/a&gt;&lt;br&gt;
Ultimately, when it comes to CSS, there is no one definitive "best" way to implement things. It's important to experiment and find the approach that works best for your specific needs and preferences.&lt;/p&gt;

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