在元素上边界中心的尾风CSS对齐文本

发布于 2025-01-19 09:59:05 字数 1985 浏览 0 评论 0原文

我正在尝试使用 Tailwind CSS 来对齐另一个元素边框内的某些文本,以便元素周围的边框在该元素周围绘制一个周边,并与边框中心顶部的文本断开。

这是在 React 组件内完成的。

我对 Tailwind CSS 很陌生。事实上,我想要的目标太新了,来自以下 StackOverflow 问题,但是那里提供的解决方案似乎不起作用。

我尝试了以下操作:

  • mt-5pd-5 添加到 h2span 的类中> 我的标签的元素,试图添加边距以将标签向下移动到边框中,但这似乎不起作用。
  • 将包含 span 元素的 h2 元素包装在 div 中,并应用 mt-5pd- 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 or pd-5 to both the class for the h2 and span 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 the span element in a div and applying mt-5 or pd-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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心未许 2025-01-26 09:59:05

我会绝对 ly将标签定位在相对 parent element中的标签中:

<script src="https://cdn.tailwindcss.com"></script>


<div class="bg-gray-700 min-h-screen text-gray-400">

  <!-- Skills() -->
  <div class="container mx-auto px-5 py-10">
    <div class="relative rounded-md border border-gray-600">
      <p class="p-3">Some content here.</p>
      <h2 class="absolute flex top-0 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
        <span class="bg-gray-700 px-2 text-sm font-medium">My desired label</span>
      </h2>
    </div>
  </div>
  <!-- Skills() -->

</div>

这样,标签即使其大小变化,标签也将始终以相对父元素的顶线为中心。

I would absolutely position the label in a relative parent element like so:

<script src="https://cdn.tailwindcss.com"></script>


<div class="bg-gray-700 min-h-screen text-gray-400">

  <!-- Skills() -->
  <div class="container mx-auto px-5 py-10">
    <div class="relative rounded-md border border-gray-600">
      <p class="p-3">Some content here.</p>
      <h2 class="absolute flex top-0 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
        <span class="bg-gray-700 px-2 text-sm font-medium">My desired label</span>
      </h2>
    </div>
  </div>
  <!-- Skills() -->

</div>

This way the label will always be centered on the top line of the relative parent element even if its size changes.

怼怹恏 2025-01-26 09:59:05

经过一番思考,我对自己的问题提出了一个解决方案。

由于某种我不太清楚的原因,添加 mt-5 并没有将标签向下移动,而是添加了一个负的 margin-bottom 类,在我的例子中 -mb-2,做到了。如果有人能解释为什么会出现这种情况,我会很高兴。

我的解决方案代码:


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 -mb-2 pr-2 pl-2">
                        <span className="bg-gray-900 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>
    );
}

我的解决方案代码呈现的内容:

在此处输入图像描述

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:


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 -mb-2 pr-2 pl-2">
                        <span className="bg-gray-900 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>
    );
}

What my solution code renders:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文