如何在 React 中使用 Tailwind CSS 樣式化 a href 連結?


React 是一個流行的 JavaScript 庫,用於構建 Web 應用程式。在建立 React 應用程式時,以美觀的方式為元件設定樣式非常重要。實現此目標的方法之一是使用 CSS 框架,例如 Tailwind CSS。

在本文中,我們將瞭解如何使用 Tailwind CSS 中提供的不同方法或類來為 React 中的 a href 連結設定樣式。

先決條件

要在 React 應用程式中使用 Tailwind CSS,我們首先需要安裝它。讓我們看看建立新的 React 專案以及安裝 Tailwind CSS 的步驟。

步驟 1:建立一個新的 React 應用程式

要建立一個新的 React 應用程式,可以使用 create-react-app 命令。開啟您的終端或命令提示符並執行以下命令:

npx create-react-app my-app

步驟 2:安裝 Tailwind CSS

要在您的 React 應用程式中安裝 Tailwind CSS,您需要在終端或命令提示符中執行以下命令:

npm install tailwindcss

步驟 3:為 Tailwind CSS 建立配置檔案

安裝 Tailwind CSS 後,您需要建立一個配置檔案來自定義預設設定。為此,請在您的終端或命令提示符中執行以下命令。

npx tailwindcss init

步驟 4:配置 PostCSS

Tailwind CSS 需要 PostCSS 來處理 CSS。要在您的 React 應用程式中配置 PostCSS,請在應用程式的根目錄中建立一個名為 postcss.config.js 的新檔案,並新增以下程式碼

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

步驟 5:在您的專案中匯入 Tailwind CSS

要在您的 React 應用程式中使用 Tailwind CSS,您需要將其匯入您的 index.css 檔案。開啟 src/index.css 檔案並在頂部新增以下行:

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

就是這樣!您已成功建立了一個新的 React 應用程式並安裝了 Tailwind CSS。您現在可以透過修改 tailwind.config.js 檔案並在 React 元件中使用 Tailwind CSS 類來自定義樣式。

我們也可以在 HTML 檔案中使用 Tailwind CSS CDN,而無需建立 React 應用程式。因此,為了演示本文,我們將使用這種方法。

方法 1:使用 className 屬性

在 React 中的 <a> 標籤中設定 href 連結樣式的第一種方法是使用 Tailwind CSS 的 className 屬性。在這種方法中,我們使用 Tailwind CSS 建立一個 CSS 類,然後應用 <a> 標籤的 className 屬性。現在,在 className 屬性中,我們定義了在 Tailwind 中使用的樣式,例如,要將文字段落的字型大小定義為大號,我們可以使用 text-lg,等等。

語法

以下是定義如何在 React 中使用 Tailwind CSS 的 className 屬性的語法:

import React from 'react';
import './styles.css';
function App() {
   return (
      <div>
         <a href="#" className="text-blue-500 underline font-bold">My Link</a>
      </div>
   );
}
export default App;

在此語法中,我們使用 className 屬性定義了 href 連結的樣式。我們定義了樣式,例如 ‘text-blue-500’ 類將文字設定為藍色,‘underline’ 類為連結新增下劃線,並使用 ‘font-bold’ 類將字型粗細設定為粗體。

示例

在此示例中,我們匯入了使用 React 和 Tailwind CSS 所需的庫和指令碼。我們定義了一個名為“App”的 React 元件,它呈現一些元件的標題、段落和錨標籤。

在這裡,我們使用了帶有 Tailwind 類的 className 屬性來設定 href 連結的背景顏色、文字顏色、字型粗細、填充和邊框半徑。

<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="#" 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>

方法 2:使用 Tailwind JIT

在 React 的 <a> 標籤中設定 href 連結樣式的第二種方法是使用 Tailwind CSS JIT(Just-in-Time)。Tailwind CSS 的 JIT 或 Just-in-Time 編譯器用於根據我們編寫模板時按需生成樣式,而不是在開發初期預先生成所有內容。

在這種方法中,我們在 Tailwind CSS 中啟用 JIT 模式,並使用 className 屬性將類直接應用於 <a> 標籤中的 href 屬性。

語法

以下是定義如何在 React 中使用 Tailwind CSS JIT 的語法:

<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="#" class="class-name">My Link</a>

在此語法中,我們首先使用 @apply 指令定義了一個名為 .new-link 的自定義類來應用 Tailwind CSS 類。然後,此自定義類新增到 href class 屬性中以使用定義的樣式。

示例

在此示例中,我們定義了一個名為 new-link 的 CSS 類,該類使用 @apply 指令應用 Tailwind CSS 類。我們定義了一個名為“App”的 React 元件,它呈現一些元件的標題、段落和錨標籤。

在此方法中,當元件呈現時,錨標籤將使用在 style 標籤中定義的 new-link CSS 類進行樣式設定。

<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="#" class="new-link"> Search </a>
         </div>
      ); } } ReactDOM.render(
      <App />, document.getElementById('react') );
   </script>
</body>
</html>

在本文中,我們瞭解瞭如何在 React 中使用 Tailwind CSS 設定 href 連結的樣式。我們介紹了兩種不同的方法來設定 href 連結的樣式。

更新於:2023年5月2日

瀏覽量 1K+

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告