How to Create Gradient Text with Tailwind CSS?
Last Updated :
14 Oct, 2024
Gradient text adds visual appeal to user interfaces enhancing the design and attracting attention to important elements. we will learn how to create gradient text using Tailwind CSS in a React project. Gradient text can add a vibrant and visually appealing effect to your UI and make important headlines or elements stand out.
Prerequisites
Steps to Create & Configure the Project
We will create a sample React JS project, then we will install Tailwind CSS once it is completed. We will start development for gradient text using React and Tailwind CSS. Below are the steps to create and configure the project
Step 1: Set up a React Application.
First, create a sample React JS application by using the command then navigate to the project folder.
npx create-react-app react-app
cd react-app
Project Structure:
project folderUpdated Dependencies:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Step 2: Install and Configure Tailwind CSS
Once Project is created successfully Now install and configure the Tailwind css by using below commands in your project.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Step 3: Develop Business logic
Once Tailwind css installation and configuration is completed. Now we need to develop user interface for gradient text using tailwind CSS and for making it responsive we will use App.js and App.css files.
- App.js ( src\App.js )
- index.css ( src\index.js )
- tailwind.config.js ( tailwind.config.js )
Example: This demonstrates the creation of gradient text using React and Tailwind CSS.
CSS
/* index.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
body {
margin: 0;
font-family: 'Times New Roman', Times, serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
JavaScript
// scr/App.js
function App() {
return (
<div className="flex items-center justify-center bg-green-100 creen">
<h1 className="text-6xl font-bold text-transparent
bg-gradient-to-r from-green-500 via-purple-500
to-indigo-500 bg-clip-text">
Welcome To GeeksForGeeks
</h1>
</div>
);
}
export default App;
JavaScript
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {
},
},
plugins: [],
}
Step 4: Run the Application
Once development is completed now, we need to run the React JS application by using the below command. By default, the React JS application run on port number 3000.
npm start
Output: Once project is successfully running then open the below URL to test the output.
http://localhost:3000/
outputConclusion
Tailwind CSS offers powerful utilities to easily create complex visual effects like gradient text. By using its built in classes, we can efficiently apply a beautiful gradient effect to text by adding a dynamic and modern touch to the user interface. With this approach developers can focus more on functionality while still achieving visually stunning results with minimal CSS code.