Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

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.

enter image description here

My problem is how to expand the parent container when the help text exceeds the height of the question controls. As per:

enter image description here

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.

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

I have created the following fiddle to illustrate my existing solution / problem:

jsfiddle

I would appreciate any advice on the best structure for this problem.

like image 361
Chris Prior Avatar asked Sep 27 '22 06:09

Chris Prior


People also ask

What element allows vertical-align?

Note that vertical-align only applies to inline, inline-block and table-cell elements: you can't use it to vertically align block-level elements.

How do you make a child div expand with their parents?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.


1 Answers

Well, you cannot expand the parent container based on the absolutely positioned div only with CSS.

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. 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.

This will make your container fit the tooltip. Plus, I added the 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>
like image 198
LcSalazar Avatar answered Oct 07 '22 19:10

LcSalazar