简体   繁体   English

如何在 flex 显示中将文本与容器 div 的右侧对齐?

[英]How to align text to right side of the container div in flex display?

can someone help me?有人能帮我吗? I want to align the "End text" to the right side of the .Container to be horizontally in the same row with the First name: John .我想将“结束文本”与.Container的右侧.Container ,以便与First name: John水平在同一行中。

(Recommend you when run my code to check in full screen,because you can't see well the whole container in a small window ) (建议您在运行我的代码时全屏检查,因为在小窗口中您无法清楚地看到整个容器)

Check the image,this is how I want to look like:检查图像,这就是我想要的样子:

在此处输入图片说明

 * { margin: 0; padding: 0; color: white; box-sizing: border-box; font-family: 'Poppins', sans-serif; font-weight: 700; } body { background-color: black; font-family: 'Poppins', sans-serif; overflow-x: hidden; height: 100%; padding: 0; margin: 0; } .Container { display: flex; align-items: center; margin: 0 auto; width: 75%; height: 250px; border-bottom: 2px solid white; } .Container span { color: white; font-size: 25px; margin-left: 30px; margin-right: 30px; } .dataContainer { font-size: 20px; display: inline-grid; line-height: 3rem; } .endtext { display: flex; justify-content: space-between; }
 <div class="Container"> <span>Data:</span> <div class="dataContainer"> <div class="endtext"> <p>First Name:  John</p>&nbsp; <p>End text</p> </div> <p>Last Name:  Williams</p> <p>Age:  23</p> </div> </div>

You're almost there.您快到了。 You have to tell certain children of a flex container to "grow" to fill in the remaining space.您必须告诉 flex 容器的某些子项“增长”以填充剩余空间。 Add flex-grow: 1 to the .dataContainer .flex-grow: 1添加到.dataContainer

Without the grow, you can see that the red .dataContainer doesn't fill the parent (blue) .Container .如果没有增长,您可以看到红色.dataContainer没有填充父(蓝色) .Container

没有 flex-grow

After adding flex-grow: 1 - it fills the parent as you would expect:添加flex-grow: 1 - 它如您所料填充父级:

在此处输入图片说明

Since you are using space between you need to make the container 100%由于您正在使用之间的空间,因此您需要使容器 100%

.dataContainer { width: 100%; }

 * { margin: 0; padding: 0; color: white; box-sizing: border-box; font-family: 'Poppins', sans-serif; font-weight: 700; } body { background-color: black; font-family: 'Poppins', sans-serif; overflow-x: hidden; height: 100%; padding: 0; margin: 0; } .Container { display: flex; align-items: center; margin: 0 auto; width: 75%; height: 250px; border-bottom: 2px solid white; } .Container span { color: white; font-size: 25px; margin-left: 30px; margin-right: 30px; } .dataContainer { font-size: 20px; display: inline-grid; line-height: 3rem; width: 100%; } .endtext { display: flex; justify-content: space-between; }
 <div class="Container"> <span>Data:</span> <div class="dataContainer"> <div class="endtext"> <p>First Name:  John</p>&nbsp; <p>End text</p> </div> <p>Last Name:  Williams</p> <p>Age:  23</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM