HTML Button Generator: CSS Button Code With Hover Effects
What Is an HTML Button?
A button is the thing people click to make something happen: send a form, add to cart, open a menu. In HTML it's
one tag, <button>. It works with a mouse, a finger and a keyboard right away, and screen readers
announce it as a button. Out of the box it looks small and gray, so almost every site styles it with CSS.
This generator writes that CSS for you. Pick a preset, change the colors, size, corners, border, shadow and hover
effect, and try the real button in the preview: hover it, click it, and press Tab to see the focus ring. Then copy
the HTML and CSS. It all runs in your browser, and your last button is remembered for next time.
How to Use It, Step by Step
Five quick steps. The preview is the exact code you copy, so what you see is what you get.
Step 1 Pick a Preset
Click a tile under Presets to start from a look you like: Primary, Outline, Pill, Ghost,
Gradient, 3D pressed, Dark, Danger red, Success green or Link style. Presets only change the look, so your text,
icon and link stay as they are.
Step 2 Button or Link, Then the Text
Under Element, choose Button for an action on the page or
Link button when it opens another page (then type the address). Type the
Button text and, if you like, pick an Icon and the side it sits on. Quotes,
< and & in your text are escaped in the code, so they can't break your page.
Step 3 Set the Size, Colors and Shape
- Size: font size and padding, or Full width for a button that fills its container.
- Text style: font weight, uppercase, letter spacing, font and underline.
- Colors: a solid or gradient background (or transparent) and the text color. The contrast line tells you if it's easy to read.
- Border and corners: border width, style and color, and the corner radius. 999 makes a pill.
- Shadow: none, soft, strong, a solid 3D edge, or your own.
Step 4 Choose the Hover, Pressed and Focus Look
Hover colors can darken automatically or use your own colors. Hover movement
lifts the button or makes it grow a little, and When pressed pushes it down or shrinks it. Set
the Animation speed, the Focus ring keyboard users see, and switch on
Disabled to see and copy the disabled look.
Step 5 Try It, Then Copy the Code
Hover and click the button in the preview, and press Tab to see the focus ring. Switch the page behind it to
white, gray or dark to check it fits your site. Then pick Class + <style> (the best
choice for real sites) or Inline styles, and press Copy all,
Copy HTML or Copy CSS.
💡 Inline styles can't do :hover, :active or :focus-visible, so with
inline output the button looks right but doesn't change when you hover or press it. The tool tells you when
that happens.
How to Make a Button in HTML
Write <button>, the text, and </button>. Add type="button": inside a
form, a button without a type is a submit button and sends the form, which is a common surprise.
The code:
<button type="button">Click me</button>
You'll see (the browser's default look):
Style a Button With CSS: Colors, Padding and Corners
Give the button a class and style it. background-color and color set the colors,
padding makes room around the text (top and bottom first, then left and right), and
border-radius rounds the corners. border: none removes the default gray border, and
font-family: inherit makes the button use your page's font, because buttons don't do that by default.
The code:
<style>
.my-button {
padding: 12px 24px;
font-family: inherit;
font-size: 16px;
font-weight: 600;
color: #ffffff;
background-color: #4f46e5;
border: none;
border-radius: 8px;
cursor: pointer;
}
</style>
<button type="button" class="my-button">Get started</button>
Need a color that matches your brand? Find it with the HTML Color Picker, or make a
two-color background with the CSS Gradient Generator.
Button or Link: Which One Should You Use?
They can look the same, but they do different jobs. A link (<a href>) goes somewhere: people can
open it in a new tab, copy its address, and screen readers call it a link. A button does something on the page you're
on. A good rule: if the address bar changes, it's a link.
| When you click it, it… | Use | Code |
| Goes to another page or site | Link | <a href="/pricing"> |
| Downloads a file | Link | <a href="/guide.pdf" download> |
| Sends a form | Button | <button type="submit"> |
| Opens a menu, popup or tab on this page | Button | <button type="button"> |
| Adds to cart, likes, plays a video | Button | <button type="button"> |
A Button That Links to Another Page
Style a normal link so it looks like a button. Links are inline, so give it display: inline-block (or
inline-flex) to let the padding work, and text-decoration: none to drop the underline.
The code:
<style>
.link-button {
display: inline-block;
padding: 12px 24px;
font-weight: 600;
color: #ffffff;
background-color: #15803d;
border-radius: 6px;
text-decoration: none;
}
</style>
<a href="/pricing" class="link-button">See pricing</a>
Two common ways people do it that cause trouble:
<!-- Don't: invalid, a button inside a link -->
<a href="/sign-up"><button>Sign up</button></a>
<!-- Don't: needs JavaScript, can't open in a new tab -->
<button onclick="location.href='/sign-up'">Sign up</button>
<!-- Do: a link that looks like a button -->
<a href="/sign-up" class="link-button">Sign up</a>
A <button> inside an <a> isn't valid HTML, and browsers and screen readers
handle it in different ways. onclick="location.href=…" only works with JavaScript, can't be opened in a
new tab or bookmarked, and search engines don't follow it. Pick Link button in the generator and you
get the right version.
Hover, Pressed and Focus States
A button feels alive when it reacts. CSS has a state for each moment:
:hover is when the mouse is over it. Usually a slightly darker background. :active is while it's being pressed. Moving it down 1px with transform: translateY(1px) feels like a real press. :focus-visible is when someone reaches it with the Tab key. Show a clear ring here. Mouse clicks don't trigger it, so it won't get in the way. transition on the normal rule makes the changes fade in over, say, 0.15 seconds instead of jumping.
The code:
<style>
.my-button {
padding: 12px 24px;
font: inherit;
font-weight: 600;
color: #ffffff;
background-color: #4f46e5;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.15s ease, transform 0.15s ease;
}
.my-button:hover {
background-color: #4338ca;
}
.my-button:active {
transform: translateY(1px);
}
.my-button:focus-visible {
outline: 3px solid #4f46e5;
outline-offset: 2px;
}
</style>
<button type="button" class="my-button">Hover, click or Tab to me</button>
You'll see (hover it, click it, then click beside it and press Tab):
Phones have no hover, so never hide anything important behind it. The generator also adds a
prefers-reduced-motion rule when the button moves: people who turn off animations in their system
settings still see the color change, but the button stays still.
Rounded and Pill Buttons
border-radius sets how round the corners are. Small values (4px to 10px) soften them. A huge value like
999px makes the ends fully round, so you get a pill shape at any width. 50% looks similar on a
square button but turns a wide button into an oval, which is why 999px is the usual choice.
The code:
<style>
.b {
padding: 10px 20px;
font: inherit;
color: #ffffff;
background-color: #0369a1;
border: none;
cursor: pointer;
}
.square { border-radius: 0; }
.soft { border-radius: 8px; }
.pill { border-radius: 999px; }
</style>
<button type="button" class="b square">0</button>
<button type="button" class="b soft">8px</button>
<button type="button" class="b pill">999px</button>
For a softer or deeper shadow under a rounded button, the CSS Box Shadow Generator lets you stack layers.
Center a Button
A button sits inline, like a word in a sentence, so you center it the way you center text: put
text-align: center on the element around it. Flexbox works too: display: flex and
justify-content: center on the parent. The Center Text in HTML guide shows
every way, including centering up and down.
The code:
<div style="text-align: center;">
<button type="button">Centered with text-align</button>
</div>
<div style="display: flex; justify-content: center;">
<button type="button">Centered with flexbox</button>
</div>
Disabled Buttons
Add disabled and the browser blocks clicks and keyboard focus. Style it with :disabled, usually
faded with opacity and a not-allowed cursor. Links can't be disabled: the generator removes the
href and adds aria-disabled="true" instead.
The code:
<style>
.my-button {
padding: 10px 20px;
font: inherit;
color: #ffffff;
background-color: #4f46e5;
border: none;
border-radius: 8px;
cursor: pointer;
}
.my-button:disabled {
opacity: 0.55;
cursor: not-allowed;
}
</style>
<button type="button" class="my-button">Save</button>
<button type="button" class="my-button" disabled>Save</button>
You'll see (the second one can't be clicked):
A grayed-out button with no reason is frustrating. Say what's missing next to it (“Fill in your email to continue”),
or keep the button on and show the problem when it's clicked.
Make Your Button Accessible
- Keep a focus ring. Never use
outline: none without putting something just as clear in its place. The generator always writes a :focus-visible ring. - Check the contrast. Text needs at least 4.5:1 against its background (3:1 for large text: 24px, or about 19px when bold). White on a light green or yellow often fails. The contrast line in the tool checks it for you.
- Use real text. Not a picture of text. If the button only shows an icon, give it
aria-label so screen readers can say what it does. The generator adds one when you leave the text empty. - Make it big enough. Aim for about 44px tall on phones. 24px is the very least.
- Use the right element. A
<div onclick> can't be reached with Tab or pressed with Enter. A <button> can.
An icon-only button with a name screen readers can read:
<button type="button" aria-label="Download">
<svg width="20" height="20" viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 4v11m0 0-4-4m4 4 4-4M5 20h14"/></svg>
</button>
What This Generator Doesn't Do
- It makes one button at a time. For a row of buttons, copy the CSS once and reuse the class on each one.
- It doesn't write JavaScript. What the button does when clicked (opening a menu, sending data) is up to your own script or form.
- Links in the preview open in a new tab. Relative addresses like
/sign-up point at this site there, so they'll only work once the button is on your own site. - Font weights only show if your font has them. Many fonts have 400 and 700 only, so 500 or 800 may look the same as their neighbors.
Building a form? Make the inputs with the HTML Text Box Generator, put it all
together in the HTML Editor, and check it with the HTML Validator.
Frequently Asked Questions
How do I make a button in HTML?
Use the button element: <button type="button">Click me</button>. That one line gives you a real button that works with a mouse, a keyboard (Tab to reach it, Enter or Space to press it) and screen readers. Everything else, like the color, size and rounded corners, comes from CSS. Copy the code from this generator to get both parts at once.
Should I use a button or a link?
Ask what happens when you click it. If it takes you to another page or address, it’s a link: use <a href="/pricing">, even when it looks like a button. If it does something on the current page (sends a form, opens a menu, adds to cart, plays a video), use <button>. Screen readers announce them differently, and links can be opened in a new tab or copied, which buttons can’t.
How do I make a button that links to another page?
Style a link to look like a button: <a href="/sign-up" class="my-button">Sign up</a>, with CSS like display: inline-block, padding, a background color and text-decoration: none. Don’t wrap a <button> in an <a>, and don’t use <button onclick="location.href=...">. The first is invalid HTML, and the second can’t be opened in a new tab, needs JavaScript, and is announced as a button. Pick “Link button” in the generator to get the right code.
Why does my button submit the form?
A <button> inside a <form> is a submit button unless you say otherwise, because type="submit" is its default. So a “Show password” or “Add row” button sends the form by accident. Add type="button" to buttons that shouldn’t send anything, and keep type="submit" for the real send button. The generator always writes the type for you.
How do I change the button color on hover?
Add a :hover rule with the new colors: .my-button:hover { background-color: #4338ca; }. Add transition: background-color 0.15s ease to the normal rule so the change fades in instead of jumping. Keep the text readable on the hover color too. The generator’s Darken option picks a slightly darker shade for you, or choose Custom to set your own hover colors.
How do I make a rounded or pill-shaped button?
Use border-radius. A small value like 6px or 8px softens the corners, and a very big value like 999px turns the ends into half circles, which gives the pill shape no matter how wide the button is. Using 50% instead makes an oval on wide buttons, so 999px is the usual trick.
How do I disable a button?
Add the disabled attribute: <button type="button" disabled>Save</button>. The browser stops clicks and keyboard focus, and you can style it with .my-button:disabled { opacity: 0.55; cursor: not-allowed; }. Links have no disabled attribute, so for a link you remove the href and add aria-disabled="true". Tell people why it’s disabled too, for example with a short line of text next to it.
Why shouldn’t I remove the focus outline from buttons?
People who use a keyboard press Tab to move between buttons and links, and the outline is how they see where they are. If you remove it with outline: none and add nothing back, they’re lost. Use :focus-visible to show a clear ring only for keyboard focus: .my-button:focus-visible { outline: 3px solid #4f46e5; outline-offset: 2px; }. Mouse clicks won’t show it, so it doesn’t get in the way of your design.
How do I center a button on the page?
A button is inline by default, so center it like text: put text-align: center on the element around it. Or make the parent a flex container with display: flex and justify-content: center. For a full-width button, set width: 100% instead. The Center Text in HTML guide shows each way with live examples.