Tips for customizing the default style of input type=“file” in reactjs?

I'm in the process of creating a field for users to select and upload their files.

So far, I've developed a basic code snippet for file selection:

 export const UploadFile = () => {
  return (
      <input type="file" id="fileUpload" onChange={uploadFile} />

};

Before selection: https://i.sstatic.net/eNajs.png

After selection: https://i.sstatic.net/TZxg4.png

However, I want to enhance the appearance of this component by adding some styling. The challenge lies in using an input.

Currently, my design code looks like this:

        <div className="justify-between">
          <span>
            C:\example\Users\user\someVideo\
          </span>
          <button type="button">
            choose file
          </button>
        </div>

https://i.sstatic.net/82CLa.png

My question is how can I merge the initial code snippet with the design code above?

Answer №1

In a recent project, I implemented a unique styling technique by targeting the file-selector-button of an input element to customize the appearance of the button. You can see an example on this codesandbox link where I demonstrate how to style a file input.

Below is the snippet of CSS code that I used:

#attachment::file-selector-button {
  background-color: #48bb78;
  border: none;
  color: #ffffff;
  height: 100%;
  border-radius: 0 0.375rem 0.375rem 0;
  cursor: pointer;
}

The "attachment" ID refers to the file input element being styled.

Keep in mind that these styles are from my specific project and can be modified as needed.

EDIT: To position the button on the right, you can make the following adjustment:

#attachment::file-selector-button {
  float: right; 
  background-color: #48bb78;
  border: none;
  color: #ffffff;
  height: 100%;
  border-radius: 0 0.375rem 0.375rem 0;
  cursor: pointer;
  margin-right: 0;
}

By adding the "float: right" property, the button will shift to the right side. The "margin-right: 0" ensures it aligns with the end. Keep in mind that other styles may need to be adjusted to maintain the desired layout.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The forEach method is supported on FileList in CRA/Next JS applications, however, it does not work on FileList in Next JS applications created using

I've encountered a puzzling issue. While using forEach on a FileList in a website created with CRA and then migrated to Next JS, the code runs smoothly. However, when I recreated the website using create-next-app, forEach doesn't work on the file ...

Having trouble with clearing divs function not working as expected

I'm facing a challenge in properly aligning 3 div elements without resorting to adding padding at the bottom of the wrapping div, which isn't the most practical solution. Check out the issue demo here How would you suggest addressing this parti ...

Learn how to bring in images using props within React and incorporate the 'import' pathway

These are the props I am passing: const people=['Eliana','Stefania','Ahmed'] { people.map(function(name, index){ return <Person item={index} name={name}/>; }) } import Eliana from '../assets/imgs/people ...

CSS background image not displaying in ReactJS with SCSS

Currently, I am working on a React project and I am trying to incorporate a local image from a public folder as the background for a div container with transparent color. However, I am facing an issue where the image is not being displayed. I have success ...

Struggling to implement JQuery code in a Next.js application

I'm having trouble getting my tracking code to work in Next.js. <Script> window.dataLayer = window.dataLayer || []; function gtag(){ dataLayer.push(arguments) } gtag('js', new Date()) ...

`How can I relocate an IFRAME within a DIV?`

Users have the ability to freely edit HTML content, which is fine. However, I need to relocate all IFRAME elements into a DIV. I attempted the following code: $('#rolunk_content iframe').html ('<div>1' + $('#rolunk_content i ...

The text box and buttons are not properly aligned

https://i.stack.imgur.com/lxeKw.png I am puzzled by the misalignment of my buttons and text box. <div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="Pic">Picture/File</label> <div class="col ...

Interactive chart embedded within a container

I have recently designed a card using Bootstrap and I would like to include a table with some data in it. However, I am facing an issue where the table data spills out of the card when I reduce the screen size for responsiveness testing. Could someone pl ...

Looking for a method to transfer response.body information from an express server (server.js) to a React client (client.js)?

I'm currently working on a project that involves setting up a REST endpoint (POST) for the backend to send response data back to my React app. The goal is to construct an HTML page and then send that HTML back to the backend team. However, the challen ...

Adding a border to dynamically generated content while excluding the outer borders using CSS - an easy guide

My current project involves establishing a grid system that dynamically populates content, resulting in an uncertain number of elements being created. At the moment, each element is placed within a flexbox container with 3 elements per row. If there are mo ...

Django: Implementing Subdirectory Structure for Static Files

My static files are organized as follows: /static base.css /core /css style.css /js stuff.js When accessing these files locally using href="/static/core/css/style.css", everything works correctly. However, when deploying live, ...

"I can't figure out why my footer keeps showing up at the top of the page unexpectedly

Currently, my footer is showing up at the top of the page but I really want it to be at the bottom. I'm new to HTML so apologies if this is a simple fix. Here is my file with internal CSS: .middle { position: fixed; top: 50%; left: 50%; tr ...

What is the best way to overwrite a function that has been declared in an NPM

Beginner's inquiry: I am currently working with an npm package and I am attempting to make changes to the original function without much success. The specific function I am attempting to modify is as follows: AnimationItem.prototype.getMarkerData = (m ...

Error: The function "this.state.data.map" is not defined in ReactJS

class Home extends Component { constructor(props) { super(props); this.state = { data: [], isLoaded: false, }; } componentDidMount() { fetch("https://reqres.in/api/users?page=2") .then((res) => res.json ...

Issue with SVG mask not functioning when transform is applied

I am struggling to apply a mask to a transformed SVG element. When I try to do this with a path, the structure is the same. If I apply the mask to an element outside of the transformed group, it works as expected. However, if I try to do the same inside, t ...

The uppermost part is malfunctioning

Below is the snippet of code: $('> li', this).each(function (index) { var top_space = $(this).css('padding-top'); $(this).prepend('<div></div>'); $('> div', this).css({ position ...

Maintain parental visibility with children when navigating to a different page

I am currently working on a vertical accordion menu that opens on hover, stays open, and closes when other items are hovered. The great assistance I received from @JDandChips has been instrumental in getting this feature up and running. Now, my main focus ...

What is the most effective way to adjust an adaptation: adjusting the max-width or the font-size?

Being new to coding, I'm still in the practice stage. However, I've noticed that whenever I shrink the screen size, the text starts misbehaving. How can I address this issue? Here is a specific example of the problem: https://i.sstatic.net/QZlN ...

Is there a way to make sure that the antd datepicker response is sent to the backend only after I click on the submit button?

After receiving a response from the antd datepicker and successfully sending it to the backend, I encountered an issue. The response is automatically sent to the backend as soon as a date is selected. However, I want the request to be sent only after click ...

Custom property fallback values do not function properly with CSS Variables

I defined a custom property in the :root selector as shown below, but for some reason my color is not rendering correctly. Can someone please help me identify what I am doing wrong? :root { --color-primary: var(--color-primary, #ffc107); } body { ...