简体   繁体   English

html5垂直对齐3个Divs等距排列

[英]html5 vertically aligning 3 Divs even-spaced

the right Div on my html-page contains only some rows of thumnails - created dynamically in javascript - while the left Div contains 3 Sub-Divs (inner Divs) arranged vertically (also dynamically created on receiving my query-result from my webService) 我的html页上的右Div只包含一些缩略图行-在javascript中动态创建-而左Div包含3个垂直排列的子Div(内部Div)(也在从我的webService接收我的查询结果后动态创建)

In order to make it look balanced I want the left Div to automatically adjust to the same height as the right one (which holds the dynamically inserted thumbnails). 为了使其看起来平衡,我希望左侧的Div自动调整为与右侧的高度相同(该高度包含动态插入的缩略图)。

The 3 vertical aligned Sub-Divs in the left Div should behave like this: 左Div中的3个垂直对齐的Sub-Div的行为应如下所示:

Div2 with the larger Image should be vertically centered 具有较大图像的Div2应该垂直居中

Div1 with the header text should be vertically centered in the space above Div2 具有标题文本的Div1应该垂直位于Div2上方的中心位置

Div3 with the title text should be vertically centered in the space below Div2 带标题文本的Div3应该在Div2下面的空间中垂直居中

.

question: 题:

is this possible with css ? CSS有可能吗? - ie without calculation of the heights on dynamic creation in javascript -即无需在javascript中动态创建时计算高度

.

if this is possible with css then, please, give me some hints about which float, display, align, margin (and similar) settings I could try for the left Container Div and for its 3 inner Divs. 如果这可以通过CSS进行,请给我一些提示,以了解我可以为左侧Container Div及其3个内部Div尝试的浮动,显示,对齐,边距(及类似设置)设置。

I do not need to get a sample or a solution resp. 我不需要样品或解决方案。 but I need some hints in plain words, some ideas that I could try out, and some considerations (tipps) which I should obey. 但是我需要一些通俗易懂的提示,一些我可以尝试的想法以及一些我应该遵循的注意事项。

many thanks in advance. 提前谢谢了。

Flexbox can do what you need: Flexbox可以满足您的需求:
jsfiddle: https://jsfiddle.net/ornx7oar/ jsfiddle: https ://jsfiddle.net/ornx7oar/

 .main { display: flex; } .left-panel { padding: 5px; margin-right: 5px; background-color: green; /*width: 30%;*/ flex: 0 0 30%; /* don't grow or shrink based on contents, 30% width (put auto if you prefer to use the width property) */ display: flex; flex-direction: column; /* align vertically */ justify-content: space-around; /* center vertically */ } .right-panel {} .thumb {height: 70px; width: 150px; border: 1px solid; display: inline-block;} * { box-sizing: border-box; margin: 0; } 
 <div class="main"> <div class="left-panel"> <div>first</div> <div>second</div> <div>third</div> </div> <div class="right-panel"> <div class="thumb"></div> <div class="thumb"></div> <div class="thumb"></div> <div class="thumb"></div> <div class="thumb"></div> <div class="thumb"></div> <div class="thumb"></div> <div class="thumb"></div> </div> </div> 

I would add the same class to all three divs. 我将向所有三个div添加相同的类。 In your css, add margin: 10px auto; 在您的CSS中,添加margin: 10px auto; Depending on the current code you might need to add position: relative to this class as well. 根据当前代码,您可能还需要添加position: relative对于此类的position: relative

You can tweak the 10px part depending on how far apart you want them. 您可以调整10px的部分,具体取决于您希望它们分开的距离。 The auto centers the divs to its parent. 自动将div居中到其父级。

As far as changing the order look at w3 schools documentation on order 至于更改订单,请查看订单上的w3学校文档

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

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