How to add a button to an image with CSS?

Following is the code to add a button to an image with CSS −


 Live Demo

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
   width: 100%;
div {
   position: relative;
   width: 100%;
   max-width: 400px;
div img {
   width: 100%;
   height: auto;
div button {
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
   background-color: rgb(64, 21, 133);
   color: white;
   font-size: 20px;
   padding: 24px 36px;
   width: 220px;
   border: none;
   cursor: pointer;
   border-radius: 5px;
   text-align: center;
   font-weight: bolder;
   font-family: monospace,sans-serif,serif;
div button:hover {
<h1>Button on Image Example</h1>
<img src="">
<button class="btn">Button</button>


The above code will produce the following output −

On hovering over the button, the button will change color as shown −