在元素上边界中心的尾风CSS对齐文本
我正在尝试使用 Tailwind CSS 来对齐另一个元素边框内的某些文本,以便元素周围的边框在该元素周围绘制一个周边,并与边框中心顶部的文本断开。
这是在 React 组件内完成的。
我对 Tailwind CSS 很陌生。事实上,我想要的目标太新了,来自以下 StackOverflow 问题,但是那里提供的解决方案似乎不起作用。
我尝试了以下操作:
- 将
mt-5
或pd-5
添加到h2
和span
的类中> 我的标签的元素,试图添加边距以将标签向下移动到边框中,但这似乎不起作用。 - 将包含
span
元素的h2
元素包装在div
中,并应用mt-5
或pd- 5
,但这也不起作用。 - 我还尝试通过 Chrome 开发工具编辑浏览器中的边距,但这没有帮助。
我的代码:
export default function Skills() {
return (
<section id="skills">
<div className="container px-5 py-10 mx-auto">
<div className="pt-3">
<h2 className="w-full text-center leading-border-text mt-5">
<span className="text-sm font-medium">My desired label</span>
</h2>
<div className="flex flex-wrap px-5 pb-5 pt-4 border-b border-t border-r border-l border-gray-600 rounded-md">
Some content here.
</div>
</div>
</div>
</section>
);
}
leading-border-text
TailwindCSS 类是我在 tailwind.config.js
中定义的一个类,用于简单地添加 0.1 的自定义行高。
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {
lineHeight: {
'border-text': '0.1'
}
},
},
plugins: [],
}
这是我的代码当前呈现的内容:
我希望该文本,我想要的标签
与其下方的边框对齐。
我做错了什么?
I am trying to use Tailwind CSS to align some text within the border of another element, so that the border around my element draws a perimeter around the element, and has a break with the text in the center top of the border.
This is being done within a React component.
I am very new to Tailwind CSS. So new, in fact, that my desired goal comes from the following StackOverflow Question, but the solutions provided there do not appear to work.
I have tried the following:
- Adding
mt-5
orpd-5
to both the class for theh2
andspan
elements for my label, in an effort to add a margin to move the label down into the border, but this does not seem to work. - Wrapping the
h2
element containing thespan
element in adiv
and applyingmt-5
orpd-5
to that, but that also did not work. - I've also tried editing the margin in-browser via the Chrome Development tools, but that did not help.
My Code:
export default function Skills() {
return (
<section id="skills">
<div className="container px-5 py-10 mx-auto">
<div className="pt-3">
<h2 className="w-full text-center leading-border-text mt-5">
<span className="text-sm font-medium">My desired label</span>
</h2>
<div className="flex flex-wrap px-5 pb-5 pt-4 border-b border-t border-r border-l border-gray-600 rounded-md">
Some content here.
</div>
</div>
</div>
</section>
);
}
The leading-border-text
TailwindCSS class is one that I have defined in my tailwind.config.js
to simply add a custom line height of 0.1.
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {
lineHeight: {
'border-text': '0.1'
}
},
},
plugins: [],
}
This is what my code currently renders:
And I'd like for that text, My desired label
to be in-line with the border just below it.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会
绝对
ly将标签定位在相对
parent element中的标签中:这样,标签即使其大小变化,标签也将始终以相对父元素的顶线为中心。
I would
absolute
ly position the label in arelative
parent element like so:This way the label will always be centered on the top line of the relative parent element even if its size changes.
经过一番思考,我对自己的问题提出了一个解决方案。
由于某种我不太清楚的原因,添加
mt-5
并没有将标签向下移动,而是添加了一个负的 margin-bottom 类,在我的例子中-mb-2
,做到了。如果有人能解释为什么会出现这种情况,我会很高兴。我的解决方案代码:
我的解决方案代码呈现的内容:
After some more thought, I came up with a solution to my own question.
For some reason that is not immediately clear to me, adding
mt-5
did not move the label down but adding a negative margin-bottom class, in my case-mb-2
, did. I would love it if someone could explain why this is the case.My solution code:
What my solution code renders: