CSS Button
CSS Buttons
Basic Button Styling
Default Button CSS Button
<!DOCTYPE html>
<html>
<head>
<style>
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
</style>
</head>
<body>
<button class="button"> this is a button </button>
</body>
</html>
Button Colors
Green Blue Red Gray Black Use the background-color property to change the background color of a button:
<!DOCTYPE html>
<html>
<head>
<style>
.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 */
</style>
</head>
<body>
<button class="button1"> button </button>
<button class="button2"> button </button>
<button class="button3"> button </button>
<button class="button4"> button </button>
<button class="button5"> button </button>
</body>
</html>
Button Sizes
use 10px 12px 16px 20px 24px; Use the font-size property to change the font size of a button:
<!DOCTYPE html>
<html>
<head>
<style>
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
/*Use the padding property to change the padding of a button:
10px 24px 12px 28px 14px 40px 32px 16px 16px*/
.button11 {padding: 10px 24px;}
.button21 {padding: 12px 28px;}
.button31 {padding: 14px 40px;}
.button41 {padding: 32px 16px;}
.button51 {padding: 16px;}
</style>
</head>
<body>
<button class="button1"> button </button>
<button class="button2"> button </button>
<button class="button3"> button </button>
<button class="button4"> button </button>
<button class="button5"> button </button>
<button class="button11"> button </button>
<button class="button21"> button </button>
<button class="button31"> button </button>
<button class="button41"> button </button>
<button class="button51"> button </button>
</body>
</html>
Rounded Buttons
use 2px 4px 8px 12px 50%; Use the border-radius property to add rounded corners to a button:
<!DOCTYPE html>
<html>
<head>
<style>
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
</style>
</head>
<body>
<button class="button1"> button </button>
<button class="button2"> button </button>
<button class="button3"> button </button>
<button class="button4"> button </button>
<button class="button5"> button </button>
</body>
</html>
Colored Button Borders
Green Blue Red Gray Black Use the border property to add a colored border to a button:
<!DOCTYPE html>
<html>
<head>
<style>
.button1 {
background-color: white;
color: black;
border: 2px solid #4CAF50; /* Green */
</style>
</head>
<body>
<button class="button1"> button </button>
</body>
</html>
Hoverable Buttons
Green Blue Red Grey Black Use the :hover selector to change the style of a button when you move the mouse over it.
Tip: Use the transition-duration property to determine the speed of the "hover" effect:
<!DOCTYPE html>
<html>
<head>
<style>
Example
.button {
transition-duration: 0.4s;
}
.button:hover {
background-color: #4CAF50; /* Green */
color: white;
</style>
</head>
<body>
<button class="button"> button </button>
</body>
</html>
Shadow Buttons
Shadow Button Shadow on hover Use the box-shadow property to add shadows to a button:
<!DOCTYPE html>
<html>
<head>
<style>
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);
</style>
</head>
<body>
<button class="button1"> button </button>
<button class="button2"> button </button>
</body>
</html>
Disabled Buttons
Normal Button Disabled Button Use the opacity property to add transparency to a button (creates a "disabled" look). Tip: You can also add the cursor property with a value of "not-allowed", which will display a "no parking sign" when you mouse over the button:
<!DOCTYPE html>
<html>
<head>
<style>
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
</style>
</head>
<body>
<button class="disabled"> button </button>
</body>
</html>
Button Width
250px 50% 100% By default, the size of the button is determined by its text content (as wide as its content). Use the width property to change the width of a button:
<!DOCTYPE html>
<html>
<head>
<style>
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
</style>
</head>
<body>
<button class="button1"> button </button>
<button class="button2"> button </button>
<button class="button3"> button </button>
</body>
</html>
Button Groups
Remove margins and add float:left to each button to create a button group:
<!DOCTYPE html>
<html>
<head>
<style>
.button {
float: left;
</style>
</head>
<body>
<button class="button"> button </button>
<button class="button"> button </button>
<button class="button"> button </button>
</body>
</html>
Bordered Button Group
Use the border property to create a bordered button group:
<!DOCTYPE html>
<html>
<head>
<style>
.button {
float: left;
border: 1px solid green;
}
</style>
</head>
<body>
<button class="button"> button </button>
<button class="button"> button </button>
<button class="button"> button </button>
</body>
</html>
Vertical Button Group
Use display:block instead of float:left to group the buttons below each other, instead of side by side:
<!DOCTYPE html>
<html>
<head>
<style>
.button {
display: block;
border: 1px solid green;
}
</style>
</head>
<body>
<button class="button"> button </button>
<button class="button"> button </button>
<button class="button"> button </button>
</body>
</html>