简体   繁体   English

使左div的高度变紧而不会影响父级中的其他div

[英]Make left div height tight without affecting other div in parent

Was unsure on how to name this one, so apologies if it's confusing. 不确定如何命名此名称,因此感到困惑时深表歉意。 Basically, I'm creating a tabbed module where the .right div is always the same height. 基本上,我正在创建一个选项卡式模块,其中.right div总是相同的高度。

The .left div houses the links/tabs, but I want this div's height not to match the .right ones. .left div包含链接/选项卡,但我希望此div的高度与.right的高度不匹配。

For example, in the below approach, I want the div with the text "Content" to be auto (match the text height). 例如,在下面的方法中,我希望具有文本“ Content”的div是自动的(与文本高度匹配)。

Approach: 方法:

 .parent { display: flex; flex-direction: column; } .flex-row{ display: flex; flex-direction: row; } .left, .right { flex-basis: 50%; } .left { border: 1px solid blue; } .right { background: red; min-height: 200px; width: 200px; } 
 <div class="parent"> <div class="flex-row"> <div class="left">Content</div> <div class="right"></div> </div> <div class="flex-row"> <div class="left">Content 2</div> </div> </div> 

So something like this: 所以像这样:

在此处输入图片说明

I've achieved the above by just doing <div class="left">Content<br>Content 2</div> for demo purposes. 我仅出于演示目的通过执行<div class="left">Content<br>Content 2</div>达到了上述目的。

Use left as a flex container, and add two children to it. left用作flex容器,并向其添加两个子代。 Then, set first child's height property to 100%. 然后,将第一个孩子的height属性设置为100%。 Like this: 像这样:

 .parent { display: flex; flex-direction: column; } .flex-row { display: flex; flex-direction: row; } .left, .right { flex-basis: 50%; } .left { border: 1px solid blue; display: flex; flex-direction: column; } .row1 { height: 100%; } .row2 { background-color: lightblue; } .right { background: red; min-height: 200px; width: 200px; } 
 <div class="parent"> <div class="flex-row"> <div class="left"> <div class="row1"> Content </div> <div class="row2"> Content2 </div> </div> <div class="right"></div> </div> </div> 

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

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