How To Add Custom Box Shadow In Tailwind CSS?
Last Updated :
03 Oct, 2024
Tailwind CSS is a utility-focused CSS framework. Offers a wide range of design utilities. It does include predefined shading, however, for unique design needs, you might want a custom box shadow that goes beyond the default. in this article, you will learn how to extend Tailwind's default shadow and how to create a custom box shadow utility in your project.
Approach
Start by creating and initializing a new project with npm init -y, then install Tailwind CSS with PostCSS and Autoprefixer. Next, generate a tailwind.config.js file using npx tailwindcss init, set the content paths, and extend the theme to add custom shadows. In the public/tailwind.css file, include the base, components, and utilities from Tailwind. Then, build the CSS by running npx tailwindcss -i public/tailwind.css -o public/styles.css --watch. Lastly, create an index.html file in the public folder to test your custom shadows, and open it in the browser to see how they look.
Steps to Create & Configure the Project
Step 1: Create a New Project Directory
Firstly, we must create a new project directory and navigate to it using the following command in the terminal.
mkdir tailwind-custom-shadow
cd tailwind-custom-shadow
Next you need to initialize the project using the following command:
npm init -y
Step 2: Install Tailwind CSS
When you create a new project you will need to install Tailwind CSS and create a configuration file named tailwind.config.js, you can do this by running following command.
npm install tailwindcss postcss autoprefixer
npx tailwindcss init
Step 3: Configure Template Paths
Edit tailwind.config.js file to add content paths for your HTML and JavaScript files.
JavaScript
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./public/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
Step 4: Add Tailwind Directives
In public/tailwind.css file, add base, components and utilities layers of Tailwind:
@tailwind base;
@tailwind components;
@tailwind utilities;
Step 5: Extend Tailwind for Custom Shadows
To add custom box shadows, modify tailwind.config.js file. Add custom shadows to theme.extend section:
JavaScript
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./public/**/*.{html,js}"],
theme: {
extend: {
boxShadow: {
'custom-light': `4px 4px 10px
rgba(0, 0, 0, 0.1)`,
'custom-dark': `6px 6px 15px
rgba(0, 0, 0, 0.3)`,
'custom-color': `5px 5px 20px
rgba(34, 60, 80, 0.7)`,
},
},
},
plugins: [],
}
Step 6: Create the HTML File
Now create public/index.html
file to apply and test your custom box shadows:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Custom Shadows</title>
<link href="./styles.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
<h1 class="text-center text-green-600 text-4xl font-bold mb-8">GeeksforGeeks</h1>
<div class="p-8">
<div class="shadow-custom-light p-6 bg-white rounded mb-6">
Light Custom Shadow
</div>
<div class="shadow-custom-dark p-6 bg-white rounded mb-6">
Dark Custom Shadow
</div>
<div class="shadow-custom-color p-6 bg-white rounded">
Colorful Custom Shadow
</div>
</div>
</body>
</html>
Project Structure
Folder StructureStep 7: Build the Tailwind CSS File
In your terminal run Tailwind build process to generate final styles.css:
npx tailwindcss -i public/tailwind.css -o public/styles.css --watch
We now have our custom shadow ready and applied to HTML element.
Output: Open index.html file in your browser to see custom box shadows applied.
Add Custom Box Shadow In Tailwind CSS
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read
Use Case Diagram - Unified Modeling Language (UML)
A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read