简体   繁体   English

如何使嵌套的<div>扩展到其内容?

[英]How do I make a nested <div> expand to its contents?

I am using this system to try to implement a sliding window selector. 我正在使用这个系统来尝试实现一个滑动窗口选择器。
I have a <div> that is contained in another <div> . 我有一个<div>包含在另一个<div> The outer <div> has a fixed size and the inner one should expand to contain its contents. 外部<div>具有固定大小,内部应扩展以包含其内容。

<div style="width: 25px; overflow: hidden">
    <div id="middle">
        <div style="width: 50px"></div>
    </div>
</div>

The outer <div> has a fixed size. 外部<div>具有固定大小。
The middle <div> should expand to match the size of the inner <div> (the one that has width 50px) middle <div>应该扩展以匹配内部<div> (宽度为50px的那个)的大小
How, using CSS or JavaScript can I ensure that the middle <div> is as wide as the inner <div> , but still gets cut off by the outer <div> ? 如何,使用CSS或JavaScript,我可以确保中间<div>与内部<div>一样宽,但仍然被外部<div>切断?

I have tried to use JQuery to get the length of the inner <div> and then dynamically set the width of the middle <div> , but this does not consistently get the right length. 我试图使用JQuery来获取内部<div>的长度,然后动态设置中间<div>的宽度,但这并不总是得到正确的长度。

Div elements, by default, try to fit their container. 默认情况下,Div元素会尝试适合其容器。 so the middle one will try to fit its container which is the outer div.. it is not affected by content. 所以中间人将尝试适应其外部div的容器..它不受内容的影响。

If you set the middle one to be display:inline-block you make it fit the contents instead of the container it that fixes the issue.. 如果你设置中间的一个display:inline-block你使它适合内容而不是修复问题的容器..

Can you make inner div float? 你能让内部div漂浮吗? That way it should display full width of the span but without editing outer div width to expand the content longer than outer div width will be invisible. 这样它应该显示跨度的全宽,但不编辑外部div宽度以扩展内容长于外部div宽度将是不可见的。

divs are block elements, so your inner div will naturally expand to the width of the containing div. div是块元素,因此你的内部div自然会扩展到包含div的宽度。 You can ensure this by setting the style attribute of the inner div to 100%. 您可以通过将内部div的style属性设置为100%来确保这一点。 You should also set its overflow CSS property to "hidden." 您还应将其溢出CSS属性设置为“隐藏”。

您可以获取任何HTML对象的内容宽度,如下所示:

document.getElementById("myDIV").clientWidth

Use display: inline-block and max-width: ?px on middle . middle使用display: inline-blockmax-width: ?px You'll want to put overflow-x: hidden on middle (not outer like in your code), but I left it off in the demo so you could see the width working. 你需要将overflow-x: hiddenmiddle (不像代码中的outer ),但是我在演示中将其保留下来,这样你就可以看到宽度正常工作了。

Demo: http://jsfiddle.net/ThinkingStiff/hHDQS/ 演示: http//jsfiddle.net/ThinkingStiff/hHDQS/

HTML: HTML:

<div class="outer">
    <div class="middle">
        <div id="inner1"></div>
    </div>
</div>

<div class="outer">
    <div class="middle">
        <div id="inner2"></div>
    </div>
</div>

CSS: CSS:

.outer {    
    border: 1px solid green;
    height: 100px;
    width: 300px;
}

.middle {
    border: 1px solid red;
    display: inline-block;
    height: 75px;
    max-width: 300px;
}

#inner1 {
    border: 1px solid blue;
    height: 50px;
    width: 50px;
}

#inner2 {
    border: 1px solid blue;
    height: 50px;
    width: 350px;
}

Output: 输出:

在此输入图像描述

If the inner div can be absolutely positioned the following will stretch it to fill up the parent completely (width and height): 如果内部div可以绝对定位,则以下将拉伸它以完全填充父级(宽度和高度):

#myInner{
  position:absolute;
  left:0;
  right:0;
  top:0;
  bottom:0;
  width:auto;
  height:auto;
}

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

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