【问题标题】:How can I have a textarea take up the remaining height in a div?如何让 textarea 占据 div 中的剩余高度?
【发布时间】:2012-06-15 13:01:51
【问题描述】:

我有一组如下代码。关键是要在 div 中放置一组图像,并且 div 的其余部分填充文本区域。如果我将高度设置为 100%,它将使其高度相同,这不是 (div.height - images.height) 并使文本区域更长。

它在 w3c 上说 inherit 目前不起作用,auto 只会创建一个默认文本区域,并且硬编码它永远不会完成它,因为 div 可能更大或更小。

你们都做了什么来完成这件事?

<div id = "parent">
  <div id = "images" style="background-color:#99ccff;">
    <img style = "padding:0px; border:0px;" src = "..." />
    <img style = "padding:0px; border:0px;" src = "..." />
    <img style = "padding:0px; border:0px;" src = "..." />
    <img style = "padding:0px; border:0px;" src = "..." />
    <img style = "padding:0px; border:0px;" src = "..." />
    <img style = "padding:0px; border:0px;" src = "..." />
    <img style = "padding:0px; border:0px;" src = "..." />
  </div>
  <textarea style="width:100%; height:100%; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;" >
  </textarea>
</div>

【问题讨论】:

  • #parent的高度是怎么定义的?你能制作一个jsfiddle.net 演示来显示问题吗? (图片可以使用dummyimage.com

标签: html css image styles textarea


【解决方案1】:

似乎不可能在除 chrome 之外的其他浏览器中使用此行为。

我尝试将图片和文本区域拆分为两个 CSS 表格行,以给 #images 一个高度,以便 #textarea-container 自动调整其高度。下一步是给 textarea 100% 的高度和 100% 的宽度。

不支持在相对定位的表格单元格中绝对定位元素
CSS Positioning inside of an HTML Table

所以textarea { position: absolute; left:0;right:0;bottom:0;top:0 } 在相对定位的表格单元格中不起作用。

HTML

<div id ="parent">
    <div id="images">
        <img src="http://www.dummyimage.com/100x20/cc5acc/fff.png" />
        <img src="http://www.dummyimage.com/80x20/cc5acc/fff.png" />
        <img src="http://www.dummyimage.com/20x20/cc5acc/fff.png" />
        <img src="http://www.dummyimage.com/80x20/cc5acc/fff.png" />
        <img src="http://www.dummyimage.com/100x20/cc5acc/fff.png" />
        <img src="http://www.dummyimage.com/120x20/cc5acc/fff.png" />
        <img src="http://www.dummyimage.com/50x20/cc5acc/fff.png" />
    </div>

    <div id="textarea-container">
        <textarea></textarea>    
    </div>    
</div>​

CSS

* { margin:0; padding: 0 }

#parent{
    width: 400px;
    height: 300px;
    display: table;
    background: blue;
}
#images {
    display: table-row;
    height: 0;
}

#textarea-container {
    display: table-row;
}

textarea {
    height: 100%;
    width: 100%;
}

!此解决方案依赖于display: tabledisplay: table-row 和谷歌浏览器。

现场演示(仅适用于谷歌浏览器)http://jsfiddle.net/atTK7/4/
显示表格支持http://www.quirksmode.org/css/display.html

建议使用 Javascript 解决方法,用于浏览器(Chrome 除外)。

【讨论】:

    【解决方案2】:

    我不太擅长使用它,但你不妨试试 display:box;。我有一个example here 可以做(我认为)你想要的,但是“主要错误”是你不能再重新调整 textarea 的大小(但如果这不是问题,你可以改变 resize:none;) .

    【讨论】:

    【解决方案3】:

    可能是您的浏览器有问题。我刚刚尝试了你的代码,似乎做了你想做的事:http://jsfiddle.net/Rorok_89/DMdyY/1/

    【讨论】:

    • 我相信 OP 希望 textarea 与图像位于同一行。
    • 不在同一行。我希望它填充剩余的内容....所以就像所有图像都会包裹并占据一些总高度一样,然后正如我上面的评论:我会有某种填充命令,它会占用剩余的高度并将其应用于文本区域
    【解决方案4】:

    即使我不清楚你想做什么,你也可以使用 jQuery 来帮助你获取实际元素的大小,并强制你的 textarea 扩大或缩小。这应该可以帮助您:

    <script type="text/javascript">
    var max_div_height = 1000; //The max height of your div in pixels
    var total_div_height = 0 // The total height of your divs combined
    jQuery(".container").each(function(){
        var context = $(this);
        var height = context.height();
        total_div_height+=height; //If it's width, set height otherwise
    });
    var textarea_size = max_div_height - total_div_height;
    jQuery("#myTextarea").css("height", textarea_size+"px") // Give an ID to your textarea
    </script>
    

    【讨论】:

    • OP 没有用 jQuery 或 JavaScript 标记问题。
    • 我不想为此使用 JS。我希望这只是完全与 CSS 相关的......就像某种填充命令......用 textarea 填充容器的剩余高度。
    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2011-01-02
    相关资源
    最近更新 更多