如何隐藏输入文件和样式输入="文件"到图标/图像

时间:2017-05-25 06:18:28

标签: javascript html css

我希望隐藏input="file"并使用图标设置input="file"样式并点击图标以选择图片。



.cover_photo {width:100%; height:250px; overflow:hidden; position:relative;}
.cover_photo img {width:100%;}
.upload_btn { position:absolute; top:0; left:0;}
.upload_btn input[type="file"] {display:none;}

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<section>
	<div class="container-fluid">
    	<div class="row">
        	<div class="col-md-12">
            	<div class="cover_photo">
                  <img class="img-responsive" src="http://underafricanskiessafaris.co.za/wp-content/uploads/2015/02/BG-1.jpg" />
                  <div class="upload_btn">
                    <form>
                        <i id="icon_upload" class="fa fa-camera"></i>
                        <input type="file" name="cover-photo" id="icon_upload" />
                      </form>
                  </div>
                </div>
            </div>
        </div>
    </div>
</section>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:4)

带标签

&#13;
&#13;
#fileInput{
  display: none;
}
#icon{
  width: 100px;
  cursor: pointer;
}
&#13;
<label for="fileInput"> 
  <img id="icon" src="https://image.freepik.com/free-icon/upload-arrow_318-26670.jpg">
</label>
<input id="fileInput" type="file">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用标签并将图像放在内部而不是文本中,然后根据需要进行设计。

<div>
    <label for="browse"><img src="img/0.jpg" /></label>
    <input type="file" id="browse" name="browse" style="display: none">
</div>

答案 2 :(得分:0)

我想你想要这样的东西。放置一个图标并点击图标,以编程方式点击输入类型文件。

&#13;
&#13;
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<input type="file" id="file_upload_id" style="display:none">

<label>Upload:</label>&nbsp;&nbsp;<a href="#"><i id="icon_upload" class="fa fa-upload" onclick="_upload()"></i></a>

<script>
function _upload(){
    document.getElementById('file_upload_id').click();
}
</script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

使用opacity: 0隐藏文件输入并将其设为position: absolute

.cover_photo {
  width:100%; 
  height:250px; 
  overflow:hidden; 
  position:relative;
}
.cover_photo img {
  width:100%;
}
.upload_btn { 
  position:absolute; 
  top:0; 
  left:0;
}
.upload_btn input[type="file"] {
  height: 28px;
  left: 0;
  position: absolute;
  top: 0;
  opacity: 0;
  width: 32px;
  z-index: 1;
}
#icon_upload {
  font-size:30px;
  cursor: pointer;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<section>
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-12">
        <div class="cover_photo">
          <img class="img-responsive" src="http://underafricanskiessafaris.co.za/wp-content/uploads/2015/02/BG-1.jpg" />
          <div class="upload_btn">
            <form>
              <i id="icon_upload" class="fa fa-camera"></i>
              <input type="file" name="cover-photo" id="icon_upload" />
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>