Tips for modifying the appearance of this input document

Hey there, I have this input element created in React:

<span className="btn-file" type='button'>

                  <div style={{display:'flex',justifyContent:'space-around'}}>
                      <span style={{fontSize:'16px'}}>
                          Upload your image
                      </span>
                      <img
                        src={UploadFile}
                        alt="upload"
                        style={{ height: 22 }}
                      ></img>
                  </div>

                  <input type="file" style={{ display: 'none' }} onChange={onChange} onBlur={onBlur} name='newAvatar' />
                </span>

I attempted to change the style of it but couldn't make it round using the border-radius property. This is the CSS file I tried:

.btn-file {
  z-index: 5;
  background-color: white !important;
  color: #292D5A;
  border: 10px solid #292D5A;
  border-radius: 32px;
  position: relative;
  width: 190px;
  height: 43px;
  left: -96px;
  top: 125px;
}

This is what I ended up with:

https://i.sstatic.net/LAlfO.png

But this is what I actually need:

https://i.sstatic.net/JfCxA.png

Answer №1

Regarding the method of constructing a custom file input.

You have the option to utilize either a span or a div, both of which are neutral tags like many others and can have their display style reset.

  • span is typically used for phrasing content (text, images, inputs, etc.),

  • div can contain any block or phrasing content.


To customize your input, you can associate it with a label. This way, the label will trigger the click event for the input allowing you to select a file for uploading.

The linking of input and label is done using the attributes id and for. For example:

<input id="zeInput"type="file"> <label for="zeInput" > click me </label>

By hiding the input element and styling the label, it will still function as intended.


You can then style either a wrapper containing both input and label, or just the label itself.

Here are two examples incorporating an icon from Font Awesome (replacing the image for demonstration purposes):

@import url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css);
.inline-box {
  display: inline-block;
}

.customIpF {
  color: rgb(112, 114, 141);
  border: solid;
  border-radius: 0.5em;
  padding: 0.25em 1em;
  font-weight: bold;
  font-size: 18px;
  cursor: pointer;
}

.customIpF i,
.customIpF img {
  font-size: 1.5em;
  vertical-align: middle;
}

label {
  cursor: pointer;
}

.customIpF input[type="file"] {
  display: none;
}
<div class="inline-box customIpF">
  <input type="file" name="filex" id="filex" class="inputfilex" />
  <label for="filex"> 
    <span>Upload your image</span>
    <i class="fas fa-file-upload" ></i><!-- or use an img here -->    
  </label>
</div>

Alternatively, you can style just the label itself:

@import url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css);
.inline-box {
  display: inline-block;
}

.customIpF {
  color: rgb(112, 114, 141);
  border: solid;
  border-radius: 0.5em;
  padding: 0.25em 1em;
  font-weight: bold;
  font-size: 18px;
  cursor:pointer;
}

.customIpF i, .customIpF img {
  font-size: 1.5em;
  vertical-align: middle;
}

input[type="file"].inputfilex {
  display: none;
}
<input type="file" name="filex" id="filex" class="inputfilex" />
<label for="filex" class="inline-box customIpF"> 
    <span>Upload your image</span>
    <i class="fas fa-file-upload" ></i><!-- or use an img here -->    
  </label>

View react jsfiddle

Answer №2

make sure the element is set to display:block or display:inline-block

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

Express Form Validation: Ensuring Data Accuracy

I have recently learned that client-side form validations may not be sufficient to prevent malicious actions from users. It is recommended to also validate the form on the server-side. Since I am new to using express, can someone provide guidance on what s ...

What is the best way to ensure the div expands to the width of the screen as it is being

What changes should I make to the CSS in order to adjust the div class="breadcrumbs" when the table width exceeds the screen length and the background does not extend all the way to the end? See the screenshot for reference. I want the dark blue bar to str ...

Creating an Innovative Grid Design with Varying Column Widths for Each Row

Seeking advice on achieving a specific layout using HTML. I have attempted to use HTML tables but struggled with creating rows with varying columns. Does anyone have recommendations for grid tools that may be more helpful? Thank you ...

Switching the color of links when they are focused, without changing it when they are clicked by either a mouse or

Is there a way to change the color of a link only when a user navigates to it using the TAB key and not when they click on it? The simple CSS solution I found was: a:focus { color: red; } However, this also changes the color when the link is activated by ...

Change in navigation bar appearance on scroll down

I am attempting to create a fixed navigation bar by customizing the Bootstrap CSS file. The goal is to change the navbar's color and make it stick to the top when scrolling down. Although I followed the instructions in this article, the JS code I add ...

"Is there a way to separate and iterate through the results of a Group_Concat

Fetch Data from MySQL with Group Concat $result = $con->prepare("SELECT results.StuID, subjects_d.SubjectID, exams.ID, GROUP_CONCAT(results.ExamID,',',results.Exam1,',',results.Exam2,',',results.Exam3 ORDER ...

Navigating through pages using Jquery's show and hide functionality

How come my item is not returning? I used the .show() method on the item. <div id="back">< back</div> <div class="item">item</div> <div class="content">My content</div> $(function(){ $('.item').on(&apo ...

"Hidden panels in Sencha Touch only respond to show() and hide() methods after a resize event

Check out this demonstration of a Sencha Touch app by visiting this link. The button located in the bottom-left corner is supposed to show or hide the menu panel on top of the "Location info goes here" bar, but it seems to be functioning in an unexpected m ...

Tips for converting HTML content into a properly formatted text file

I have a user interface where users can perform various actions using ajax calls. Once the ajax call is completed, the results are displayed in a div with an id = "log". I want to provide users with an option (a button labeled Export) so that when they hav ...

ReactJS testing with Jest for input field and button interaction

I am seeking guidance on using jest as I am a beginner. I attempted to write test cases with jest for an input change and button click in the following component, but it failed. The issue states 'Method “props” is only meant to be run on a single ...

Images within links appear with blue lines underneath them

Currently, I am in the process of adding links to a footer by using <img> tags with SVG images. However, I am encountering an issue where there are small blue lines under the icons. After searching online, it seems that you need to add this CSS code: ...

I keep encountering an Uncaught TypeError when trying to read the property 'options' of null, despite having the element ID properly defined

I am a newcomer to the world of Javascript and HTML. Despite having the element defined in HTML, I am encountering an exception. Could someone please offer assistance? My goal is to create a shape (initially a circle) based on user input such as shape type ...

Is there a way to display the specific information of a flatlist item in a pop-up window?

Welcome to the HomesScreen.js! This section handles rendering the flat list elements. import { StatusBar } from 'expo-status-bar'; import { Button, Image, Modal, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { ...

The radio button in the HTML form is disabled when the user selects

I have two radio buttons labeled Billable "Y" and Billable "N". These radio buttons are linked to a database with corresponding values of "Y" or "N". If the user selects "Y" using the radio button, then the NonBillableReason text box should be disabled. ...

How can one generate an HTML element using a DOM "element"?

When extracting an element from an HTML page, one can utilize a DOM method like .getElementById(). This method provides a JavaScript object containing a comprehensive list of the element's properties. An example of this can be seen on a MDN documentat ...

Hiding content in HTML with the Display:none property

After exploring various posts on this topic, I am still unable to find a solution that fits my specific scenario. Despite the challenges, I thought it would be worth asking for recommendations. Currently, I have a PowerShell script generating a report in ...

Error: accessing 'map' property of undefined during API retrieval

I am currently working on a project for a full stack blog site. I have successfully created an API for the back-end and it is functioning properly as I am able to retrieve posts from it. However, when I attempt to iterate through the posts using map, I enc ...

The issue experienced in next-auth is due to a TypeError where the function res.getHeader is not

Currently, my focus is on the next 13 along with utilizing the next-auth. I've encountered an error stating TypeError: res.getHeader is not a function while validating the API by using getServerSession(req, res, authOptions) to determine if the user i ...

Issue arising from the lack of direct communication between parent and child elements

I am facing an issue with indirect communication in a react native application. The situation involves a parent component, implemented as a class component, and a child component, which is a functional component. Here is the Parent component: constructor ...

How to Properly Align Navbar Elements Using Bootstrap

My goal is to customize the alignment of my Navbar Items. Currently, the Navbar displays as follows: https://i.sstatic.net/Ru7ad.png I am aiming for the following alignment for the Navbar: Logo ---------------------- About Blog Writeups --------------- ...