使用css自定义文件按钮

时间:2016-02-04 06:36:25

标签: html css

我尝试使用css自定义文件上传按钮。

<label  class="custom-file-input">
   <input type="file" name="file" id="videofile" /> <!--class="dropzone"-->
</label>

jsfiddle中的完整代码。 https://jsfiddle.net/2e7dLvoy/

如何显示正常文件上传按钮所选择的文件名?

enter image description here

2 个答案:

答案 0 :(得分:2)

使用此

HTML:

<div class="form-group upload_btn">

  <div class="fileUpload pull-left">
    <span>Browse</span>
    <input type="file" class="upload " id="uploadBtn">
  </div>
  <input disabled="disabled" class="input pull-left" placeholder="Upload your resume" id="uploadFile">
</div>

的CSS:

.form-group.upload_btn {
  overflow: hidden;
  margin-bottom: 20px;
}

.fileUpload {
  background: #e1e1e1 none repeat scroll 0 0;
  color: #626262;
  display: inline-block;
  font-size: 18px;
  font-weight: 300;
  overflow: hidden;
  padding: 5.5px 10px;
  position: relative;
}

.pull-left {
  float: left;
}

.fileUpload input.upload {
  cursor: pointer;
  font-size: 20px;
  left: 0;
  margin: 0;
  opacity: 0;
  padding: 0;
  position: absolute;
  top: 0;
}

.upload_btn .input {
  width: 79%;
}

.input {
  border: 0px;
  padding: 8px;
  box-shadow: none;
  background-color: #fff;
}

JS:

document.getElementById("uploadBtn").onchange = function () {
    document.getElementById("uploadFile").value = this.value;
};

以下是jsfiddle链接

答案 1 :(得分:1)

试试这个:

摆弄jquery:https://jsfiddle.net/2e7dLvoy/4/

<强> HTML

 <label  class="custom-file-input">
   <input type="file" name="file" id="videofile" /> <!--class="dropzone"-->
   <p id="selectedFile"></p>
 </label>

<强> CSS

.custom-file-input {
    display: inline-block;
    position: relative;
    color: #533e00;
}
.custom-file-input input {
    visibility: hidden;
    width: 100px
}
.custom-file-input:before {
    content: 'Drag & Drop';
    display: block;
    background: -webkit-linear-gradient( -180deg, #ffdc73, #febf01);
    background: -o-linear-gradient( -180deg, #ffdc73, #febf01);
    background: -moz-linear-gradient( -180deg, #ffdc73, #febf01);
    background: linear-gradient( -180deg, #ffdc73, #febf01);
    border: 3px solid #dca602;
    border-radius: 10px;
    padding: 5px 0px;
    outline: none;
    white-space: nowrap;
    cursor: pointer;
    text-shadow: 1px 1px rgba(255,255,255,0.7);
    font-weight: bold;
    text-align: center;
    font-size: 10pt;
    position: absolute;
    left: 0;
    font-size: 12px;
}
.custom-file-input:hover:before {
    border-color: #febf01;
}
.custom-file-input:active:before {
    background: #febf01;
}
p {
    float: left;
    margin: -5% 14% 0% 32%;
}

<强>的jQuery

$('input[type=file]').change(function(e){
            $in=$(this);
            $('#selectedFile').html($in.val());
});