CSS Button
This section, we introduce the use of CSS to create the button.
Basic Button Style
CSS Examples
background-color: # 4CAF50; / * Green * /
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
try it"
Button color
We can use the background-color
property to set the button color:
CSS Examples
.button2 {background-color: # 008CBA;} / * Blue * /
.button3 {background-color: # f44336;} / * Red * /
.button4 {background-color: # e7e7e7; color: black;} / * Gray * /
.button5 {background-color: # 555555;} / * Black * /
try it"
Button Size
We can use the font-size
property to set the size of the button:
CSS Examples
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
try it"
Rounded button
We can use the border-radius
property to set the fillet button:
CSS Examples
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
try it"
Button border color
We can use the border
property of a button border color:
CSS Examples
background-color: white;
color: black;
border: 2px solid # 4CAF50; / * Green * /
}
...
try it"
Hover Button
We can use the :hover
selector to modify the style by hovering over the button.
Tip: We can use the transition-duration
property set to "hover" effect speed:
CSS Examples
-webkit-transition-duration: 0.4s; / * Safari * /
transition-duration: 0.4s;
}
.button: hover {
background-color: # 4CAF50; / * Green * /
color: white;
}
...
try it"
Button shadow
We can use the box-shadow
property to add a shadow to the button:
CSS Examples
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);
}
try it"
Disable button
We can use the opacity
property to add transparency (looks similar to "disabled" attribute effects) for the button.
Tip: What can I add cursor
property and set to "not-allowed" to set up a disabled image:
Button Width
By default, the size of the button text on the button has decided (based on text matching length). We can use the width
property to set the width of the button:
Tip: If you want to set a fixed width can use pixels (px) as a unit, if you want to set responsive buttons can be set as a percentage.
Button Group
Remove from the outside and add float:left
set of buttons:
With border Button Group
We can use the border
property to set the button group with border: