How to style href links in React using Tailwind CSS?

PHPz
Release: 2023-09-12 10:29:14
forward
844 people have browsed it

如何使用 Tailwind CSS 在 React 中设置 href 链接的样式?

React is a popular JavaScript library for building web applications. When creating a React application, it's important to style your components in a beautiful way. One way to achieve this is to use a CSS framework such as Tailwind CSS.

In this article, we will learn how to style href links in React using Tailwind CSS and the different methods or classes available in Tailwind CSS.

prerequisites

To use Tailwind CSS in a React application, we first need to install it. Let’s look at the steps to create a new React project and install tailwind CSS.

Step 1: Create a new React application

To create a new React application, you can use the create-react-app command. Open a terminal or command prompt and run the following command -

npx create-react-app my-app
Copy after login

Step 2: Install Tailwind CSS

To install Tailwind CSS in your React application, you need to run the following command in the terminal or command prompt -

npm install tailwindcss
Copy after login

Step 3: Create a configuration file for Tailwind CSS

After installing Tailwind CSS, you need to create a configuration file to customize the default settings. To do this, run the following command in the terminal or command prompt.

npx tailwindcss init
Copy after login

Step 4: Configure PostCSS

Tailwind CSS requires PostCSS to handle CSS. To configure PostCSS in your React application, create a new file called postcss.config.js in the root directory of your application and add the following code

module.exports = {
   plugins: [
      require('tailwindcss'),
      require('autoprefixer'),
   ],
};
Copy after login

Step 5: Import Tailwind CSS into the project

To use Tailwind CSS in a React application, you need to import it into your index.css file. Open the src/index.css file and add the following lines at the top -

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
Copy after login

That's it! You have successfully created a new React application and installed Tailwind CSS. You can now customize the styling by modifying the tailwind.config.js file and using the Tailwind CSS classes in your React component.

We can also use Tailwind CSS CDN in HTML files without creating a React application. Therefore, for the purpose of demonstrating this article, we will use this method.

Method 1: Using the ClassName attribute

The first way to style href links available in tags in React is to use the className property of Tailwind CSS. In this method, we create a CSS class with the help of Tailwind CSS and then apply the className attribute of the tag. Now, in the className attribute, we define the style used in tailwind, for example, to define the font size of a text paragraph as large, we can use text-lg,, etc. .

grammar

The following is the syntax that defines how to use the className attribute in React using Tailwind CSS -

import React from 'react';
import './styles.css';
function App() {
   return (
      <div>
         <a href="https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b" className="text-blue-500 underline font-bold">My Link</a>
      </div>
   );
}
export default App;
Copy after login

In this syntax, we use the className attribute to define the style of the href link. We defined styles such as the "text-blue-500" class to set the text to blue, the "underline" class to underline links, and the "font-bold" class to set the font-weight to bold.

Example

In this example, we import the libraries and scripts required to use React and Tailwind CSS. We defined a React component called "App" that renders some component's title, paragraph, and anchor tags.

Here, we use the className attribute of the Tailwind class to set the background color, text color, font weight, padding and border radius of the href link.

<html>
<head>
   <title>Style href Links in React using Tailwind CSS</title>
   <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
   <div id="react"></div>
   <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
   <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
   <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
   <script type="text/babel">
      class App extends React.Component { render() { return (
         <div className="p-4">
            <h2 className="text-2xl font-bold mb-4">Welcome to Tutorialspoint </h2>
            <p className="mb-4 text-green-700">
               Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
            </p>
            <a href="https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b" className="bg-green-500 bg-green-800 text-white font-bold py-2 px-4 rounded">  Search </a>
        </div>
      ); } } ReactDOM.render(
      <App />, document.getElementById('react') );
   </script>
</body>
</html>
Copy after login

Method 2: Using Tailwind JIT

The second way to style href links available in tags in React is to use Tailwind CSS JIT or Just-in-Time. Tailwind CSS's JIT or just-in-time compiler is used to generate styles on demand as we write templates, rather than generating everything upfront at the beginning of development.

In this approach, we enable JIT mode in Tailwind CSS and apply the class directly to the href attribute in the tag using the className attribute.

grammar

The following is the syntax that defines how to use Tailwind CSS JIT in React -

<style>
   /* styles for href using JIT method */
   .new-link {
      @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
   }
</style>
/*In the <body> */
<a href="https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b" class="class-name">My Link</a>
Copy after login

In this syntax, we first define a custom class named .new-link using the @apply directive to apply the Tailwind CSS class. Afterwards, this custom class is added to the href class attribute to use the defined styles.

Example

In this example, we define a CSS class called new-link that applies the Tailwind CSS class using the @apply directive. We defined a React component called "App" that renders some component's title, paragraph, and anchor tags.

In this method, when the component is rendered, the anchor tag will be styled using the new-link CSS class defined in the style tag.

<html>
<head>
   <title>Style href Links in React using Tailwind CSS</title>
   <link rel="stylesheet" href="https://cdn.tailwindcss.com/just-in-time/3.3.1/tailwind.min.css">
   <style>
      /* styles for href using JIT method */
      .new-link {
         @apply bg-blue-500 hover: bg-blue-700 text-white font-bold py-2 px-4 rounded;
      }
   </style>
</head>
<body>
   <div id="react"></div>
   <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
   <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
   <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
   <script type="text/babel">
      class App extends React.Component { render() { return (
         <div className="p-4">
            <h2 className="text-2xl font-bold mb-4">Welcome to Tutorialspoint</h2>
            <p className="mb-4 text-green-700">
               Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
            </p>
            <a href="https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b" class="new-link"> Search </a>
         </div>
      ); } } ReactDOM.render(
      <App />, document.getElementById('react') );
   </script>
</body>
</html>
Copy after login

In this article, we learned how to style href links in React using Tailwind CSS. We have two different ways to style href links.

The above is the detailed content of How to style href links in React using Tailwind CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!