How to create: button on an image

Learn how to add a button to an image using CSS.

Button on an image

Mountains

Try it yourself

How to add a button on an image

First step - Add HTML:

<div class="container">
  <img src="img_snow.jpg" alt="Snow">
  <button class="btn">Button</button>
</div>

Second step - Add CSS:

/* A container is needed to position the button. Adjust the width as needed */
.container {
  position: relative;
  width: 50%;
}
/* Make the image responsive to the layout */
.container img {
  width: 100%;
  height: auto;
}
/* Set styles for the button and position it in the center of the container/image */
.container .btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  background-color: #555;
  color: white;
  font-size: 16px;
  padding: 12px 24px;
  border: none;
  cursor: pointer;
  border-radius: 5px;
}
.container .btn:hover {
  background-color: black;
}

Try it yourself