CSS Buttons
This section introduces how to create buttons using CSS.
Basic Button Styles
Example
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
Button Colors
We can set the button color using the background-color
property:
Example
.button1 {background-color: #4CAF50;} /* Green */
.button2 {background-color: #008CBA;} /* Blue */
.button3 {background-color: #f44336;} /* Red */
.button4 {background-color: #e7e7e7; color: black;} /* Gray */
.button5 {background-color: #555555;} /* Black */
Button Sizes
We can set the button size using the font-size
property:
Example
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
Rounded Buttons
We can set rounded buttons using the border-radius
property:
Example
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
Button Border Color
We can set the button border color using the border
property:
Example
.button1 {
background-color: white;
color: black;
border: 2px solid #4CAF50; /* Green */
}
...
Hover Buttons
We can modify the style of a button when the mouse hovers over it using the :hover
selector.
Tip: We can use the transition-duration
property to set the speed of the "hover" effect:
Example
.button {
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
}
.button:hover {
background-color: #4CAF50; /* Green */
color: white;
}
...
Button Shadows
We can add shadows to buttons using the box-shadow
property:
Example
.button1 {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}
Disabled Buttons
We can add opacity to a button to make it look disabled using the opacity
property.
Tip: We can add the cursor
property and set it to "not-allowed" to indicate a disabled state:
Example
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
Button Width
By default, the size of a button is determined by the text content within it. We can set the button width using the width
property:
Tip: To set a fixed width, use pixels (px). For responsive buttons, use percentages.
Example
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {
width: 100%;
}
Button Group
Use float: left
to set the button group:
Example
.button {
float: left;
}
Button Group with Border
Use the border
property to set a button group with a border:
Example
.button {
float: left;
border: 1px solid green;
}
Example
.btn-group button {
background-color: #04AA6D; /* Green background */
border: 1px solid green; /* Green border */
color: white; /* White text */
padding: 10px 24px; /* Padding */
cursor: pointer; /* Pointer/hand icon */
float: left; /* Float buttons side by side */
}
Button Animation
Example
Add an arrow marker when hovering over the button:
Hover
Example
Add a "ripple" effect when clicking:
Click
Example
Add a "pressed" effect when clicking:
Click
>
More button styles can be created using a CSS Button Generator: