How to add a button to an image using CSS ?
Last Updated :
28 Apr, 2025
In the article, we will explore how to add a button to an image using CSS. Adding a button to an image is often used to create an overlay effect to a button on an image that provides an interactive and engaging layout on a webpage. We can achieve this effect with the combination of various CSS Properties including position relative and position absolute. Use right, left, top, and bottom to define button position.
Using Properties Position, Bottom, and Right
It utilizes the properties position: absolute
, bottom: 0
, and right: 0
to place a button at the bottom right corner of an image. It offers a clean and integrated visual design, and the values can be adjusted to position the button differently on the image.
Approach
- Make the basic structure of the web page using
<h1>
heading, a paragraph <p>
, and an image <img>
within a container div having class named "imgbox"
. - A button with the class name
mybtn
is added to the image container. Set the universal selector (*
) to include the Google font, box-sizing, and zero margins and padding. - Now make element having a class name
.imgbox
is styled to the position relative to its container and set its width. - Give button the class name "
.mybtn"
and set the position absolute at the bottom-right corner of the image, styled with border radius, font size, and box shadow.
Example: In this example, we will see how to add a button to an image using CSS.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Button to an image</title>
<link rel="stylesheet"
href="index.css">
</head>
<body>
<div class="box1">
<div class="box">
<h1>GeeksforGeeks</h1>
<p>
How to add a button to
an image using CSS.
</p>
<div class="imgbox">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240105113704/ig.webp"
alt="gfg">
<button class="mybtn">
Click Here
</button>
</div>
</div>
</div>
</body>
</html>
CSS
@import url(
'https://fonts.googleapis.com/css2?family=Poppins&display=swap');
* {
font-family: 'Poppins', sans-serif;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.imgbox {
position: relative;
width: 500px;
}
.mybtn {
position: absolute;
bottom: 20px;
right: 20px;
width: 110px;
height: 40px;
border-radius: 20px;
font-size: 15px;
border: none;
box-shadow: rgba(242, 242, 242) 0px 3px 20px;
transition: 0.3s linear;
font-weight: 700;
}
.mybtn:hover {
transform: translateY(5px);
}
img {
width: 500px;
margin: 0;
padding: 0;
border-radius: 30px;
box-shadow: rgb(21, 20, 20) 0px 3px 20px;
}
h1 {
color: rgb(16, 87, 16);
text-align: center;
margin-bottom: 20px;
}
p {
font-size: 20px;
color: palevioletred;
font-weight: 700;
text-align: center;
margin-bottom: 20px;
}
.box1 {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Output:

Using Properties Position, Top, and Left
In this method we will use position property of CSS with top and left property to set the position of button on the image.We will use top:70% and left:40%.
Approach
- Make the basic structure of the web page using
<h1>
heading, a paragraph <p>
, and an image <img>
within a container div having class named "imgbox"
. - A button with the class name
mybtn
is added to the image container. Set the universal selector (*
) to include the Google font, box-sizing, and zero margins and padding. - The element having a class name
.imgbox
is styled to position it relative to its container and set its width. - The button having class name "
.mybtn"
is setting the position absolute with property top 70% and left 40% of the image, styled with border-radius, font size, and box shadow.
Example: In this example we will see how to add a button to an image using CSS.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Button to an image</title>
<link rel="stylesheet"
href="index.css">
</head>
<body>
<div class="box1">
<div class="box">
<h1>GeeksforGeeks</h1>
<p>
How to add a button to
an image using CSS.
</p>
<div class="imgbox">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231017114110/WEB-DESIGN-copy.webp"
alt="gfg">
<button class="mybtn">
Click Here
</button>
</div>
</div>
</div>
</body>
</html>
CSS
@import url(
'https://fonts.googleapis.com/css2?family=Poppins&display=swap');
* {
font-family: 'Poppins', sans-serif;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.imgbox {
position: relative;
width: 500px;
}
.mybtn {
position: absolute;
top: 70%;
left: 40%;
width: 110px;
height: 40px;
border-radius: 10px;
font-size: 17px;
border: none;
box-shadow: rgba(242, 242, 242) 0px 3px 20px;
transition: 0.3s linear;
}
.mybtn:hover {
transform: translateY(-5px);
}
img {
width: 500px;
margin: 0;
padding: 0;
border-radius: 30px;
box-shadow: rgb(21, 20, 20) 0px 3px 20px;
}
h1 {
color: rgb(16, 87, 16);
text-align: center;
margin-bottom: 20px;
}
p {
font-size: 20px;
color: palevioletred;
font-weight: 700;
text-align: center;
margin-bottom: 20px;
}
.box1 {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Output:
Similar Reads
How to Change an Input Button Image using CSS? Changing an input button image using CSS allows developers to replace the default button style with a custom image. This enhances the button's appearance and improves user interaction. CSS properties like background-image and hover effects enable dynamic and visually appealing button designs for web
2 min read
How to Add Border to an Image Using HTML and CSS? Adding a border to an image means putting a line around the image to make it look better and more noticeable. To add a border to an image using HTML and CSS, we can use the <img> tag and apply CSS styles like border, border-width, and border color to customize the border's appearance.Syntax.im
1 min read
How to Add a Login Form to an Image using HTML and CSS? The login form on an Image is used on many websites. Like hotel websites that contain pictures of the hotels or some organizations that organize special events holding that event picture and login form. In that case, you can design a login or registration form on that picture. This design will make
2 min read
How to add image refaction using CSS ? This article will show how to add image reflection using CSS. To achieve this task, you can use the -webkit-box-reflect to add the reflection of any HTML element. The box-reflect property is a CSS property that allows you to create a reflection effect for an element. It is primarily used to add a mi
2 min read
How to Add Image in Text Background using HTML and CSS ? In this article, we will use HTML and CSS to set the image in the text background. To set the image in the text background, some CSS property is used. To add an image in the text background using HTML and CSS, create a container element (e.g., a `<div>`), set its background to the desired imag
2 min read