css - 流畅地垂直扩展 DIV 或 TEXTAREA

标签 css html textarea fluid-layout

我希望文本区域在不与任何其他视觉元素重叠的情况下占据尽可能多的垂直空间。显然,不同的屏幕/设备具有不同的高度,因此我需要流畅的解决方案(我认为这是正确的术语)。

我看过的其他问题不涉及文本区域,而是使用内容已经确定的(子)DIV。我不需要 textarea 动态扩展以适应它的内容,我只希望它尽可能高但不遮挡任何其他元素。

我已经收集了类似问题的部分答案,但不能完全解决问题: http://jsfiddle.net/wa5zU/

CSS:

body, html {
    height:100%
}
p {
    text-align:justify
}
textarea {
    resize:vertical;
    height:auto;
    width:100%;
    box-sizing:border-box;
    -webkit-box-sizing:border-box;
    -moz-box-sizing:border-box;
}
.vexpand {
    border:1px solid blue
}
.vexpand {
    position:absolute;
    bottom:0;
    width:90%;
    height:auto
}

HTML:

<h2>Some content of variable length / height to fill the top portion of the screen</h2>

<p>Either: 1) make the blue-bordered DIV expand fluidly to fill this gap or 2) make the textarea expand to achieve the same effect</p>
<div class="vexpand">
    <div>One line of content related to the textarea that must be kept with the textarea</div>
    <textarea rows=5 cols=10>I have heard that textareas need valid rows and cols attributes in order to respond correctly to height and width css</textarea>
</div>

本次尝试基于position:absolutebottom:0假设DIV可以向上扩展。我这样做是因为 DIV/TEXTAREA 上方的内容是可变的,所以无法找到一种优雅而稳健的方式来从 top 开始测量。

有一行与textarea相关的内容必须与textarea保持在一起,因此将此内容和textarea封装在一个div中。理想情况下,我希望该内容位于文本区域上方。

我在相关问题中尝试/看到的事情:

  • position:absolute 和冲突的绝对位置
  • bodyhtmlheight设置为100%,以便CSS进行计算
  • 在包装器 div .vexpandtextarea 上使用 height:autoheight:100%/li>
  • textarea 上设置 colsrows 属性,使其响应 height宽度

最佳答案

这是你想做的吗?

http://jsfiddle.net/wa5zU/2/

body, html {
    height:100%;
    margin: 0;
    padding: 0;
}
p {
    text-align:justify
}
*
{
   box-sizing:border-box;
    -webkit-box-sizing:border-box;
    -moz-box-sizing:border-box;   
}

textarea {
    resize:vertical;
    height: calc(100% - 20px) ;
    width:100%;      
}

.text-content
{
    height: 80%;
    overflow: auto;
    padding: 10px;
}

.editor
{
  height: 20%; 
  overflow: hidden;
  padding: 10px;
}

关于css - 流畅地垂直扩展 DIV 或 TEXTAREA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21784662/

相关文章:

css - 强制 Bootstrap 的折叠导航 100% 高度

javascript - 到达在 javascript 中动态设置的 URL

java - 对象数组到文本区域

javascript - 文本区域有神秘的空白

css - 将外部字体应用于 CSS 时遇到问题

css - 缩放html页面时的滚动条

javascript - 为什么 js 数组的值会覆盖自己?

javascript - video.js - 在播放期间找到搜索的开始时间

javascript - 在没有焦点的情况下替换文本区域内的文本

CSS:如何在一条线上对齐不同大小的图像?