Styling an input type=“file” button

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

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

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

    Don't be fooled by "great" CSS-only solutions that are actually very browser-specific, or that overlay the styled button on top of the real button, or that force you to use a <label> instead of a <button>, or any other such hack. JavaScript IS necessary to get it working for general usage. Please study how gmail and DropZone do it if you don't believe me.

    Just style a normal button however you want, then call a simple JS function to create and link a hidden input element to your styled button.

    <!DOCTYPE html>
    <meta charset="utf-8">
    
    <style>
        button {
            width            : 160px;
            height           : 30px;
            font-size        : 13px;
            border           : none;
            text-align       : center;
            background-color : #444;
            color            : #6f0;
        }
        button:active {
            background-color : #779;
        }
    </style>
    
    <button id="upload">Styled upload button!</button>
    
    <script>
    
    function Upload_On_Click(id, handler) {
        var hidden_input = null;
        document.getElementById(id).onclick = function() {hidden_input.click();}
        function setup_hidden_input() {
            hidden_input && hidden_input.parentNode.removeChild(hidden_input);
            hidden_input = document.createElement("input");
            hidden_input.setAttribute("type", "file");
            hidden_input.style.visibility = "hidden";
            document.querySelector("body").appendChild(hidden_input);
            hidden_input.onchange = function() {
                handler(hidden_input.files[0]);
                setup_hidden_input();
            };
        }
        setup_hidden_input();
    }
    
    Upload_On_Click("upload", function(file) {
        console.log("GOT FILE: " + file.name);
    });
    
    </script>
    

    Notice how the above code re-links it after every time the user chooses a file. This is important because "onchange" is only called if the user changes the filename. But you probably want to get the file every time the user provides it.

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

    css can do a lot here... with a little trickery...

    <div id='wrapper'>
      <input type='file' id='browse'>
    </div>
    
    #wrapper {
         width: 93px; /*play with this value */
         height: 28px; /*play with this value */
         background: url('browseBtn.png') 0 0 no-repeat;
         border:none;
         overflow:hidden;
    }
    
    #browse{
         margin-left:-145px; /*play with this value */
         opacity:0; /* set to .5 or something so you can better position it as an overlay then back to zero again after you're done */
         -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
         filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
    }
    

    ::reference::http://site-o-matic.net/?viewpost=19

    -abbey

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

    Working example here with native Drag and drop support : https://jsfiddle.net/j40xvkb3/

    When styling a file input, you shouldn't break any of native interaction the input provides.

    The display: none approach breaks the native drag and drop support.

    To not break anything, you should use the opacity: 0 approach for the input, and position it using relative / absolute pattern in a wrapper.

    Using this technique, you can easily style a click / drop zone for the user, and add custom class in javascript on dragenter event to update styles and give user a feedback to let him see that he can drop a file.

    HTML :

    <label for="test">
      <div>Click or drop something here</div>
      <input type="file" id="test">
    </label>
    

    CSS :

    input[type="file"] {
      position: absolute;
      left: 0;
      opacity: 0;
      top: 0;
      bottom: 0;
      width: 100%;
    }
    
    div {
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #ccc;
      border: 3px dotted #bebebe;
      border-radius: 10px;
    }
    
    label {
      display: inline-block;
      position: relative;
      height: 100px;
      width: 400px;
    }
    

    Here is a working example (with additional JS to handle dragover event and dropped files).

    https://jsfiddle.net/j40xvkb3/

    Hope this helped !

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

    I am able to do it with pure CSS using below code. I have used bootstrap and font-awesome.

    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
    
    <label class="btn btn-default btn-sm center-block btn-file">
      <i class="fa fa-upload fa-2x" aria-hidden="true"></i>
      <input type="file" style="display: none;">
    </label>

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

    This is a nice way to do it with material / angular file upload. You could do the same with a bootstrap button.

    Note I used <a> instead of <button> this allows the click events to bubble up.

    <label>
        <input type="file" (change)="setFile($event)" style="display:none" />
    
        <a mat-raised-button color="primary">
          <mat-icon>file_upload</mat-icon>
          Upload Document
        </a>
    
      </label>
    
    0 讨论(0)
  • 2020-11-21 11:56

    follow these steps then you can create custom styles for your file upload form:

    1. this is the simple HTML form(please read the HTML comments I have written here below)

      <form action="#type your action here" method="POST" enctype="multipart/form-data">
        <div id="yourBtn" style="height: 50px; width: 100px;border: 1px dashed #BBB; cursor:pointer;" onclick="getFile()">Click to upload!</div>
        <!-- this is your file input tag, so i hide it!-->
        <div style='height: 0px;width:0px; overflow:hidden;'><input id="upfile" type="file" value="upload"/></div>
        <!-- here you can have file submit button or you can write a simple script to upload the file automatically-->
        <input type="submit" value='submit' >
      </form>
      
    2. then use this simple script to pass the click event to file input tag.

      function getFile(){
           document.getElementById("upfile").click();
      }
      

      Now you can use any type of styling without worrying about how to change default styles.

    I know this very well because I have been trying to change the default styles for a month and a half. believe me, it's very hard because different browsers have different upload input tag. So use this one to build your custom file upload forms. Here is the full AUTOMATED UPLOAD code.

    function getFile() {
      document.getElementById("upfile").click();
    }
    
    function sub(obj) {
      var file = obj.value;
      var fileName = file.split("\\");
      document.getElementById("yourBtn").innerHTML = fileName[fileName.length - 1];
      document.myForm.submit();
      event.preventDefault();
    }
    #yourBtn {
      position: relative;
      top: 150px;
      font-family: calibri;
      width: 150px;
      padding: 10px;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      border: 1px dashed #BBB;
      text-align: center;
      background-color: #DDD;
      cursor: pointer;
    }
    <form action="#type your action here" method="POST" enctype="multipart/form-data" name="myForm">
      <div id="yourBtn" onclick="getFile()">click to upload a file</div>
      <!-- this is your file input tag, so i hide it!-->
      <!-- i used the onchange event to fire the form submission-->
      <div style='height: 0px;width: 0px; overflow:hidden;'><input id="upfile" type="file" value="upload" onchange="sub(this)" /></div>
      <!-- here you can have file submit button or you can write a simple script to upload the file automatically-->
      <!-- <input type="submit" value='submit' > -->
    </form>

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