Tailwind CSS在flex flex-col内垂直和水平居中?

huangapple go评论96阅读模式
英文:

Tailwind CSS center vertically and horizontally inside flex flex-col?

问题

我已经有以下代码并尝试添加"justify-content"、"align-middle"和其他属性,但似乎仍然无法使"h5"和链接在包含的div中垂直居中对齐。我应该以另一种方式构建这个结构,而不使用"flex"或"flex-col"吗?

<section id="cta-image-box" class="container mx-auto py-24 mt-32 mb-32">
	<div class="h-[335px] rounded-[40px] bg-slate-500 bg-bgCTA bg-cover bg-center bg-blend-multiply">
		<div class="flex flex-col items-center">
			<h5 class="text-5xl font-bold text-white mb-10">
				Lorem ipsum dolor site <span class="text-gray-300">15% OFF</span> amet
			</h5>
			<a href="#" alt="" class="px-6 py-3 w-fit rounded-full bg-slate-200 text-gray-800 hover:bg-slate-800 hover:text-white">
				Discount Code
			</a>
		</div>
	</div>
</section>
英文:

I've got the following code and have tried adding "justify-content", "align-middle", and others but still can't seem to get it to align the h5 and link in the middle of the containing div. Should I be structuring this another way, without using "flex" or "flex-col"?

&lt;section id=&quot;cta-image-box&quot;
         class=&quot;container mx-auto py-24 mt-32 mb-32&quot;&gt;
	&lt;div class=&quot;h-[335px] rounded-[40px] bg-slate-500 bg-bgCTA bg-cover bg-center bg-blend-multiply&quot;&gt;
		&lt;div class=&quot;flex flex-col items-center&quot;&gt;
			&lt;h5 class=&quot;text-5xl font-bold text-white mb-10&quot;&gt;
				Lorem ipsum dolor site &lt;span class=&quot;text-gray-300&quot;&gt;15% OFF&lt;/span&gt; amet
			&lt;/h5&gt;
			&lt;a href=&quot;#&quot;
			   alt=&quot;&quot;
			   class=&quot;px-6 py-3 w-fit rounded-full bg-slate-200 text-gray-800 hover:bg-slate-800 hover:text-white&quot;&gt;
				Discount Code
			&lt;/a&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/section&gt;

答案1

得分: 1

你可以在包裹的div上使用h-fullplace-content-centerplace-items-center来实现这个效果。

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

<body>
  <section id="cta-image-box" class="container mx-auto py-24 mt-32 mb-32">
    <div class="h-[335px] rounded-[40px] bg-slate-500 bg-bgCTA bg-cover bg-center bg-blend-multiply">
      <div class="h-full flex flex-col place-content-center text-center place-items-center">
        <h5 class="text-5xl font-bold text-white mb-10">
          Lorem ipsum dolor site <span class="text-gray-300">15% OFF</span> amet
        </h5>
        <a href="#" alt="" class="px-6 py-3 w-fit rounded-full bg-slate-200 text-gray-800 hover:bg-slate-800 hover:text-white">
          Discount Code
        </a>
      </div>
    </div>
  </section>
</body>
英文:

You can achieve this using h-full and place-content-center place-items-center on wrapper div.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;head&gt;
  &lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;
  &lt;section id=&quot;cta-image-box&quot; class=&quot;container mx-auto py-24 mt-32 mb-32&quot;&gt;
    &lt;div class=&quot;h-[335px] rounded-[40px] bg-slate-500 bg-bgCTA bg-cover bg-center bg-blend-multiply&quot;&gt;
      &lt;div class=&quot;h-full flex flex-col place-content-center text-center place-items-center&quot;&gt;
        &lt;h5 class=&quot;text-5xl font-bold text-white mb-10&quot;&gt;
          Lorem ipsum dolor site &lt;span class=&quot;text-gray-300&quot;&gt;15% OFF&lt;/span&gt; amet
        &lt;/h5&gt;
        &lt;a href=&quot;#&quot; alt=&quot;&quot; class=&quot;px-6 py-3 w-fit rounded-full bg-slate-200 text-gray-800 hover:bg-slate-800 hover:text-white&quot;&gt;
                Discount Code
            &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/section&gt;
&lt;/body&gt;

<!-- end snippet -->

答案2

得分: 1

看一下这个答案,它已经解释了如何垂直居中对齐,非常清楚。

下面,你可以看到如何将它应用到你的代码中。你应该给这些类:flex justify-center items-center

<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"/>
<section id="cta-image-box" class="container mx-auto py-24 mt-32 mb-32">
  <div class="flex justify-center items-center h-[335px] rounded-[40px] bg-slate-500 bg-bgCTA bg-cover bg-center bg-blend-multiply">
    <div class="flex flex-col items-center">
      <h5 class="text-5xl font-bold text-white mb-10">
        Lorem ipsum dolor site <span class="text-gray-300">15% OFF</span> amet
      </h5>
      <a href="#" alt="" class="px-6 py-3 w-fit rounded-full bg-slate-200 text-gray-800 hover:bg-slate-800 hover:text-white">
        Discount Code
      </a>
    </div>
  </div>
</section>
英文:

Take a look to that answer which is already explained how to align vertically center quite clear.

Below, you can see how to apply it to your code. You should give these classes: flex justify-center items-center.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;section id=&quot;cta-image-box&quot; class=&quot;container mx-auto py-24 mt-32 mb-32&quot;&gt;
  &lt;div class=&quot;flex justify-center items-center h-[335px] rounded-[40px] bg-slate-500 bg-bgCTA bg-cover bg-center bg-blend-multiply&quot;&gt;
    &lt;div class=&quot;flex flex-col items-center&quot;&gt;
      &lt;h5 class=&quot;text-5xl font-bold text-white mb-10&quot;&gt;
        Lorem ipsum dolor site &lt;span class=&quot;text-gray-300&quot;&gt;15% OFF&lt;/span&gt; amet
      &lt;/h5&gt;
      &lt;a href=&quot;#&quot; alt=&quot;&quot; class=&quot;px-6 py-3 w-fit rounded-full bg-slate-200 text-gray-800 hover:bg-slate-800 hover:text-white&quot;&gt;
          Discount Code
        &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 06:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定