Styling an input type=“file” button

后端 未结 30 1663
终归单人心
终归单人心 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 element but instead uses a element on top of other elements (which can be styled). The 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;
    }
    
    
    Select file..

    Hope this helps!!!

提交回复
热议问题