简体   繁体   English

如何为进入中心容器的最后一个元素设置左对齐?

[英]How can I set a left align for the last element which is into a center container?

Here is my code: 这是我的代码:

 .container{ width: 50%; border: 1px solid; text-align: center; } .box{ display: inline-block; border: 1px solid red; width: 100px; height: 20px; margin: 10px; } 
 <div class="container"> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> </div> 

As you see, the last element is also center. 如您所见,最后一个元素也是居中。 While I need to keep in the same line (vertically) on left column. 虽然我需要在左列上保持同一行(垂直) So this is the expected result: 所以这是预期的结果:

在此处输入图片说明

How can I do that? 我怎样才能做到这一点?

Use float:left on your block. 在您的代码块上使用float:left

You need to add nested div and set a width on it. 您需要添加嵌套的div并在其上设置宽度。

Then your box elements will all be centered to the nested div and have your last box in the left column. 然后,您的box元素将全部位于嵌套div的中心,并在左列中显示最后一个box。

 .center { text-align: center; margin-left:auto%; margin-right:auto%; width: 100%; } .container{ margin-left:25%; margin-right:25%; width:40%; border: 1px solid; } .box{ display: inline-block; border: 1px solid red; float: left; width: 100px; height: 20px; margin: 10px; } 
 <div class="center"> <div class="container"> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> </div> </div> 

Try with floating property .And fix the container with min heigh 尝试使用浮动属性。以最小高度固定容器

 .container{ width: 50%; border: 1px solid; text-align: center; min-height:150px; } .box{ float:left; border: 1px solid red; width: 100px; height: 20px; margin: 10px; } 
 <div class="container"> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> </div> 

I added some background colours for you to see what the containers are doing. 我添加了一些背景色供您查看容器的功能。

 .container{ width: 50%; border: 1px solid; text-align: center; background-color: silver; } .box{ display: inline-block; border: 1px solid red; width: 100px; height: 20px; margin: 10px; text-align: center; } .innerContainer { width: 250px; background-color: lightgray; margin: auto; } .row { text-align: left; } 
 <div class="container" align="center"> <div class="innerContainer"> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="row"> <div class="box">sth</div> </div> </div> </div> 

Try this I think it will work fine if you want it to be responsive I will help you with mediaQuery . 试试这个,我想如果您希望它能够响应就可以了,我会为您提供mediaQuery帮助。

 .container { width: 50%; border: 1px solid; text-align: center; display: flex; flex-direction: colomn; flex-wrap: wrap; justify-content: flex-start; align-items: center; align-content: stretch; } .box { display: inline-block; border: 1px solid red; width: 100px; height: 20px; margin: 10px; } 
 <div class="container"> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> <div class="box">sth</div> </div> 

Use float:left; 使用float:left; inside box class, like: 内盒类,例如:

.box{
   display: inline-block;
   border: 1px solid red;
   width: 100px;
   height: 20px;
   margin: 10px;
   float: left;
}

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

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