如何在 reactjs 中动态更改字体大小和字体粗细?
How to change font size and font weight dynamically in reactjs?
我正在尝试动态更改我的 React 应用程序的 header 部分。
我想为我的 React Web 应用程序的主页和其他页面使用不同的字体大小、字体粗细、标题和副标题。
这就是我想要的主页。
你好 在首页上应该小一点但是 欢迎回来 应该大一点
这就是我想要在其他页面上显示的内容。
你好 在主页上应该更大一些但是 lorem ipsum lorem ipsum 应该很小
这是我的主页标题代码
const Hero = ({ fontSize, fontWeight, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p className="font-heading text-lg font-normal text-white">Hello There ,</p>
<p className="font-heading text-3xl font-bold text-white">{subTitle}</p>
// another react component
</div>
)
}
export default Hero
我想知道如何动态更改此代码,以便我可以将它用于其他页面,绕过字体粗细、字体大小、标题和副标题的不同属性。
我正在使用 react 和 tailwind css
任何人请帮助我解决这个问题。
谢谢你
已编辑:
{pathName === '/' ? (
<>
<p className="font-heading text-lg font-normal text-white">`Hello There ,</p>
<p className="font-heading text-3xl font-semibold text-white">{subTitle}</p>
</>
) : (
<>
<p className="font-heading text-3xl font-semibold text-white">Hello There ,</p>
<p className="font-heading text-lg font-normal text-white">subTitle</p>
</>
)}
您可以添加内联样式
const Hero = ({ fontSize, fontWeight, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p style={{fontSize:fontSize, fontWeight:fontWeight}} className="font-heading text-lg font-normal text-white">{title}</p>
<p className="font-heading text-3xl font-bold text-white">{subTitle}</p>
</div>
)
}
export default Hero
并对另一个元素做相应的处理
编辑:
或者你可以传入 fontSize 和 fontWeight 作为 calss names
const Hero = ({ titleFontSize,subTitleFontSize, titleFontWeight, subTitleFontWeight, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p className={`font-heading ${titleFontSize} ${titleFontWeight} text-white`}>{title}</p>
<p className={`font-heading ${subTitleFontSize} ${subTitleFontWeight} text-white`}>{subTitle}</p>
</div>
)
}
export default Hero
然后每当你使用该组件时,你都会传递这些道具,例如
<Hero titleFontSize="text-lg" subTitleFontSize="text-3xl" tileFontWeight="font-normal" subTitleFontWeight="font-bold" title="Title" subTitle="Sub Title" />
或使用if语句
const Hero = ({ scenario1, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p className={`font-heading ${scenario1 ? "text-lg font-normal" : "text-3xl font-bold"} text-white`}>{title}</p>
<p className={`font-heading ${scenario1 ? "text-3xl font-bold" : "text-lg font-normal"} text-white`}>{subTitle}</p>
</div>
)
}
export default Hero
并像这样使用它
<Hero title="Title" subTitle="Subtitle" scenario1/> // This will render Scenario 1
<Hero title="Title" subTitle="Subtitle"/> // This will render default -in your case its Scenario 2
我编辑我的答案
检查这个:
<p
className={"font-heading text-white " +
(pathName !== '/'? "text-3xl font-semibold" : "text-lg font-normal")
}
>Hello There ,
</p>
<p
className={"font-heading text-white " +
(pathName === '/'? "text-3xl font-semibold" : "text-lg font-normal")
}
>
{subTitle}
</p>
我正在尝试动态更改我的 React 应用程序的 header 部分。
我想为我的 React Web 应用程序的主页和其他页面使用不同的字体大小、字体粗细、标题和副标题。
这就是我想要的主页。
你好 在首页上应该小一点但是 欢迎回来 应该大一点
这就是我想要在其他页面上显示的内容。
你好 在主页上应该更大一些但是 lorem ipsum lorem ipsum 应该很小
这是我的主页标题代码
const Hero = ({ fontSize, fontWeight, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p className="font-heading text-lg font-normal text-white">Hello There ,</p>
<p className="font-heading text-3xl font-bold text-white">{subTitle}</p>
// another react component
</div>
)
}
export default Hero
我想知道如何动态更改此代码,以便我可以将它用于其他页面,绕过字体粗细、字体大小、标题和副标题的不同属性。 我正在使用 react 和 tailwind css
任何人请帮助我解决这个问题。 谢谢你 已编辑:
{pathName === '/' ? (
<>
<p className="font-heading text-lg font-normal text-white">`Hello There ,</p>
<p className="font-heading text-3xl font-semibold text-white">{subTitle}</p>
</>
) : (
<>
<p className="font-heading text-3xl font-semibold text-white">Hello There ,</p>
<p className="font-heading text-lg font-normal text-white">subTitle</p>
</>
)}
您可以添加内联样式
const Hero = ({ fontSize, fontWeight, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p style={{fontSize:fontSize, fontWeight:fontWeight}} className="font-heading text-lg font-normal text-white">{title}</p>
<p className="font-heading text-3xl font-bold text-white">{subTitle}</p>
</div>
)
}
export default Hero
并对另一个元素做相应的处理
编辑: 或者你可以传入 fontSize 和 fontWeight 作为 calss names
const Hero = ({ titleFontSize,subTitleFontSize, titleFontWeight, subTitleFontWeight, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p className={`font-heading ${titleFontSize} ${titleFontWeight} text-white`}>{title}</p>
<p className={`font-heading ${subTitleFontSize} ${subTitleFontWeight} text-white`}>{subTitle}</p>
</div>
)
}
export default Hero
然后每当你使用该组件时,你都会传递这些道具,例如
<Hero titleFontSize="text-lg" subTitleFontSize="text-3xl" tileFontWeight="font-normal" subTitleFontWeight="font-bold" title="Title" subTitle="Sub Title" />
或使用if语句
const Hero = ({ scenario1, title, subTitle }) => {
return (
<div className="mt-2 mb-8">
<p className={`font-heading ${scenario1 ? "text-lg font-normal" : "text-3xl font-bold"} text-white`}>{title}</p>
<p className={`font-heading ${scenario1 ? "text-3xl font-bold" : "text-lg font-normal"} text-white`}>{subTitle}</p>
</div>
)
}
export default Hero
并像这样使用它
<Hero title="Title" subTitle="Subtitle" scenario1/> // This will render Scenario 1
<Hero title="Title" subTitle="Subtitle"/> // This will render default -in your case its Scenario 2
我编辑我的答案
检查这个:
<p
className={"font-heading text-white " +
(pathName !== '/'? "text-3xl font-semibold" : "text-lg font-normal")
}
>Hello There ,
</p>
<p
className={"font-heading text-white " +
(pathName === '/'? "text-3xl font-semibold" : "text-lg font-normal")
}
>
{subTitle}
</p>