CSS Advanced
Discover how to use CSS to style buttons.
.button {
background-color: #04AA6D; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
To alter a button’s background color, use the background-color property:
.button1 {background-color: #04AA6D;} /* Green */
.button2 {background-color: #008CBA;} /* Blue */
.button3 {background-color: #f44336;} /* Red */
.button4 {background-color: #e7e7e7; color: black;} /* Gray */
.button5 {background-color: #555555;} /* Black */
se the font-size property to change the font size of a button:
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
To alter a button’s padding, use the padding property:
.button1 {padding: 10px 24px;}
.button2 {padding: 12px 28px;}
.button3 {padding: 14px 40px;}
.button4 {padding: 32px 16px;}
.button5 {padding: 16px;}
For buttons, apply the border-radius attribute to add rounded corners:
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
To give a button a colorful border, use the border property:
.button1 {
background-color: white;
color: black;
border: 2px solid #04AA6D; /* Green */
}
...
When hovering the mouse over a button, you can alter its appearance by using the :hover selector.
A helpful hint is to use the transition-duration attribute to calculate the “hover” effect’s speed:
.button {
transition-duration: 0.4s;
}
.button:hover {
background-color: #04AA6D; /* Green */
color: white;
}
...
To give a button shadow, use the box-shadow property:
.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);
}
To add transparency to a button and give it a “disabled” appearance, use the opacity property.
Tips: You can also add the “not-allowed” cursor attribute, which will cause a “no parking sign” to appear while the mouse is over the button:
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
The button’s default size (as wide as its content) is set by the text within it. To alter a button’s width, use the width property:
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
To build a button group, remove the margins from each button and add float:left to it.
.button {
float: left;
}
To make a button group with borders, use the border property:
.button {
float: left;
border: 1px solid green;
}
Change the float:left to display:block to arrange the buttons below one another rather than side by side:
.button {
display: block;
}
Add an arrow on hover:
Add a “pressed” effect on click:
Fade in on hover:
Add a “ripple” effect on click:
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.