How to add button on image in HTML

HTML is a versatile language that allows us to create interactive and visually appealing web pages. One of the ways to enhance interactivity is by adding buttons on images. By incorporating buttons to images, we can create engaging user experiences and provide additional functionality.

Step 1: Set up the HTML document

To get started, open a plain text editor or an HTML editor of your choice. Create a new file and save it with a .html extension. Start by adding the HTML boilerplate structure:

<!DOCTYPE html>
<html>
<head>
<title>Add Button on Image</title>
</head>
<body>
<!-- Your content goes here -->
</body>
</html>

Step 2: Add an image

We can either use our own image or find a suitable one online. Insert the following code inside the <body> tags:

<img src="Add image link here" alt="nature-image">

Step 3: Create the button

To add a button on an image, you can use the <button> tag. Insert the following code right after the <img> tag:

<button>Button Here!</button>

This creates a basic button with the label "Button Here!".

Step 4: Position the button on the image

To position the button on top of the image, you can use CSS. Add the following CSS code in your CSS file:

.button-overlay {
position: relative;
display: inline-block;
}
.button-overlay button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

The code above positions the button on top of the image using relative and absolute positioning. The container element is set to have a relative position, while the button itself is given an absolute position. By using the transform property with the translate() function, the button is centered both horizontally and vertically within its container. Applying this code will result in a visually appealing overlay effect of the button on the image.

Step 5: Combine the image and button

To overlay the button on the image, we need to wrap both elements within a container as follows:

<div class="button-overlay">
<img src="Add image link here" alt="nature-image">
<button>Button Here!</button>
</div>

In the code above, we wrapped the button and image in a div and applied the button-overlay class to it.

Step 6: Run code

Save your HTML file and open it in a web browser. You should now see the image with the button positioned on top of it. Following is an example:

Add a button on image

We can add multiple buttons on the image. The following code example demonstrates how to achieve this.

Add multiple buttons on image

We configure the div element (contains button-overlay class) with display: flex, turning it into a flex container, and its child elements become flex items. By setting the flex-direction property to column, we arrange the buttons vertically within the div. Additionally, align-items: center; ensures that the buttons are centered along the cross-axis (vertical axis) of the flex container, ensuring vertical center alignment within the div.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved