Styling an input type=“file” button

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

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

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

    Maybe a lot of awnsers. But I like this in pure CSS with fa-buttons:

    .divs {
        position: relative;
        display: inline-block;
        background-color: #fcc;
    }
    
    .inputs {
        position:absolute;
        left: 0px;
        height: 100%;
        width: 100%;
        opacity: 0;
        background: #00f;
        z-index:999;
    }
    
    .icons {
        position:relative;
    }
    <div class="divs">
    <input type='file' id='image' class="inputs">
    <i class="fa fa-image fa-2x icons"></i>
    </div>
    
    <div class="divs">
    <input type='file' id='book' class="inputs">
    <i class="fa fa-book fa-5x icons"></i>
    </div>
    <br><br><br>
    <div class="divs">
    <input type='file' id='data' class="inputs">
    <i class="fa fa-id-card fa-3x icons"></i>
    </div>
    
    
    
    
    
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

    Fiddle: https://jsfiddle.net/zoutepopcorn/v2zkbpay/1/

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

    If you are using Bootstrap 3, this worked for me:

    See http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/

    .btn-file {
      position: relative;
      overflow: hidden;
    }
    .btn-file input[type=file] {
      position: absolute;
      top: 0;
      right: 0;
      min-width: 100%;
      min-height: 100%;
      font-size: 100px;
      text-align: right;
      filter: alpha(opacity=0);
      opacity: 0;
      outline: none;
      background: white;
      cursor: inherit;
      display: block;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    
    <span class="btn btn-primary btn-file">
        Browse...<input type="file">
    </span>

    Which produces the following file input button:

    Seriously, check out http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/

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

    The best way is using the pseudo element :after or :before as an element overt the de input. Then style that pseudo element as you wish. I recomend you to do as a general style for all input files as follows:

    input {
      height: 0px;
      outline: none;
    }
    
    input[type="file"]:before {
      content: "Browse";
      background: #fff;
      width: 100%;
      height: 35px;
      display: block;
      text-align: left;
      position: relative;
      margin: 0;
      margin: 0 5px;
      left: -6px;
      border: 1px solid #e0e0e0;
      top: -1px;
      line-height: 35px;
      color: #b6b6b6;
      padding-left: 5px;
      display: block;
    }
    
    0 讨论(0)
  • 2020-11-21 12:18

    You don't need JavaScript for this! Here is a cross-browser solution:

    See this example! - It works in Chrome/FF/IE - (IE10/9/8/7)

    The best approach would be to have a custom label element with a for attribute attached to a hidden file input element. (The label's for attribute must match the file element's id in order for this to work).

    <label for="file-upload" class="custom-file-upload">
        Custom Upload
    </label>
    <input id="file-upload" type="file"/>
    

    As an alternative, you could also just wrap the file input element with a label directly: (example)

    <label class="custom-file-upload">
        <input type="file"/>
        Custom Upload
    </label>
    

    In terms of styling, just hide1 the input element using the attribute selector.

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

    Then all you need to do is style the custom label element. (example).

    .custom-file-upload {
        border: 1px solid #ccc;
        display: inline-block;
        padding: 6px 12px;
        cursor: pointer;
    }
    

    1 - It's worth noting that if you hide the element using display: none, it won't work in IE8 and below. Also be aware of the fact that jQuery validate doesn't validate hidden fields by default. If either of those things are an issue for you, here are two different methods to hide the input (1, 2) that work in these circumstances.

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

    HTML

    <div class="new_Btn">SelectPicture</div><br>
    <input id="html_btn" type='file'" /><br>
    

    CSS

    .new_Btn {
    // your css propterties
    }
    
    #html_btn {
     display:none;
    }
    

    jQuery

    $('.new_Btn').bind("click" , function () {
            $('#html_btn').click();
        });
    //edit: 6/20/2014: Be sure to use ".on" not ".bind" for newer versions of jQuery
    

    Fiddle: http://jsfiddle.net/M7BXC/

    You can reach your goals too without jQuery with normal JavaScript.

    Now the newBtn is linkes with the html_btn and you can style your new btn like you want :D

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

    This week I also needed to custom the button and display the selected file name aside it, so after reading some of the answers above (Thanks BTW) I came up with the following implementation:

    HTML:

    <div class="browse">
    <label id="uploadBtn" class="custom-file-upload">Choose file
    <input type="file" name="fileInput" id="fileInput" accept=".yaml" ngf-select ngf-change="onFileSelect($files)" />
    </label>
    <span>{{fileName}}</span>
    </div>
    

    CSS

       input[type='file'] {
        color: #a1bbd5;
        display: none;
    
    }
    
    .custom-file-upload {
        border: 1px solid #a1bbd5;
        display: inline-block;
        padding: 2px 8px;
        cursor: pointer;
    }
    
    label{
        color: #a1bbd5;
        border-radius: 3px;
    }
    

    Javascript (Angular)

    app.controller('MainCtrl', function($scope) {
    
            $scope.fileName = 'No file chosen';
    
              $scope.onFileSelect = function ($files) {
              $scope.selectedFile = $files;
              $scope.fileName = $files[0].name;
        };
    });
    

    Basically I'm working with ng-file-upload lib, Angular-wise I'm binding the filename to my $scope and giving it the initial value of 'No file chosen', I'm also binding the onFileSelect() function to my scope so when a file gets selected I'm getting the filename using ng-upload API and assign it to the $scope.filename.

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