React项目Tailwind文本颜色异常:灰色变蓝色且带下划线求助
我正在开发一个React JS项目,采用Node和npm作为包管理器,项目中引入了heroicons、Tailwind CSS等多个依赖包。我希望页脚文本显示为灰色,因此在Footer.js文件中使用了text-slate-500类,但实际显示效果却是蓝色且带有下划线,而我并未在代码中设置下划线属性。我是前端开发及Tailwind CSS的新手,非常感谢您的帮助,若有信息缺失敬请谅解。
相关文件
Footer.js
import React from 'react' import { HomeIcon } from '@heroicons/react/outline' export const Footer = () => { return ( <footer className="mx-auto mt-32 w-full max-w-container px-4 sm:px-6 lg:px-8"> <div className="border-t border-slate-900/5 py-10 row-start-auto"> <p className="mt-5 text-center text-sm leading-6 text-slate-500">© {new Date().getFullYear()} Hochschule für angewandte Wissenschaften Würzburg - Schweinfurt</p> <div className="mt-16 flex items-center justify-center space-x-4 text-sm font-semibold leading-6 text-slate-700"> <a href="/datenschutz">Datenschutz</a> <div className="h-4 w-px bg-slate-500/20"> </div> <a href="/impressum">Impressum</a> <div className="h-4 w-px bg-slate-500/20"></div> <a href="/barrierefreiheit">Barrierefreiheit</a> <div className="h-4 w-px bg-slate-500/20"></div> <a href="/">Deutsch - English</a> </div> </div> </footer> ) } export default Footer;
package.json
{ "name": "aicard", "version": "0.1.0", "private": true, "dependencies": { "@headlessui/react": "^1.6.6", "@heroicons/react": "^2.0.10", "@testing-library/jest-dom": "^5.16.4", "@testing-library/react": "^13.1.1", "@testing-library/user-event": "^13.5.0", "axios": "^0.27.2", "bootstrap": "^5.1.3", "chart.js": "^3.9.1", "fast-equals": "^4.0.1", "io": "^1.0.5", "nodemon": "^2.0.16", "query-string": "^7.1.1", "react": "^18.1.0", "react-chartjs-2": "^4.3.1", "react-dom": "^18.1.0", "react-router-dom": "^6.3.0", "react-scripts": "^5.0.1", "socket.io-client": "^4.5.1", "web-vitals": "^2.1.4" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "devDependencies": { "autoprefixer": "^10.4.8", "postcss": "^8.4.16", "tailwindcss": "^3.1.8" } }
tailwind.config.js
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./src/**/*.{js,jsx,ts,tsx}", ], theme: { extend: { colors: { 'regal-blue': '#243c5a', 'gameblue': '#1d3f56', 'gamebluehover' : '#122736', 'gamebluelight' : '#244f6b', 'gamebluelightest' :'#326d94', 'fhwsorange' : '#dd742e', 'gameboardconfig' : '#2e4c61', }, }, }, plugins: [], }
问题原因
你同时引入了Bootstrap和Tailwind CSS,Bootstrap自带全局<a>标签默认样式:蓝色文本+下划线,这个样式的优先级覆盖了Tailwind的配置,导致你设置的text-slate-500不生效。
解决方案
方案1:移除Bootstrap(如果项目不需要)
执行卸载命令:
npm uninstall bootstrap
同时检查项目中是否有引入Bootstrap CSS的代码(比如import 'bootstrap/dist/css/bootstrap.min.css'),如果有需一并删除。
方案2:保留Bootstrap,用Tailwind类覆盖样式
给Footer中的所有<a>标签添加Tailwind的文本颜色类和去下划线类,修改后的代码如下:
import React from 'react' import { HomeIcon } from '@heroicons/react/outline' export const Footer = () => { return ( <footer className="mx-auto mt-32 w-full max-w-container px-4 sm:px-6 lg:px-8"> <div className="border-t border-slate-900/5 py-10 row-start-auto"> <p className="mt-5 text-center text-sm leading-6 text-slate-500">© {new Date().getFullYear()} Hochschule für angewandte Wissenschaften Würzburg - Schweinfurt</p> <div className="mt-16 flex items-center justify-center space-x-4 text-sm font-semibold leading-6 text-slate-700"> <a href="/datenschutz" className="text-slate-700 no-underline hover:underline">Datenschutz</a> <div className="h-4 w-px bg-slate-500/20"></div> <a href="/impressum" className="text-slate-700 no-underline hover:underline">Impressum</a> <div className="h-4 w-px bg-slate-500/20"></div> <a href="/barrierefreiheit" className="text-slate-700 no-underline hover:underline">Barrierefreiheit</a> <div className="h-4 w-px bg-slate-500/20"></div> <a href="/" className="text-slate-700 no-underline hover:underline">Deutsch - English</a> </div> </div> </footer> ) } export default Footer;
no-underline:移除Bootstrap默认的下划线text-slate-700:确保文本显示为你需要的灰色hover:underline:可选,保留鼠标悬停时的下划线效果,提升交互体验
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

