Add CSS style to input type=file | Custom file input

featured Image

In this article, we will see how we can create a custom file input by styling the input field of type=’file’ using CSS.

To build a button/field to upload files and images we most commonly use the input field and set the type attribute to file.

<input type="file">

We get an output like this on our webpage.

css to input type=file

Next, we will style the input field using CSS to make it look like a button.

To hide the default “choose file” button, we will use the ::file-selector-button pseudo selector.

input::file-selector-button {
  visibility: hidden;
}

/* Chrome, Edge & Safari */
input::-webkit-file-upload-button {
  visibility: hidden;
}

The ::file-selector-button pseudo selector is used for representing an input field with type=”file”.

Note: If you are using an older version of WebKit browsers like chrome, opera, or safari, you have to use a non-standard pseudo-element ::webkit-file-upload-button.

Now once that is done, we can add a class to the input field and add some CSS styles

input::file-selector-button {
  visibility: hidden;
}

.input-upload {
  color: transparent;
}

.input-upload::before {
  content: "Select some files";
  display: inline-block;
  background: yellowgreen;
  border: 1px solid #999;
  border-radius: 3px;
  padding: 5px 8px;
  cursor: pointer;
  color: black;
}

So the result of the CSS will be like this

custom field input

DEMO CODE:

Edit input-styling

So, this is how you can create a custom file input for your HTML form using CSS styling.

Related Posts

featured Image

Disable scrolling Behavior on a body with element on position fixed in mobile

Here in this article, we will learn on how to disable the scroll behavior of the body whenever we scroll over an element whose position is fixed in our mobile…

Read more
featured Image

How to Remove Underline from Links in Bootstrap.

In this article, we will learn how to remove the underline from anchor tags in Bootstrap. Just like any other HTML element, links can also be styled using CSS. We…

Read more
featured Image

How to disable the resizable property of a textarea

In this article, we will see quick ways to disable the resizable property of a textarea using CSS. In CSS, we can disable it using the resize property. Set resize…

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *