7

I want the input="file" to be hidden and style the input="file" with icon and clicking upon the icon to select image.

.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>
Minal Chauhan
  • 6,025
  • 8
  • 21
  • 41
User113
  • 153
  • 1
  • 2
  • 14

5 Answers5

15

With label

#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">
vlk
  • 1,414
  • 1
  • 13
  • 22
  • 1
    `label` solution seams to be the best. I also found it explained here: [Styling an input type](https://stackoverflow.com/questions/572768/styling-an-input-type-file-button). – NightKn8 May 25 '17 at 06:38
  • Thank you so much for this. – ANUBIS Aug 05 '21 at 22:22
  • I was able to use the content property in my label to avoid using an `img` element. – hBrent Jan 07 '22 at 23:00
2

I think you want something like this.Place an icon and clicking on the icon, programmatically click the input type file.

<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>
Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
1

You can use for label and put the image inside instead of text, then design it what ever you want.

<div>
    <label for="browse"><img src="img/0.jpg" /></label>
    <input type="file" id="browse" name="browse" style="display: none">
</div>
KaoriYui
  • 912
  • 1
  • 13
  • 43
0

Used opacity: 0 to hide the file input and make it 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>
Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
0

.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>
electronixG
  • 136
  • 1
  • 1
  • 18