How to Make an Image Button with CSS and HTML
Use a native <button> with a CSS background image and enough left padding to separate the icon from the label. This example treats the magnifying glass as decoration and keeps the word “Search” in the HTML.
Choose what the image and button each do
CSS backgrounds have no alt attribute. Give the button a text label so it has a name even when the icon is missing. type="submit" submits a form. Use type="button" when you want a separate action without submitting the form. HTML Standard · button
The example uses this site’s 32×32 PNG magnifying glass. If you copy the image into your project, point url() to your own file. A relative URL in a separate stylesheet is resolved from the location of that CSS file. CSS Values and Units · Relative URLs
Build a button with text and an icon
Save the code as image-button.html and open it. The remote image needs an internet connection. The background declaration places the 24×24 CSS pixel icon 8px from the left and centers it vertically. The 44px left padding leaves room for the label, and the button is at least 44px tall.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Search button with an image</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
form { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; max-width: 520px; }
label { display: grid; gap: 6px; flex: 1 1 200px; }
input { width: 100%; min-height: 44px; padding: 8px; border: 1px solid #66798c; font: inherit; }
.search-button {
min-height: 44px; padding: 8px 16px 8px 44px;
border: 1px solid #66798c; border-radius: 6px;
background: #eef4fa url("https://www.codingfactory.net/wp-content/uploads/button_search.png") no-repeat 8px center / 24px 24px;
color: #203348; font: inherit; cursor: pointer;
}
.search-button:hover { background-color: #dceafa; }
.search-button:focus-visible { outline: 3px solid #146bcc; outline-offset: 3px; }
.search-button:active { background-color: #c7dcf4; }
</style>
</head>
<body>
<h1>Style a search button</h1>
<form method="get" role="search">
<label for="query">Search query
<input id="query" name="q" type="search" required>
</label>
<button class="search-button" type="submit">Search</button>
</form>
<p>Submitting sends the text as q in the URL. This demo does not look up search results.</p>
</body>
</html>
Check keyboard controls and form submission
Press Tab to focus the button and look for the outline. Press Enter or Space to activate it. :hover and :active change the background color when the pointer is over the button and while it is pressed. Keep the :focus-visible outline so keyboard users can locate it. WCAG · Focus Visible
To check form submission over HTTP, open the folder with Python 3 available and run python -m http.server 8000 --bind 127.0.0.1. Submit css at http://127.0.0.1:8000/image-button.html and the address gains ?q=css. An empty field is blocked by required. The example passes the query in the URL. A search results page requires its own implementation.
Handle missing images and icon-only buttons
If the icon is missing, check the image URL in the Network panel. If it loads, inspect whether another rule overrides background-size or background-position. Temporarily disable the image declaration to see that the button still has text, then restore the rule.
For an icon-only button, provide a name that describes its action, such as aria-label="Search". If a named button contains a decorative <img>, give the image alt="" to avoid repeating the name. A visible label makes the action clear even if the icon fails to load.




