
CSS Cursor Property: Values and Examples
19/07/2023
CSS Shadows: text-shadow and box-shadow
20/07/2023
CSS can style interactive buttons to clarify what they do and show when they receive focus or are pressed.
5. CSS buttons
Start with a button element and apply a class to it:
HTML:
<button class="my-button">Click here</button>
CSS:
.my-button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.my-button:hover {
background-color: #45a049;
}
.my-button:active {
background-color: #3e8e41;
}
The HTML uses .my-button. The CSS sets padding, background, text color, border, corner radius, and cursor. :hover applies when a pointer is over the element and :active while it is being activated. Keep the keyboard focus indicator visible.
Adapt the appearance to your site and test that the button stays easy to read and operate in all states.
5.1. Buttons for different actions
These examples use solid fills for styles such as primary, secondary, success, warning, and danger. The approach is inspired by Bootstrap and uses plain CSS.
HTML:
<button class="btn primary">Primary</button> <button class="btn secondary">Secondary</button> <button class="btn success">Success</button> <button class="btn warning">Warning</button> <button class="btn danger">Danger</button>
CSS:
.btn {
border: none;
color: white;
padding: 14px 28px;
cursor: pointer;
border-radius: 5px;
}
.primary {background-color: #007bff;}
.primary:hover {background: #0b7dda;}
.secondary {background-color: #e7e7e7; color: black;}
.secondary:hover {background: #ddd;}
.success {background-color: #04AA6D;}
.success:hover {background-color: #46a049;}
.warning {background-color: #ff9800; color: #222;}
.warning:hover {background: #e68a00;}
.danger {background-color: #f44336;}
.danger:hover {background: #da190b;}
5.2. Outline buttons
Use borders to distinguish the variants and change the fill on hover.
HTML:
<button class="btn primary">Primary</button> <button class="btn secondary">Secondary</button> <button class="btn success">Success</button> <button class="btn warning">Warning</button> <button class="btn danger">Danger</button>
CSS:
.btn {
border: none;
color: black;
padding: 14px 28px;
cursor: pointer;
border-radius: 5px;
}
.primary {background-color: white; border: 2px solid #007bff;}
.primary:hover {background: #0b7dda; color: white;}
.secondary {background-color: white; border: 2px solid #e7e7e7;}
.secondary:hover {background: #ddd; color: black;}
.success {background-color: white; border: 2px solid #04AA6D;}
.success:hover {background-color: #46a049; color: white;}
.warning {background-color: white; border: 2px solid #ff9800;}
.warning:hover {background: #e68a00; color: #222;}
.danger {background-color: white; border: 2px solid #f44336;}
.danger:hover {background: #da190b; color: white;}
5.3. Full-width buttons
Add the following class to let a button fill the width of its container:
CSS:
.btn-block{
width: 100%;
display: block;
margin-bottom: 5px;
}
5.4. Pagination with CSS
Pagination uses links to move between pages. Style the navigation arrows and page numbers, and identify the current page with aria-current="page".
HTML:
<ul class="pagination"> <li><a href="#" aria-label="Previous page">«</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a class="active" aria-current="page" href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#" aria-label="Next page">»</a></li> </ul>
CSS:
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 9px 18px;
text-decoration: none;
}
ul.pagination li a.active {
background-color: #4CAF50;
color: white;
}
ul.pagination li a.active:hover {background-color: #397f3b;}
ul.pagination li a:hover:not(.active) {background-color: #ddd;}
5.5. Breadcrumbs with CSS
A breadcrumb shows the path through a site. Put the list in a labeled navigation region and mark its final item as the current page.
HTML:
<nav aria-label="Breadcrumb"> <ul class="breadcrumb"> <li><a href="#">Home</a></li> <li><a href="#">Contact</a></li> <li><a href="#">Blog</a></li> <li><a class="active" aria-current="page" href="#">Services</a></li> </ul> </nav>
CSS:
ul.breadcrumb {
padding: 8px 16px;
list-style: none;
background-color: #eee;
}
ul.breadcrumb li {display: inline;}
ul.breadcrumb li+li:before {
padding: 8px;
color: black;
content: "/\00a0";
}
ul.breadcrumb a:link, ul.breadcrumb a:visited {
text-decoration:none;
color: black;
}
ul.breadcrumb li a.active {
color: #4CAF50;
font-weight: bold;
}
ul.breadcrumb li a.active:hover {
color: #397f3b;
font-weight: bold;
}
5.6. Buttons with icons
Pair an icon with a text label so the action is easy to recognize. When the icon repeats the label, keep it decorative.
HTML:
<button class="btn primary"><i aria-hidden="true" class="fas fa-exclamation-circle"></i> Primary</button> <button class="btn secondary"><i aria-hidden="true" class="fas fa-exclamation-circle"></i> Secondary</button> <button class="btn success"><i aria-hidden="true" class="fas fa-check-circle"></i> Success</button> <button class="btn warning"><i aria-hidden="true" class="fas fa-exclamation-triangle"></i> Warning</button> <button class="btn danger"><i aria-hidden="true" class="fas fa-times-circle"></i> Danger</button>
CSS:
.btn {
border: none;
color: white;
padding: 14px 28px;
cursor: pointer;
border-radius: 5px;
display: inline-flex;
}
.btn i {
margin-right: 5px;
}
.primary {background-color: #007bff;}
.primary:hover {background: #0b7dda;}
.secondary {background-color: #e7e7e7; color: black;}
.secondary:hover {background: #ddd;}
.success {background-color: #04AA6D;}
.success:hover {background-color: #46a049;}
.warning {background-color: #ff9800; color: #222;}
.warning:hover {background: #e68a00;}
.danger {background-color: #f44336;}
.danger:hover {background: #da190b;}
To run the example with Font Awesome, load the corresponding stylesheet in the HTML <head>:
<head> <!-- Other head elements… --> <!-- Load the Font Awesome stylesheet --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"> </head>
5.7. Gradient buttons
Use background-image with linear-gradient() to blend two colors in a chosen direction. See the CSS gradients lesson. Test text contrast across the entire gradient.
HTML:
<button class="btn primary">Primary</button> <button class="btn secondary">Secondary</button> <button class="btn success">Success</button> <button class="btn warning">Warning</button> <button class="btn danger">Danger</button>
CSS:
.btn {
border: none;
color: white;
padding: 14px 28px;
cursor: pointer;
border-radius: 5px;
display: inline-block;
margin: 5px;
}
.primary {background-image: linear-gradient(to right, #007bff, #4ab2e2);}
.primary:hover {background-image: linear-gradient(to right, #0b7dda, #4ab2e2);}
.secondary {background-image: linear-gradient(to right, #6c757d, #adb5bd);}
.secondary:hover {background-image: linear-gradient(to right, #848b92, #adb5bd);}
.success {background-image: linear-gradient(to right, #28a745, #56c150);}
.success:hover {background-image: linear-gradient(to right, #34b556, #56c150);}
.warning {background-image: linear-gradient(to right, #ffc107, #ffda4e); color: #222;}
.warning:hover {background-image: linear-gradient(to right, #ffb200, #ffda4e);}
.danger {background-image: linear-gradient(to right, #dc3545, #e0666e);}
.danger:hover {background-image: linear-gradient(to right, #d32838, #e0666e);}
If a variant also uses an icon, load the Font Awesome stylesheet shown above.
5.8. Buttons with a press effect
The .pulse-effect button scales down slightly when activated. A short transition makes the change gradual. See transitions and transforms for how the effect works.
HTML:
<button class="btn pulse-effect">Click me</button>
CSS:
.pulse-effect {
border: none;
color: white;
padding: 14px 28px;
cursor: pointer;
border-radius: 5px;
background-color: #007bff; /* Base button color */
transition: transform 0.3s ease-in-out; /* Ease the scale transition */
}
.pulse-effect:hover {
background-color: #0056b3; /* Change the background on hover */
}
.pulse-effect:active {
transform: scale(0.9); /* Make the button slightly smaller while pressed */
}
5.9. Buttons with a shadow on hover
The .shadow-effect button gains a shadow on hover. Adjust the color, spread, and transition for your project. The CSS shadows lesson explains the values.
HTML:
<button class="btn shadow-effect">Hover me</button>
CSS:
.shadow-effect {
border: none;
color: white;
padding: 14px 28px;
cursor: pointer;
border-radius: 5px;
background-color: #007bff; /* Base button color */
transition: box-shadow 0.3s ease-in-out; /* Ease the shadow transition */
}
.shadow-effect:hover {
box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.3); /* Show a more visible shadow */
}