Styling an input type=“file” button

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

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

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

    Here is a solution which doesn't really style the <input type="file" /> element but instead uses a <input type="file" /> element on top of other elements (which can be styled). The <input type="file" /> element is not really visible hence, the overall illusion is of a nicely styled file upload control.

    I came across this problem recently and despite the plethora of answers on Stack Overflow, none really seemed to fit the bill. In the end, I ended up customizing this so as to have a simple and an elegant solution.

    I have also tested this on Firefox, IE (11, 10 & 9), Chrome and Opera, iPad and a few android devices.

    Here's the JSFiddle link -> http://jsfiddle.net/umhva747/

    $('input[type=file]').change(function(e) {
        $in = $(this);
        $in.next().html($in.val());
        
    });
    
    $('.uploadButton').click(function() {
        var fileName = $("#fileUpload").val();
        if (fileName) {
            alert(fileName + " can be uploaded.");
        }
        else {
            alert("Please select a file to upload");
        }
    });
    body {
        background-color:Black;
    }
    
    div.upload {
        background-color:#fff;
        border: 1px solid #ffffd;
        border-radius:5px;
        display:inline-block;
        height: 30px;
        padding:3px 40px 3px 3px;
        position:relative;
        width: auto;
    }
    
    div.upload:hover {
        opacity:0.95;
    }
    
    div.upload input[type="file"] {
        display: input-block;
        width: 100%;
        height: 30px;
        opacity: 0;
        cursor:pointer;
        position:absolute;
        left:0;
    }
    .uploadButton {
        background-color: #425F9C;
        border: none;
        border-radius: 3px;
        color: #FFF;
        cursor:pointer;
        display: inline-block;
        height: 30px;
        margin-right:15px;
        width: auto;
        padding:0 20px;
        box-sizing: content-box;
    }
    
    .fileName {
        font-family: Arial;
        font-size:14px;
    }
    
    .upload + .uploadButton {
        height:38px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <form action="" method="post" enctype="multipart/form-data">
        <div class="upload">
            <input type="button" class="uploadButton" value="Browse" />
            <input type="file" name="upload" accept="image/*" id="fileUpload" />
            <span class="fileName">Select file..</span>
        </div>
        <input type="button" class="uploadButton" value="Upload File" />
    </form>

    Hope this helps!!!

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

    The best way I have found is having an input type: file then setting it to display: none. Give it an id. Create a button or any other element you want to open the file input.

    Then add an event listener on it (button) which when clicked simulates a click on the original file input. Like clicking a button named hello but it opens a file window.

    Example code

    //i am using semantic ui
    
    <button class="ui circular icon button purple send-button" id="send-btn">
          <i class="paper plane icon"></i>
        </button>
      <input type="file" id="file" class="input-file" />
    

    javascript

    var attachButton=document.querySelector('.attach-button');
        attachButton.addEventListener('click', e=>{
            $('#file').trigger("click")
        })
    
    0 讨论(0)
  • 2020-11-21 12:12

    ONLY CSS

    Use this very simple and EASY

    .choose::-webkit-file-upload-button {
      color: white;
      display: inline-block;
      background: #1CB6E0;
      border: none;
      padding: 7px 15px;
      font-weight: 700;
      border-radius: 3px;
      white-space: nowrap;
      cursor: pointer;
      font-size: 10pt;
    }
    <label>Attach your screenshort</label>
    <input type="file" multiple class="choose">

    0 讨论(0)
  • 2020-11-21 12:12
    <input type="file" name="media" style="display-none" onchange="document.media.submit()">
    

    I would normally use simple javascript to customize the file input tag.A hidden input field,on click of button,javascript call the hidden field,simple solution with out any css or bunch of jquery.

    <button id="file" onclick="$('#file').click()">Upload File</button>
    
    0 讨论(0)
  • 2020-11-21 12:13

    Here is a solution, that also shows the chosen file name: http://jsfiddle.net/raft9pg0/1/

    HTML:

    <label for="file-upload" class="custom-file-upload">Chose file</label>
    <input id="file-upload" type="file"/>
    File: <span id="file-upload-value">-</span>
    

    JS:

    $(function() {
        $("input:file[id=file-upload]").change(function() {
            $("#file-upload-value").html( $(this).val() );
        });
    });
    

    CSS:

    input[type="file"] {
        display: none;
    }
    
    .custom-file-upload {
          background: #ffffd;
          border: 1px solid #aaa;
          border-top: 1px solid #ccc;
          border-left: 1px solid #ccc;
          -moz-border-radius: 3px;
          -webkit-border-radius: 3px;
          border-radius: 3px;
          color: #444;
          display: inline-block;
          font-size: 11px;
          font-weight: bold;
          text-decoration: none;
          text-shadow: 0 1px rgba(255, 255, 255, .75);
          cursor: pointer;
          margin-bottom: 20px;
          line-height: normal;
          padding: 8px 10px; }
    
    0 讨论(0)
  • 2020-11-21 12:13

    Simply simulate a click on the <input> by using the trigger() function when clicking on a styled <div>. I created my own button out of a <div> and then triggered a click on the input when clicking my <div>. This allows you to create your button however you want because it's a <div> and simulates a click on your file <input>. Then use display: none on your <input>.

    // div styled as my load file button
    <div id="simClick">Load from backup</div>
    
    <input type="file" id="readFile" />
    
    // Click function for input
    $("#readFile").click(function() {
        readFile();
    });
    
    // Simulate click on the input when clicking div
    $("#simClick").click(function() {
        $("#readFile").trigger("click");
    });
    
    0 讨论(0)
提交回复
热议问题