Styling an input type=“file” button

后端 未结 30 1696
终归单人心
终归单人心 2020-11-21 11:50

How do you style an input type=\"file\" button?

相关标签:
30条回答
  • 2020-11-21 11:56

    Put upload file button over your nice button or element and hide it.

    Very simple and will work on any browser

    <div class="upload-wrap">
        <button type="button" class="nice-button">upload_file</button>
        <input type="file" name="file" class="upload-btn">
    </div>
    

    Styles

    .upload-wrap {
        position: relative;
    }
    
    .upload-btn {
        position: absolute;
        left: 0;
        opacity: 0;
    }
    
    0 讨论(0)
  • 2020-11-21 11:57

    This is simple with jquery. To give a code example of Ryan's suggestion with a slight modification.

    Basic html:

    <div id="image_icon"></div>
    <div id="filename"></div>
    <input id="the_real_file_input" name="foobar" type="file">
    

    Be sure to set the styling on the input when you're ready: opacity: 0 You can't set display: none because it needs to be clickable. But you can position it under the "new" button or tuck in under something else with z-index if you prefer.

    Setup some jquery to click the real input when you click the image.

    $('#image_icon').click(function() {
        $('#the_real_file_input').click();
    });
    

    Now your button is working. Just cut and paste the value when changed.

    $('input[type=file]').bind('change', function() {
        var str = "";
        str = $(this).val();
        $("#filename").text(str);
    }).change();
    

    Tah dah! You may need to parse the val() to something more meaningful but you should be all set.

    0 讨论(0)
  • 2020-11-21 11:57

    Multiple file solution with converted filename

    Bootstrap EXAMPLE

    HTML:

    <div>
      <label class="btn btn-primary search-file-btn">
        <input name="file1" type="file" style="display:None;"> <span>Choose file</span>
      </label>
      <span>No file selected</span>
    </div>
    
    <div>
      <label class="btn btn-primary search-file-btn">
        <input name="file2" type="file" style="display:None;"> <span>Choose file</span>
      </label>
      <span>No file selected</span>
    </div>
    

    1. JS with jQuery:

    $().ready(function($){
        $('.search-file-btn').children("input").bind('change', function() {
        var fileName = '';
        fileName = $(this).val().split("\\").slice(-1)[0];
        $(this).parent().next("span").html(fileName);
      })
    });
    

    2. JS without jQuery

    Array.prototype.forEach.call(document.getElementsByTagName('input'), function(item) {
      item.addEventListener("change", function() {
        var fileName = '';
        fileName = this.value.split("\\").slice(-1)[0];
        this.parentNode.nextElementSibling.innerHTML = fileName;
      });
    });
    
    0 讨论(0)
  • 2020-11-21 12:01

    the only way i can think of is to find the button with javascript after it gets rendered and assign a style to it

    you might also look at this writeup

    0 讨论(0)
  • 2020-11-21 12:01

    VISIBILITY:hidden TRICK

    I usually go for the visibility:hidden trick

    this is my styled button

    <div id="uploadbutton" class="btn btn-success btn-block">Upload</div>
    

    this is the input type=file button. Note the visibility:hidden rule

    <input type="file" id="upload" style="visibility:hidden;">
    

    this is the JavaScript bit to glue them together. It works

    <script>
     $('#uploadbutton').click(function(){
        $('input[type=file]').click();
     });
     </script>
    
    0 讨论(0)
  • 2020-11-21 12:02

    All rendering engines automatically generate a button when an <input type="file"> is created. Historically, that button has been completely un-styleable. However, Trident and WebKit have added hooks through pseudo-elements.

    Trident

    As of IE10, the file input button can be styled using the ::-ms-browse pseudo-element. Basically, any CSS rules that you apply to a regular button can be applied to the pseudo-element. For example:

    ::-ms-browse {
      background: black;
      color: red;
      padding: 1em;
    }
    <input type="file">

    This displays as follows in IE10 on Windows 8:

    This displays as follows in IE10 on Windows 8:

    WebKit

    WebKit provides a hook for its file input button with the ::-webkit-file-upload-button pseudo-element. Again, pretty much any CSS rule can be applied, therefore the Trident example will work here as well:

    ::-webkit-file-upload-button {
      background: black;
      color: red;
      padding: 1em;
    }
    <input type="file">

    This displays as follows in Chrome 26 on OS X:

    This displays as follows in Chrome 26 on OS X:

    0 讨论(0)
提交回复
热议问题