CSS Button Styles
This example demonstrates how to create reusable button styles using HTML and CSS. Explore different button colors, outlines, shapes, sizes, and visual effects that can be customized for your website.
Try It: Move your mouse over the different buttons to see their hover effects. Use the Tab key to test keyboard focus indicators, and select the buttons in the interaction example to display a message.
How CSS Button Styles Work
CSS button styles are created by applying reusable classes to HTML button elements.
The base btn class defines common properties such as padding, borders, typography, and transitions.
Additional classes modify specific properties without repeating the entire button style.
For example, the following CSS creates a basic primary button:
.btn-primary {
background-color: #146c94;
color: #fff;
}
The background-color property controls the button's background, while the color property controls its text color.
The :hover pseudo-class applies styles when the pointer is positioned over a button.
For example, the following CSS changes the primary button's background color:
.btn-primary:hover {
background-color: #0b4564;
}
The transition property creates a smooth visual change between the normal and hover states.
Hover effects provide visual feedback and help visitors recognize interactive elements.
The border-radius property controls the roundness of a button's corners.
A value of 0 creates square corners, while larger values create rounded corners.
The padding property controls the space between the button text and its border.
The font-size property changes the size of the button text.
Combining these properties allows you to create buttons suitable for different interface designs.
Use the HTML button element for actions such as submitting forms, opening menus, or changing content.
Use an HTML a element when the purpose is to navigate to another page or location.
The :focus-visible pseudo-class provides a visible outline for keyboard navigation.
Buttons should have clear labels, sufficient color contrast, and enough padding to make them easy to select.
The disabled attribute should only be used when an action is genuinely unavailable.
The prefers-reduced-motion media query removes transitions and movement effects for visitors who request reduced motion.
You can change the button colors by modifying the background-color and color declarations.
Adjust the padding and font-size properties to create larger or smaller buttons.
Modify the border-radius property to change the button shape.
You can also add borders, shadows, and other effects to match your website's design.
To create another button variation, define a new class and combine it with the existing btn class.
For example, class="btn btn-primary btn-rounded" combines the base button style, primary color, and rounded corners.