简体   繁体   English

容器的宽度由第二个孩子的宽度决定

[英]Width of container decided by the width of the second child

I have a container div with structure like below: 我有一个具有以下结构的容器div

  <div class="container">
    <div class="title"> </div>
    <div class="content"></div>
    <div class="footer"></div>
  <div>

I want the width of .container depend on the width of content . 我希望.container的宽度取决于content的宽度。

I found some solution such as FIDDLE DEMO 我找到了一些解决方案,例如FIDDLE DEMO

But it's not what I want. 但这不是我想要的。 You can see if I change the order, the width still depend on the first child: FIDDLE DEMO2 您可以查看是否更改顺序,宽度仍然取决于第一个孩子: FIDDLE DEMO2

EDIT 编辑

I may add some context, the title in my program is very long, I am OK if it will be broken in several lines. 我可能会添加一些上下文,程序中的title很长,如果可以分成几行,则可以。 But if the width depends on the widest child( title ). 但是,如果宽度取决于最宽的child( title )。 there will be too much space for content . content空间太大。 I think this DEMO may be more helpful. 我认为此演示可能会更有帮助。

This is possible but only by utilising display:table and associated properties like display:table-caption : 这是可能的,但只能通过利用display:table和相关的属性,例如display:table-caption

 .title { display: table-caption; caption-side: top; background: pink; } .container { position: relative; display: table; } .content { position: relative; display: table-row; background: lightblue; } .footer { background: lightgreen; display: table-caption; caption-side: bottom; } 
 <div class="container"> <div class="title">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatibus, quam, rerum. Repellat quasi, quis voluptatum!</div> <div class="content">This section determines the overall width of the structure.</div> <div class="footer">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div> <div> 

Try this 尝试这个

 .container { position : relative ;} 
 .title { position :absolute ;} 
.container{
    display: table
}

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

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