🎁 The Js-Craft Guide to React is now available with a 30% off discount!

CSS styling for the upload buttons of input type file

While working on the example of inverting colors in a picture with Javascript I've realized that adding CSS styling on upload buttons is not that easy.

Until I discovered the ::file-selector-button pseudo-element.

This pseudo element applies to the HTML inputs of the type file:

input[type="file"]::file-selector-button {
   /* styles here */
} 

As a shorthand you actually don’t even need to specify the file input element in front of the pseudo-element:

/* both syntaxes work the same */
input[type="file"]::file-selector-button { } 
::file-selector-button { }

Below is the code for a simple example of styling an input type file upload button with CSS:

::file-selector-button {
  background: orangered;
  color: white;
  border: 1px solid orangered;
  border-radius: 5px;
  padding: 1rem 3rem;
}

CSS styling for the upload buttons of input type file

Stying the "no file chosen" block

One related question is how can we style the whole widget block of the upload button.

This can be done by using the initial selector of selector input[type="file"]. It will target the whole widget block, that is the button and the text.

So, adding the below CSS:

input[type=file] {
  border: 1px solid orangered;
  color: orangered;
  padding: 1rem;
  border-radius: 5px;
  font-family: "Comic Sans MS", "Comic Sans", cursive;
  font-size: 120%;
  text-transform: lowercase;
}

Will produce this output:
CSS styling for the upload buttons of input type file

Extra resources and links

You can check out the codepend for this example.

And if you want if you want to see a more complex demo of what is possible with ::file-selector-button take a look here. Cool stuff πŸ’ͺ !

Also, you can check out the official documentation here. And, the support is great, being now available in all browsers.

Happy styling!

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.


Leave a Reply

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

πŸ“˜ The Guide to React Home Screencasts Best of Newsletter Search X

πŸ“˜ - 10 Javascript AI projects with React and LangChain

Hi friend! Before you go, just wanted to let you know about the 10 Javascript AI projects ideas with React and LangChain FREE ebook.

One of the best ways to learn is by doing the work. Choose from these 10 project ideas and start working on topics such as:

  • How does LangChain work
  • Connecting to OpenAI LLM
  • Create AI Agents
  • Simple and Sequential Chains
  • Adding Memory (Chat Context)
  • Prompt Templates
  • Using OutputParsers and Tools for Agents
  • Work with Documents and more...

Keep building and see the new capabilities LLM models can add to your React app! Get all projects as an ebook right to your inbox!

X