Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I position an icon over an image?

Tags:

How do I position a FontAwesome download icon over an image? I want it to be on the bottom left corner of the image. When the user clicks on the download icon, a prompt appears asking if they would like to download the image.

Like so:

enter image description here

Relevant code

<!-- font awesome CSS -->  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">    <!-- download icon -->  <a href="https://loremflickr.com/320/240/dog" download="new-filename"><i class="fas fa-download"></i></a>    <!-- image of dog -->  <img src="https://loremflickr.com/320/240/dog" alt="dog">

Does this method also work with bootstrap 3 font-awesome? I might want to add a button like this on the bottom left corner:

enter image description here

Bootstrap awesome font: https://fontawesome.com/v4.7.0/examples/

like image 722
cooldood3490 Avatar asked Feb 03 '19 22:02

cooldood3490


People also ask

How do you put an icon over an image in HTML?

To overlay two elements, one approach is to make a parent position: relative , and then the overlay position: absolute . In this case, this example should work, where the download icon can be any element, such as a bootstrap button.

How do I overlay icons on top of an image in CSS?

Assign the Positions This is the most important step. The first step to enclose the components along with the image as one element and assign the CSS property position: relative . And the icons we need to place on the image is assigned the property position: absolute . In this case, we are adding the class .

What is a overlay icon?

Overlay icons are small icons displayed on elements in Windows Explorer. Their purpose is to give you quick information on the encryption state of files. The appearance of the icons depends on the module you have installed. The Data Exchange overlay icons are only displayed on files and volumes.

How do I overlay an image in CSS?

In short, CSS overlay effects are achieved by using the following: background-image and background CSS properties to add image and linear-gradient overlay effect. position:absolute , top , bottom , right , left CSS properties to control the position of overlay image or text.


2 Answers

You need a relative container around your image and then you set your icon to be position: absolute.

.container { position: relative; }  .container img { display: block; }  .container .fa-download { position: absolute; bottom:0; left:0; }
<link href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet"/>    <div class="container">     <img src="https://placekitten.com/300/300">     <a href="dog.png" download="new-filename"><i class="fas fa-download"></i></a>  </div>

The reason why you need you img to be display: block is because, by default, img's are display: inline and your img will have a space between the bottom of it and the bottom of the container - so your download icon will appear slightly below your img. Setting it to display: block stops this.

like image 69
Adam Avatar answered Nov 02 '22 15:11

Adam


To overlay two elements, one approach is to make a parent position: relative, and then the overlay position: absolute.

In this case, this example should work, where the download icon can be any element, such as a bootstrap button.

.img-download {    position: relative;  }    .img-download > a {    position: absolute;    background: white;    bottom: 0;    left: 0;  }
<!-- font awesome CSS -->  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">    <div class="img-download">    <!-- image of dog -->    <img src="https://loremflickr.com/320/240/dog" width="320" height="240" alt="dog">        <!-- download icon -->    <a href="https://loremflickr.com/320/240/dog" download="dog.jpg"><i class="fas fa-download"></i> Download</a>  </div>
like image 44
Charlie Harding Avatar answered Nov 02 '22 15:11

Charlie Harding