英文:
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.
<div style={{margin: "0 auto"}}>
<Countdown />
<h3>'til 😴</h3>
</div>
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
.
<div className='App'>
<div style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent:"right"
}}>
<div>
<h1 style={{textAlign: "right"}}>Countdown component</h1>
<h3 style={{textAlign: "right"}}>'til 😴</h3>
</div>
</div>
</div>
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
<div style={{ width: "50%", margin: "0 auto", textAlign: "right" }}>
<Countdown />
<h3>'til 😴</h3>
</div>
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.
<div style={{ display: "flex", justifyContent: "center" }}>
<div style={{ textAlign: "right" }}>
<Countdown />
<h3>'til 😴</h3>
</div>
</div>
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.
<div style={{ display: "grid", justifyContent: "center" }}>
<div style={{ textAlign: "right" }}>
<Countdown />
<h3>'til 😴</h3>
</div>
</div>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论