简体   繁体   English

定位另一个div而不破坏垂直对齐

[英]Positioning another div without breaking vertical alignment

I had encountered to an interesting CSS challenge. 我遇到了一个有趣的CSS挑战。 In the following code I was able to vertically aligned the text and input. 在下面的代码中,我能够垂直对齐文本和输入。 The part I couldn't manage was, without breaking the vertical alignment (text - input) I need to put footer text under the input. 我无法管理的部分是,不破坏垂直对齐方式(文本-输入),我需要在输入下放置页脚文本。

 .container { width: 300px; } .head { display: inline-block; vertical-align: middle; width: 20%; } .col { display: inline-block; vertical-align: middle; } .col input { width: 100px; } 
 <div class="container"> <div class="head"> Text </div> <div class="col"> <input type="text" /> </div> <div class="col"> <input type="text" /> </div> <div class="head"></div> <div class="col"> Footer Text </div> </div> <div class="container"> <div class="head"> Three Line Text </div> <div class="col"> <input type="text" /> </div> <div class="col"> <input type="text" /> </div> <div class="head"></div> <div class="col"> Footer Text </div> </div> 

Also, container div height should effect from footer text as well. 同样,容器div的高度也应受页脚文本的影响。 So using absolute will not work for this case. 因此,在这种情况下使用绝对值将不起作用。

I already aware some JavaScript or CSS hack solutions. 我已经知道一些JavaScript或CSS hack解决方案。 But for this case, I want to proceed with a proper way. 但是对于这种情况,我想采取适当的方法。 How can we achieve this properly? 我们如何正确地做到这一点?

UPDATE UPDATE

I forgot to mention before. 我忘了提。 Footer text could be multiple lines as well. 页脚文本也可以是多行。 It should cover both inputs underneath. 它应该覆盖下面的两个输入。

Flexbox can do that but it requires restructuring the HTML and altering a class or two...oh, and a pseudo-element. Flexbox可以做到这一点,但它需要重组HTML并更改一两个类……哦,还有一个伪元素。

 .container { width: 300px; display: flex; margin: 1em; border: 1px solid red; } .head { width: 20%; display: flex; flex-direction: column; justify-content: center; } .col, .second { display: flex; flex-direction: column; justify-content: center; margin: 0 .25em; } input { width: 100px; } .col:before { content: ""; height: 1.2em; /* or whatever your line-height is */ } 
 <div class="container"> <div class="head"> Text </div> <div class="col"> <input type="text" /> <div class="foot"> Footer Text </div> </div> <div class="second"> <input type="text" /> </div> </div> <div class="container"> <div class="head"> Three Line Text </div> <div class="col"> <input type="text" /> <div class="foot"> Footer Text </div> </div> <div class="second"> <input type="text" /> </div> </div> 

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

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