css 如何隐藏输入文件并将input=“file”样式设置为icon/image

hmae6n7t  于 2023-07-01  发布在  其他
关注(0)|答案(5)|浏览(201)

我想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>
guz6ccqo

guz6ccqo1#

带标签

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

ev7lccsx2#

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

<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>
1rhkuytd

1rhkuytd3#

您可以使用标签,并将图像放在里面,而不是文本,然后设计它任何你想要的。

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

hmtdttj44#

使用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>
py49o6xq

py49o6xq5#

.image{
cursor:pointer;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet"/>

<label class="image" for="image-id"><i class="fa-regular fa-image"></i></label>
<input type="file" class="image" id="image-id" multiple accept="image/*" hidden>

相关问题