Free tool · Developer tools
CSS button generator
The default button is #2563eb with #ffffff text, 12px by 24px padding and a 6px radius. It gives 19 lines of CSS and a text contrast of 5.16:1.
- Free
- No sign-up
- Runs in your browser
- Contrast 5.16:1
- Passes 4.5:1 for normal text
- Hover color #2157cf
.button {
background-color: #2563eb;
color: #ffffff;
font-size: 16px;
font-weight: 600;
padding: 12px 24px;
border: 0;
border-radius: 6px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
cursor: pointer;
transition: background-color 0.15s ease;
}
.button:hover {
background-color: #2157cf;
}
.button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
}How to use the CSS Button Generator
- Pick a preset to start, or leave the default.
- Type the Background color and Text color as HEX codes, then change the padding, radius, border and shadow numbers.
- Set Hover darkening to the percent the button should darken under the pointer.
- Check the contrast chip, compare the Normal and Hover buttons, then press Copy the CSS.
How do you make a CSS button?
A button needs a background color, a text color, padding and a corner radius, plus a hover rule and a visible focus style. The generator writes all of them. The default sets background-color to #2563eb, color to #ffffff, font-size to 16px and padding to 12px 24px. A border width of 0 writes border: 0, and a width above 0 writes the full border rule with its color.
The shadow is written only when it can be seen. An opacity of 0, or an offset and blur both at 0, leaves the box-shadow line out. The shadow in the default is 4px down with 12px of blur at 25% black. A short transition on the background makes the hover change smooth instead of abrupt.
Every number is limited to a sensible range, and a color that is not a HEX code shows a message instead of broken CSS. The five presets below are ready to copy, and each is a starting point you can change field by field.
| Preset | Background | Text | Radius | Contrast |
|---|---|---|---|---|
| Solid with soft shadow | #2563eb | #ffffff | 6px | 5.16:1 |
| Pill | #16a34a | #ffffff | 999px | 3.29:1 |
| Outline | #ffffff | #1d4ed8 | 6px | 6.70:1 |
| Flat dark | #111827 | #ffffff | 2px | 17.73:1 |
| Lifted card | #7c3aed | #ffffff | 10px | 5.69:1 |
How dark should the hover color be?
The hover color is the background with each color channel mixed toward black by the percent you choose. The default uses 12%, which turns #2563eb into #2157cf. Small values such as 5 or 10 feel like a gentle response, and 20 or more reads as a clear press. On a dark button, a large value can make the hover almost black, so the flat dark preset uses 25 on #111827 for a visible step.
Darkening also raises the contrast of white or light text, so with light text the hover state never reads worse than the normal one. With dark text, as in the outline preset, darkening lowers the contrast a little. The last column shows the text contrast at each step.
| Darkening | Hover color | White text contrast |
|---|---|---|
| 0% | #2563eb | 5.16:1 |
| 5% | #235edf | 5.61:1 |
| 10% | #2159d4 | 6.09:1 |
| 12% | #2157cf | 6.30:1 |
| 15% | #1f54c8 | 6.64:1 |
| 20% | #1e4fbc | 7.24:1 |
| 30% | #1a45a5 | 8.61:1 |
Is the text on my button readable?
The chip under the preview shows the contrast ratio between the text and the background, worked with the WCAG 2.2 formula. The guideline sets a floor of 4.5:1 for body size text and 3:1 once text counts as large, meaning 18 point, or 14 point in bold, and above. A 16px button label does not count as large, so aim for 4.5:1. The default scores 5.16:1.
The table gives the ratio for white text on common button colors. Amber and yellow fail with white text and work with dark text instead. Swap the text color to near black and the amber button reaches 8.26:1. The ratio is cut off at two decimals rather than rounded up, so a value just under 4.5 is never shown as passing.
| Color | HEX | Contrast | Result |
|---|---|---|---|
| Blue | #2563eb | 5.16:1 | 4.5:1 normal text |
| Green | #16a34a | 3.29:1 | 3:1 large text only |
| Amber | #f59e0b | 2.14:1 | Below 3:1 |
| Red | #dc2626 | 4.82:1 | 4.5:1 normal text |
| Violet | #7c3aed | 5.69:1 | 4.5:1 normal text |
| Teal | #0d9488 | 3.74:1 | 3:1 large text only |
| Near black | #111827 | 17.73:1 | 4.5:1 normal text |
| Yellow | #fde047 | 1.31:1 | Below 3:1 |
How round and how large should the button be?
Padding sets the size of the click area, and the font size sets the text. A vertical padding of 12px with a 16px font gives a button taller than the text by 24px, and the 24px side padding keeps a short label from looking cramped. On a phone, aim for a button that is easy to hit with a thumb, and raise the padding rather than the font when it feels tight.
Corner radius runs from 0 for square corners to a large number for a pill. A radius bigger than half the button height is scaled down by the browser, so 999 always gives a fully rounded end. The keyboard focus outline is written as a separate rule in the border or background color when that color stands out from a white page by at least 3 to 1, and in near black when it does not, so people who tab through a page can see where they are. On a dark page, change the outline color to a light one.
Use it with
- Box Shadow Generator: visual CSS box-shadow builder.
- Px to Rem Converter: convert px, rem, em.
- Color Palette Generator: harmonious palettes from a seed color.
Questions
How do I make a button with CSS?
Give a class a background color, text color, padding, border radius and cursor, then add a hover rule. The generator writes 19 lines for the default, from .button to its focus rule.
How do I change a button color on hover?
Add a :hover rule with a new background-color. Here the hover color is #2157cf, which is #2563eb darkened by 12%.
How do I make a rounded or pill shaped button?
Raise the border-radius. Use 6 to 12px for rounded corners and 999px for a pill, which the browser caps at half the height.
What contrast does button text need?
WCAG 2.2 sets the minimum at 4.5:1 for regular text and 3:1 for large text. The default button scores 5.16:1, and white text on amber scores 2.14:1.
Can I add a border or an outline style?
Yes. Set the border width above 0 and pick a border color, or use the outline preset, which writes a 2px border in #1d4ed8.
Does the page keep my button settings?
No. The colors and numbers are used for the preview on your screen and go no further.
The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.