2012-03-19 80 views
16

我正在寻找一个CSS的解决方案如下: -设置DIV有它的兄弟姐妹宽度

<div style="display:inline;"> 
 
    <div>The content of this div is dynamically created but will always be wider than 
 
       the below div. 
 
    </div> 
 
    <div> Need this div to have the same width as the above div. 
 
    </div> 
 
</div>

该包装div具有内嵌显示器和按预期工作,无论是孩子divs具有动态生成的内容。我需要底部的一个来获取前一个兄弟的宽度。

非常感谢任何建议提前。

+1

DIV是块元素这是他们自动采取他的父母宽度。检查这个http://jsfiddle.net/qG7b8/ – sandeep 2012-03-19 11:51:38

+0

它的确如此。两个内部div的宽度最大化以适合其父宽度。这可能不是你的意思... – 2012-03-19 11:53:59

+0

感谢您的意见。 我给了包装div一个显示:内联属性,这意味着它需要它的最宽的孩子的宽度。 现在我需要的是第二个子div取第一个子div的宽度。 – user1278456 2012-03-19 11:58:11

回答

-1

不知道如果我明白你正在尝试做的,但看起来像一个100%的宽度设置为最后一个div应该工作:

<div style="width:100%;"> 

BTW,第一个div风格并不明确,你应该在属性定义中使用冒号,而不是等号:

<div style="display:inline;"> 
+0

有没有必要定义宽度:100%,因为div是一个块元素,它的自动宽度为 – sandeep 2012-03-19 11:58:30

+0

这迫使包装div占用整个屏幕,而不是将子div设置为包装div的100%... 是的;)很好发现。 – user1278456 2012-03-19 12:00:35

+0

在花费了大量时间处理CSS之后,我决定腾出一些时间并使用表格。 半分钟=工作完成。 http://jsfiddle.net/XdLku/1/ 现在并不是所有的尖叫和呼喊立即!感谢您的贡献。 – user1278456 2012-03-19 14:56:30

4

设置你的div来代替display:inline-block,这样一来你的DIV将它里面的内容展开。

http://jsfiddle.net/CpKDX/

+2

http://jsfiddle.net/CpKDX/5/ 在上面的例子中,我希望第二个子div取第一个子div的宽度,即它是以前的兄弟。 – user1278456 2012-03-19 12:57:02

+0

@ user1278456你可以将你的容器div设置为'display:inline-block'而不是'display:inline',你可以得到这些结果,http://jsfiddle.net/CpKDX/6/ – 2012-03-19 13:06:14

+0

这不是给我的预期的结果。我想要的是底部div取顶部div的高度,它不在上面的小提琴中。 – user1278456 2012-03-19 13:15:37

0

UPDATE:这适用于我,我刚刚试了一下:

<div style="max-width:980px;border:1px solid red;"> 
    <div style="background:#EEE;float:left;"> 
    <div style="width:auto;border:1px solid blue;float:left;">If you use 100% here, it will fit to the width of the mother div automatically.</div> 
    <div style="border:1px solid green;"> The div will be 100% of the mother div too.</div> 
    </div> 
    <div style="clear:both;"></div> 
</div> 

这是你想要的吗?边框和背景都只是为了显示的div)


只要是这样的:

比方说,你想整个的div是最大。 980px(否则只留下了这一点或100%替代)...

<div style="max-width:980px;"> 
    <div style="width:100%;">If you use 100% here, it will fit to the width of the mother div automatically. 
    </div> 
    <div style="width:100%;"> The div will be 100% of the mother div too. 
    </div> 
</div> 

第二个选择是,用一个多格...或者你使用style="width:auto;"的动态格...

+0

啊,我恐怕这也不会产生预期的效果。 – user1278456 2012-03-19 14:37:19

+0

,因为它是母亲的div的100%,对吧?如果它不是你想要的,使用宽度:自动为这两个内部div ..这是你想要的工作吗? – Chris 2012-03-19 15:06:51

+0

@ user1278456:我刚刚更新了它,它适用于我...请让我知道如果它是你在找什么:) – Chris 2012-03-19 15:29:20

10

浮游物和表是如此2000年和晚。在今天的浏览器中,我们可以让两个兄弟DIV匹配彼此的宽度,而不管哪个更大/更小。

这里有一个Flexbox的解决方案适用于2016年

.wrapper { 
 
    display: inline-block; 
 
} 
 

 
.parent { 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
/* For visualization */ 
 
.child { 
 
    border: 1px solid #0EA2E8; 
 
    margin: 2px; 
 
    padding: 1px 5px; 
 
}
<div class="wrapper"> 
 
    <div class="parent"> 
 
    <div class="child">Child number one</div> 
 
    <div class="child">Child #2</div> 
 
    </div> 
 
</div>

+0

但是,如果你不想让第二个孩子决定宽度(它是比第一个孩子长),这是行不通的。第一个孩子会像第二个孩子一样宽。 – Skitterm 2016-11-10 18:58:21

+2

@Skitterm真的,但问题是:*“我需要底部的一个来获取前一个兄弟的宽度”*。 – pilau 2016-11-12 19:25:10

+0

@ pilau是的。正如skitterm所说,如果底部的孩子比以前的兄弟姐妹拥有更大的内容,这种解决方案将无法正常工作。你懂。如果没有写'Child#2'。这可能是一种罕见的情况,但是您知道,在真实世界的场景中,您不会在内容中编写很多Child#2。我的意思是不那么明显? – Toskan 2017-07-26 15:44:17

0

如果你愿意放弃一对夫妇的<div>当时的我对你的解决方案:

<div style=“display: inline-block;”> 
<table> 
<tr> 
<td>The table automatically makes its siblings the same width</td> 
</tr> 
<tr> 
<td>So this will be as wide</td> 
</tr> 
</table> 
</div> 

记得设置div display:inline-block;

3

这是另一个Flexbox解决方案,它允许第二个孩子包裹以匹配可变高度兄弟的宽度。

.wrapper > div { 
 
    border: 1px solid; 
 
} 
 

 
.child { 
 
    display: flex; 
 
} 
 

 
.child div { 
 
    flex-grow: 1; 
 
    width: 0; 
 
} 
 

 
.wrapper { 
 
    display: inline-block; 
 
}
<div class="wrapper"> 
 
    <div>This div is dynamically sized based on its content</div> 
 
    <div class="child"><div>This div will always be the same width as the preceding div, even if its content is longer (or shorter too).</div></div> 
 
</div>