Styling an input type=“file” button

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

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

30条回答
  •  离开以前
    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)

      Click to upload!
    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;
    }
    click to upload a file

提交回复
热议问题