CSS3 Buttons
Styling buttons with CSS3 turns the plain grey browser button into something that matches your brand and gives clear feedback when hovered, clicked or disabled. A good button style covers the resting look, interaction states, and sizes, and works the same whether the element is a button or a link styled to look like one.
Resetting and building the base
Browsers add their own borders, backgrounds and fonts to buttons. Start by overriding them, then build up the look with padding, colour and rounded corners. Padding controls the size better than fixed width and height, because the button then grows naturally with longer text.
.btn {
display: inline-block;
padding: 10px 22px;
font: inherit;
font-weight: 600;
color: #fff;
background: #4057d6;
border: 2px solid transparent;
border-radius: 8px;
text-decoration: none;
cursor: pointer;
transition: background-color 0.2s ease, transform 0.1s ease;
}
.btn:hover {
background: #2f43b3;
}
.btn:active {
transform: translateY(1px);
}
.btn:focus-visible {
outline: 3px solid #9aa8ff;
outline-offset: 2px;
}
.btn:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.btn-outline {
color: #4057d6;
background: transparent;
border-color: #4057d6;
}
.btn-outline:hover {
color: #fff;
background: #4057d6;
}
Interaction states in order
A button should respond visibly to every way a user interacts with it:
- Hover: a slightly darker or lighter shade tells mouse users the element is clickable.
- Active: a tiny downward shift or darker shade while pressed makes the click feel physical.
- Focus: a visible ring for keyboard users.
:focus-visibleshows it for keyboard navigation without flashing on every mouse click. - Disabled: lower opacity and a "not-allowed" cursor show the button cannot be used right now.
If you write rules for links styled as buttons, the traditional order is link, visited, hover, active, because later rules win when specificity is equal.
Smooth effects with transitions
The transition line animates changes in background colour and transform instead of snapping instantly. Animate specific properties rather than using transition: all, which can unexpectedly animate things like width or padding and cause sluggish layouts. Transforms and opacity are the cheapest properties for the browser to animate.
Variants without repeating yourself
Keep shared rules in one base class such as .btn, then add small modifier classes for colour or size: .btn-outline, .btn-danger, .btn-small. In HTML you combine them, like class="btn btn-outline". This is the same idea Bootstrap uses, and it keeps your stylesheet short. CSS custom properties make it even tidier: define --btn-bg on the base and override just that variable in each variant.
Common mistakes
Using a div with a click handler instead of a real button is the biggest one. A div cannot be reached with the Tab key and does not respond to Enter or Space, so keyboard and screen reader users cannot use it. Style a real button for actions, and an a element for navigation.
Another frequent problem is poor contrast. Light text on a pale background may look stylish but is hard to read; aim for strong contrast between text and button colour. Also, touch targets should be large enough for a finger, so avoid tiny padding on mobile.
Lastly, if you set border: none on the base and then add a border on the outline variant, the outline button becomes a few pixels larger than the solid one. A transparent border on the base, as in the example, keeps all variants the same size.
Takeaway: build one base button class with padding, colour and transitions, define hover, active, focus and disabled states, and use modifier classes for variants.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.