简体   繁体   English

子元素的垂直对齐,可以扩展父容器的大小

[英]Vertical alignment of child element that can expand the size of the parent container

I am trying to create a layout as per the following, where the help text for a question is vertically aligned within the question container. 我正在尝试按照以下方式创建布局,其中问题的帮助文本在问题容器中垂直对齐。

在此输入图像描述

My problem is how to expand the parent container when the help text exceeds the height of the question controls. 我的问题是当帮助文本超出问题控件的高度时如何扩展父容器。 As per: 按照:

在此输入图像描述

I know this is because I am using absolute positioning to vertically centre the help text and therefore it is not included in the flow of the parent container. 我知道这是因为我使用绝对定位来垂直居中帮助文本,因此它不包含在父容器的流中。 However I'm not sure of the best css solution to this issue. 但是我不确定这个问题的最佳css解决方案。

position: absolute;
top: 50%;
transform: translateY(-50%);

I have created the following fiddle to illustrate my existing solution / problem: 我创建了以下小提琴来说明我现有的解决方案/问题:

jsfiddle 的jsfiddle

I would appreciate any advice on the best structure for this problem. 对于这个问题的最佳结构,我将不胜感激。

Well, you cannot expand the parent container based on the absolutely positioned div only with CSS. 好吧,你不能仅使用CSS扩展基于绝对定位的div的父容器。

If your prior issue is vertically centering, I'd suggest you a different approach. 如果您之前的问题是垂直居中,我会建议您采用不同的方法。 You could turn your container to a display: table element, and make both the main content, and the tooltip act as display: table-cell . 您可以将容器转换为display: table元素,并将两者作为主要内容,并将工具提示作为display: table-cell This way, you will be able to place it at the right side, in a more solid way, and vertical alignment will work with vertical-align: middle . 这样,您将能够以更加可靠的方式将其放置在右侧,垂直对齐将与vertical-align: middle

This will make your container fit the tooltip. 这将使您的容器适合工具提示。 Plus, I added the position:relative; left:20px; 另外,我添加了position:relative; left:20px; position:relative; left:20px; to misplace it a little to the right, as it was in your examples... 把它放错一点,就像在你的例子中一样......

 .cf:before, .cf:after { content: " "; /* 1 */ display: table; /* 2 */ } .cf:after { clear: both; } .container { position: relative; border: 1px #000 solid; display: table; margin-bottom: 50px; } .content, .text { display: table-cell; vertical-align: middle; } .text { width: 22.5%; } .text > div { background-color: #ccc; margin: 5px; padding: 10px; position: relative; left: 20px; } 
 <div class="container cf"> <div class="content"> This is the form content </div> <div class="text"> <div>Some text that is smaller than the control height</div> </div> </div> <div class="container cf"> <div class="content"> This is the form content </div> <div class="text"> <div>Some text that is bigger than the control height, some text that is bigger than the control height, some text that is bigger than the control height. </div> </div> </div> 

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

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