37

I was wondering if this is doable? Google comes up with nothing.

Basically i want to my button to simulate when selected, allowing users to upload files. I've read its possible with labels, but i need to do with a button to do UX/UI

<button style="position:fixed;bottom:10px;right:70px" type="button" class="btn btn-default">
    <input type="file" id="files" visbility="hidden" />
    <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
</button>

Currently my button looks like this: enter image description here to this:

enter image description here

cespon
  • 5,630
  • 7
  • 33
  • 47
Apprentice Programmer
  • 1,485
  • 4
  • 26
  • 39
  • 2
    You can use the `hidden` attribute instead `visbility="hidden"` like: `` (https://www.w3schools.com/tags/att_global_hidden.asp) – cespon Feb 13 '19 at 14:24

9 Answers9

77

You can actually do it with label, you just have to hide the input.

<!-- head -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>

<!-- body -->
<label for="upload">
      <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
      <input type="file" id="upload" style="display:none">
</label>
cespon
  • 5,630
  • 7
  • 33
  • 47
qtgye
  • 3,580
  • 2
  • 15
  • 30
17

try an opacity of 0

opacity:0;

https://jsfiddle.net/m4e0y2o3/12/

plus fallbacks if you what to support older browsers

.hiddenFileInput > input{
  height: 100%;
  width: 100;
  opacity: 0;
  cursor: pointer;
}
.hiddenFileInput{
  border: 1px solid #ccc;
  width: 100px;
  height: 100px;
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  
  /*for the background, optional*/
  background: center center no-repeat;
  background-size: 75% 75%;
  background-image:  url(data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBoZWlnaHQ9IjUxMnB4IiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9Ii01MyAxIDUxMSA1MTEuOTk5MDYiIHdpZHRoPSI1MTJweCI+CjxnIGlkPSJzdXJmYWNlMSI+CjxwYXRoIGQ9Ik0gMjc2LjQxMD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/>
</span>
Victor Radu
  • 2,262
  • 1
  • 12
  • 18
  • get a 404 for that fiddle. – chovy Sep 04 '18 at 12:02
  • On hover cursor isn't become as pointer on top ( I guess need to add some offset – Pavlo Oliinyk Jan 20 '19 at 10:39
  • @victor-radu, can you, please, explain the reason for you choosing `opacity: 0` instead of `display: none`. A little bit of reasoning (explaining when `opacity: 0` will be better) will be helpful for future reference. Thank you! – Boris D. Teoharov May 14 '21 at 08:33
  • hello, @BorisD.Teoharov display none does not actually render the element in the dom, making interactions with it hardly possible. Opacity 0 renders the element but makes it completely transparent meaning that it will still catch the click interaction as you are clicking on it even if you do not see it. – Victor Radu May 15 '21 at 10:59
4

If you're using Angular I suppose this pretty solution

  <button class="" (click)="uploader.click()">
     <input hidden type="file" #uploader />
  <button>

you can easly make the analogy with other techno

Smaillns
  • 2,540
  • 1
  • 28
  • 40
  • This is a perfect solution when using Angular. – Rodrigo Silva Feb 20 '22 at 02:15
  • How about the angular case when button is in the button row above the program and the place to display input image are in separated group i.e.:display section is in sketch.component.html / sketch.component.ts / sketch.component.spec.ts / topnav.component.scss while the button component is in topnav.component.html / topnav.component.ts / topnav.component.spec.ts / topnav.component.scss? Could you show me the example how to implement such a thing? – Wisarut Bholsithi Feb 23 '22 at 20:07
3

As all the other answers mention various ways of hiding the <input> element, consider using opacity: 0 as well, as quoted from the MDN docs:

Note: opacity is used to hide the file input instead of visibility: hidden or display: none, because assistive technology interprets the latter two styles to mean the file input isn't interactive.

You can pretty much trigger the file input with any other event (eg: button click, hover, etc.), you can call click() on the input element (getting it with getElementById(), querySelector(), etc.)

const inputElement = document.getElementById('file-input-element')

// on some other event
inputElement.click() // will let user select files
krishnakeshan
  • 1,242
  • 2
  • 14
  • 18
1

Something like this will do:

<button id="fileup" style="position:fixed;bottom:10px;right:70px" type="button" class="btn btn-default" >
<input type="file" id="files" visbility="hidden"/>
<span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
</button>
<script>
$('#fileup').click(function() {
   $('#files').click();
});
</script>
Eduardo Ramos
  • 416
  • 3
  • 8
0

For me this works:

<label htmlFor="inputGroupFile01">
     <span className="fa fa-file-image fa-lg"/>
</label>
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

We can use style="opacity: 0" in the input:

<input type="file" id="files"  style="opacity: 0"/>

Opacity versus Visibility:
opacity is used to hide the file input instead of visibility: hidden or display: none, because assistive technology interprets the latter two styles to mean the file input isn't interactive. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file

Your Button (with only folder icon):

<button style="position:fixed;bottom:10px;right:70px" type="button" class="btn btn-default">
    <input type="file" id="files"  style="opacity: 0"/>
    <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
    button
</button>

Bona Fortuna!

-1

Simple Method with beautiful input file [type="file"].

Output Image URL - https://i.stack.imgur.com/4Jl0H.jpg

input[type="file"] {
 display: inline-block;
 opacity: 0;
 position: absolute;
 margin-left: 20px;
 margin-right: 20px;
 padding-top: 30px;
 padding-bottom: 67px;
 width: 85%;
 z-index: 99;
 margin-top: 10px;
 cursor:pointer;
}
.custom-file-upload {
 position:relative;
    display: inline-block;
    cursor: pointer;
 padding-top:40px;
 padding-bottom:40px;
 background:url(http://www.a-yachtcharter.com/search-fleet/webaccess/assets/img/uploadIcon.png) #fff center center no-repeat;
 width:91%;
 border:1px dashed #ff5b57 !important;
 margin-left:20px;
 margin-right:20px;
 margin-top:10px;
 text-align:center;
}
<input type="file" multiple class="form-control">
<label for="file-upload" class="custom-file-upload"></label>
Prafulla
  • 29
  • 1
  • 6
-1

You can use the following to make an input type "file" hidden with a button:

<div>
     <input type="file" id="files" style="display:none"/>
     <button onclick="click_the_button(files);">Click me</button>
</div>enter code here

<script>
    function click_the_button(btn){
        btn.click();
    }
</script>
cespon
  • 5,630
  • 7
  • 33
  • 47
KNV
  • 15
  • 3