如何将两个右对齐的组件居中对齐

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

How to center 2 right-aligned components

问题

我目前有两个组件,我希望它们彼此右对齐。然而,我希望这两个组件位于网站的中心。我尝试使用一个 div 将这两个组件包裹起来,并将边距设置为 0 auto,但这并没有起作用。

这是当前的结果:这里
我想让这两个组件居中。

<div style={{margin: "0 auto"}}>
    <Countdown />
    <h3>'til 🕤</h3>
</div>

Countdown 和 h3 都具有 textAlign: right

英文:

I currently have 2 components that I want to be right-aligned with each other. However, I want these 2 components to be in the center of the website. I've tried wrapping these 2 components with a div, and setting the margin to 0 auto, but that is not working.

This is the current result: here.
I want to make the 2 components in the center.

&lt;div style={{margin: &quot;0 auto&quot;}}&gt; 
            &lt;Countdown /&gt;
            &lt;h3&gt;&#39;til &#128564;&lt;/h3&gt;
        &lt;/div&gt;

Countdown and h3 both have textAlign: right.

答案1

得分: 0

有很多方法可以使其居中;其中一种方法是使用 flex

<div className='App'>
  <div style={{
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    justifyContent:"right"
   }}> 
      <div>
        <h1 style={{textAlign: "right"}}>倒计时组件</h1>
        <h3 style={{textAlign: "right"}}>'til 😄</h3>
      </div>
    </div>
</div>

希望对你有帮助 如何将两个右对齐的组件居中对齐

英文:

There are bunch of ways you can make it center; one of the ways you can do it is using flex.

 &lt;div className=&#39;App&#39;&gt;
      &lt;div style={{
        display: &quot;flex&quot;,
        flexDirection: &quot;column&quot;,
        alignItems: &quot;center&quot;,
        justifyContent:&quot;right&quot;
       }}&gt; 
          &lt;div&gt;
            &lt;h1 style={{textAlign: &quot;right&quot;}}&gt;Countdown component&lt;/h1&gt;
            &lt;h3 style={{textAlign: &quot;right&quot;}}&gt;&#39;til &#128564;&lt;/h3&gt;
          &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

Hope it helps 如何将两个右对齐的组件居中对齐

答案2

得分: 0

当你尝试使用margin: "0 auto"来居中对齐元素时,通常需要为容器指定一个固定的宽度。

<div style={{ width: "50%", margin: "0 auto", textAlign: "right" }}>
    <Countdown />
    <h3>'til 🍔</h3>
</div>

当尝试使用margin: 0 auto和固定宽度来对齐组件时,可能会遇到灵活性的限制。具体而言,使用固定宽度可能无法很好地适应不同的内容大小或屏幕尺寸,可能导致布局问题或内容被裁剪。

考虑到这个挑战,这里有两种替代方法来对齐你的组件。

1. 使用 Flex 布局

Flexbox 提供了一种灵活的解决方案,允许你在保持内容右对齐的同时居中容器。

<div style={{ display: "flex", justifyContent: "center" }}>
    <div style={{ textAlign: "right" }}>
        <Countdown />
        <h3>'til 🍔</h3>
    </div>
</div>

2. 使用 CSS Grid

你可以使用 CSS Grid 创建类似的对齐效果。使用网格布局,你可以轻松地将内容居中并保持右对齐。

<div style={{ display: "grid", justifyContent: "center" }}>
    <div style={{ textAlign: "right" }}>
        <Countdown />
        <h3>'til 🍔</h3>
    </div>
</div>

这两种方法都可以提供更灵活和响应式的解决方案,以实现所描述的组件对齐效果。如果你想更深入地了解 Flexbox 和 CSS Grid 的对齐方式,你可能会发现这篇指南有帮助:CSS Grid & Flexbox 组件对齐指南

英文:

When you try to center-align elements using the margin: "0 auto", it often requires specifying a fixed width for the container

&lt;div style={{ width: &quot;50%&quot;, margin: &quot;0 auto&quot;, textAlign: &quot;right&quot; }}&gt; 
    &lt;Countdown /&gt;
    &lt;h3&gt;&#39;til &#128564;&lt;/h3&gt;
&lt;/div&gt;

When attempting to align components using "margin: 0 auto" with a fixed width, you may encounter a limitation in flexibility. Specifically, using a fixed width may not adapt well to varying content sizes or screen dimensions, potentially leading to layout issues or content clipping.

Considering this challenge, here are two alternative methods to align your components

1. Using Flex Layout

Flexbox provides a flexible solution, allowing you to center the container while keeping the content right-aligned.

&lt;div style={{ display: &quot;flex&quot;, justifyContent: &quot;center&quot; }}&gt;
    &lt;div style={{ textAlign: &quot;right&quot; }}&gt;
        &lt;Countdown /&gt;
        &lt;h3&gt;&#39;til &#128564;&lt;/h3&gt;
    &lt;/div&gt;
&lt;/div&gt;

2. Using CSS Grid

You can create a similar alignment using CSS Grid. With grid layout, you can easily center the content while maintaining the right alignment.

&lt;div style={{ display: &quot;grid&quot;, justifyContent: &quot;center&quot; }}&gt;
    &lt;div style={{ textAlign: &quot;right&quot; }}&gt;
        &lt;Countdown /&gt;
        &lt;h3&gt;&#39;til &#128564;&lt;/h3&gt;
    &lt;/div&gt;
&lt;/div&gt;

Both of these methods should provide a more flexible and responsive solution for aligning the components as described. For a deeper understanding of Flexbox and CSS Grid alignment, you might find this guide helpful: CSS Grid & Flexbox Component Alignment Guide.

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

发表评论

匿名网友

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

确定