Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace input file with my own button in the form

Tags:

html

css

Basicly I want to hide the input file and use a button to choose a file in the form.
If I use <label for="input-file">Label</label> when I click the label i get to choose a file, but I would like to be a button or at least to look like a button.
Here is a code sample in JSFiddle :

input[type=file] { display: none; }

<form>
    <input type="file" id="input-file">
    <label for="input-file">
        <button>Button</button>
    </label>
    <input type="submit">
</form>

If I but a button in the label, it acts as a submit button, if I specify its type it does nothing on click.

Is there any way to have upload a file in the form using my button or a button looking control instead of using the input type="file"?
It must be a HTML-CSS solution, no JS or any framework.

EDIT:

This CSS code appears to make the label look like a standard button :

label {
    appearance: button;
    -moz-appearance: button;
    -webkit-appearance: button;
    text-align: center;
    padding: 2px 6px 3px;
    border: 2px outset buttonface;
    font: 13.3333px Arial;
}
like image 346
Ionut Avatar asked Nov 05 '15 20:11

Ionut


People also ask

How do I change the input value of a file?

The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.

How do I change the default text of an input type file?

call( inputs, function( input ) { var label = input. nextElementSibling, labelVal = label. innerHTML; input. addEventListener( 'change', function( e ) { var fileName = ''; if( this.

Can you put a button in a form HTML?

You can create a submit button by using the <input> element and setting the type attribute to 'submit'.


2 Answers

You'll achieve this with couple of lines of CSS. Fiddle

input[type="file"] {
    display: none;
}
.custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
}
<label for="file-upload" class="custom-file-upload">
    <i class="fa fa-cloud-upload"></i> Custom Upload
</label>
<input id="file-upload" type="file"/>
like image 88
Akshaya Raghuvanshi Avatar answered Oct 13 '22 08:10

Akshaya Raghuvanshi


Your Html with simplte font awesome customization FIddle

.image-upload > input
{
    display: none;
}
.attach-doc
{
    cursor: pointer;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="image-upload">
    <label for="file-input">
        <i class="attach-doc fa fa-paperclip fa-2x" aria-hidden="true"></i>
    </label>

    <input id="file-input" type="file"/>
</div>
like image 28
Sufiyan Ansari Avatar answered Oct 13 '22 09:10

Sufiyan Ansari