简体   繁体   English

内联块div的水平对齐

[英]Horizontal align of inline-block divs

I have a series of divs inline-block displayed (and with left-right margin of 5%), as you know, once the limit of the first row of divs is reached, the rest of the divs automatically go to a second row and so on (you know, instead of overflowing the parent). 我知道显示了一系列div内联代码块(左右边距为5%),正如您所知,一旦达到第一行div的限制,其余的div就会自动转到第二行,以此类推(您知道,而不是溢出父对象)。 However this behavior has a small detail, the elements from the second row are not horizontally aligned with the ones from the first row. 但是,此行为的细节很小,第二行的元素与第一行的元素未水平对齐。 I know this is caused because of the margins, still i've got no idea on how to make all elements equidistant. 我知道这是由于边距引起的,但我仍然不知道如何使所有元素等距。 The following code represents the architecture of the section. 以下代码表示该部分的体系结构。

 .parent_div { width: 75%; float: right; /* The "parent of the parent" has its clearfix class*/ } .child_div { margin-left: 5%; margin-right: 5%; display: inline-block; } h4 { display: inline-block; } p { display: inline-block; } } 
 <div class="parent_div"> <div class="child_div"> <h4>Some text: </h4> <p>some info.</p> </div> <div class="child_div"> <h4>Some text: </h4> <p>some info.</p> </div> <div class="child_div"> <h4>Some text: </h4> <p>some info.</p> </div> <div class="child_div"> <h4>Some text: </h4> <p>some info.</p> </div> <div class="child_div"> <h4>Some text: </h4> <p>some info.</p> </div> </div> 

You can see here again the structure of the divs, and also you can observe that the divs from below are not aligned with the ones from above. 您可以在此处再次看到div的结构,还可以观察到下面的div与上面的div不对齐。

https://imgur.com/a/GLgFSsV https://imgur.com/a/GLgFSsV

It is also because of the margin, but mostly because of the different length of the content in each div. 这也是由于页边距,但主要是因为每个div中内容的长度不同。 The best way I could think of achieving what you want is by using grid. 我想实现所需目标的最佳方法是使用网格。

 .parent_div { width: 75%; float: right; display: grid; grid-gap: 2%; grid-template-columns: repeat(auto-fill, minmax(300px, max-content)); } .parent_div .child_div h4 { display: inline-block; } .parent_div .child_div p { display: inline-block; } 
 <div class="parent_div"> <div class="child_div"> <h4>Some text111: </h4> <p>some info 3234r32.</p> </div> <div class="child_div"> <h4>Some text22: </h4> <p>some info 34.</p> </div> <div class="child_div"> <h4>Some text 232434: </h4> <p>some infods 33.asr23</p> </div> <div class="child_div"> <h4>Some text 3243: </h4> <p>some infodsf s34.</p> </div> <div class="child_div"> <h4>Some text33: </h4> <p>some infodfc fdsf342.</p> </div> <div class="child_div"> <h4>Some text22: </h4> <p>some info 34.</p> </div> <div class="child_div"> <h4>Some text 3243 234234: </h4> <p>some infodsf s34.sd</p> </div> <div class="child_div"> <h4>Some text33: </h4> <p>some infodfc fdsf342.</p> </div> <div class="child_div"> <h4>Some text22: </h4> <p>some info 34.</p> </div> </div> 

A simple way to amend this would be to set a min-width, so that the columns are at least x% wide - in the snippet, the divs show two at a time on rows which are 75% of the total width, and each child is minimum 35% wide. 一种简单的修改方法是设置最小宽度,以使列的宽度至少x%-在摘要中,div一次在行上显示两个,占总宽度的75%,每个孩子的最小宽度是35%。 This way the children are correctly aligned and are of same width. 这样,子项将正确对齐并具有相同的宽度。

 .parent_div { width: 75%; float:right; /* The "parent of the parent" has its clearfix class*/ } .child_div { margin: 0 5%; display: inline-block; min-width:35%; } h4, p { display: inline-block; } 
 <div class="parent_div"> <div class="child_div"> <h4>Some text: How on earth? </h4> <p>Dunno... seriously</p> </div> <div class="child_div"> <h4>Some text: Where?</h4> <p>There on the stair</p> </div> <div class="child_div"> <h4>Some text: my name </h4> <p>Rachel.</p> </div> <div class="child_div"> <h4>Some text:Hi there </h4> <p>Nice day</p> </div> <div class="child_div"> <h4>Some text: hello </h4> <p>it's August</p> </div> </div> 

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

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