How to Create a Shadow Effect in Bootstrap 5 with Examples
Description:
Bootstrap is one of the most popular front-end frameworks for building responsive websites. With the release of Bootstrap 5, developers have access to a wide range of features that make creating modern and attractive web pages easier than ever before. One such feature is the ability to create shadow effects using built-in CSS classes. In this article, we’ll walk you through the steps to create a shadow effect in Bootstrap 5.
Creating Shadow Effect in Bootstrap 5:
Step 1: Add the required CSS
To create a shadow effect in Bootstrap 5, we need to add the necessary CSS classes to the element we want to apply the shadow effect to. The CSS classes for the shadow effect are defined in the bootstrap.css or bootstrap.min.css file.
Step 2: Add the shadow classes
Bootstrap 5 provides four shadow classes for creating shadow effects on elements. These classes are:
- shadow-sm
- shadow
- shadow-lg
- shadow-none
The shadow-sm class creates a small shadow effect, while the shadow-lg class creates a larger shadow effect. The shadow class creates a medium-sized shadow effect, which is the default shadow effect in Bootstrap 5. The shadow-none class removes the shadow effect from an element.
To add the shadow classes to an element, simply add the class name to the element’s class attribute. For example, to add a small shadow effect to a button, you can add the shadow-sm class to the button like this:
1 |
<button class="btn shadow-sm">Click me</button> |
Similarly, to add a large shadow effect to a div element, you can add the shadow-lg class to the div like this:
1 2 3 |
<div class="shadow-lg"> <!-- Content goes here --> </div> |
How to Make a Shadow Effect in Bootstrap 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap Shadow Effect Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/style.css"> <style> div > div { height: 50px; width: 200px; margin : 1em; float: left; line-height: 45px; text-align: center; } </style> </head> <body> <div class="container"> <div class="border border-secondary shadow-none">.shadow-none</div> <div class="border border-secondary shadow-sm">.shadow-sm</div> <div class="border border-secondary shadow">.shadow</div> <div class="border border-secondary shadow-lg">.shadow-lg</div> </div> <script src="js/bootstrap.bundle.js"></script> </body> </html> |
In Bootstrap 5, you can create button shadows by using the built-in shadow utility class. Here’s an example code that demonstrates how to create button shadows in Bootstrap 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Bootstrap shadow effect Tutorial</title> <link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/style.css"> <style> /* custom button styles */ .btn-custom { margin-top:30px; background-color: #007bff; color: white; border-radius: 0px; } /* button shadows */ .btn-shadow { box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.5); } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6"> <button class="btn btn-custom">Normal Button</button> </div> <div class="col-md-6"> <button class="btn btn-custom btn-shadow">Shadow Button</button> </div> </div> </div> </body> </html> |
In this example, we have created a custom button style .btn-custom that defines the background color, text color, and border radius of the button. We have also defined a new CSS class .btn-shadow that applies a shadow effect to the button by using the box-shadow property.
To apply the shadow effect to the button, simply add the .btn-shadow class to the button element. You can customize the shadow effect by changing the values of the box-shadow property. To learn the most used classes in Bootstrap 5 then read my article.Â