如何修改我的CSS以确保文本区域的高度始终与其div容器的高度匹配?

huangapple go评论86阅读模式
英文:

How to alter my CSS to ensure that the textarea height always matches the height of its div container?

问题

我有一个位于 div 内部的文本区域,我希望文本区域的高度与 div 容器的高度匹配。

目前,宽度完美适配,但文本框(一开始为空)只占据了 div 高度的约 20%。

而当一个 ajax 调用抓取一些文本并填充文本区域时,文本区域的高度仍然保持在 div 高度的约 20% 并提供滚动条。

如何修改我的 CSS 以确保文本区域的高度始终与其 div 容器的高度匹配?

#answerBox {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin: 5px auto;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
}

#answerBoxDiv {
  width: 90%;
  min-height: 50%;
  margin: 5px auto;
  border: 4px solid #00bfb6;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
}
<div id="answerBoxDiv">
  <textarea id="answerBox"></textarea>
</div>

请注意,上述 CSS 样式和 HTML 结构没有直接解决文本区域高度与 div 容器高度匹配的问题。您需要进一步的 CSS 或 JavaScript 来实现这个目标。

英文:

I have a textarea inside a div, and I wish for the text area height to match the height of the div container.

Currently, the width fits perfectly, however the textbox (which begins empty) only fills about 20% of the div height.

And when an ajax call grabs some text and populates the textarea, the textarea height remains at about 20% of the div and provides a scroll bar.

How can I alter my CSS to ensure that the textarea height always matches the height of its div container?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#answerBox {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin: 5px auto;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
}

#answerBoxDiv {
  width: 90%;
  min-height: 50%;
  margin: 5px auto;
  border: 4px solid #00bfb6;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
}

<!-- language: lang-html -->

&lt;div id=&quot;answerBoxDiv&quot;&gt;
  &lt;textarea id=&quot;answerBox&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

以下是翻译好的部分:

  1. You need to explicitly set a height of the parent container (whether that is in px or rem etc) - that way the height: 100% of the textarea will fill to the container.

    • 你需要明确设置父容器的高度(无论是以像素还是rem等为单位) - 这样文本区域的高度:100% 将填充到容器中。
  2. The expand on text content cannot be done with simple css - you need js to determine the height of the content and adjust the parent container accordingly.

    • 无法使用简单的CSS来扩展文本内容 - 您需要使用JavaScript来确定内容的高度并相应地调整父容器。
  3. The only way you can keep height: 100% on the parent container is if its ancestor has a height set (eg 100vh) - that way the browser can determine the height of each DOM element and size the text area accordingly.

    • 唯一的方法是如果父容器的祖先元素设置了高度(例如100vh),您才能保持父容器的高度为100% - 这样浏览器可以确定每个DOM元素的高度并相应地调整文本区域的大小。
  4. UPDATE - I have added a js function to automatically increase the height of the parent container on the input. (the textarea auto-increases in height since it is 100% of the parent. This will need massaging - but when you type into the textarea the height will auto-expand.

    • 更新 - 我已添加了一个JavaScript函数,以在输入时自动增加父容器的高度(文本区域的高度会自动增加,因为它占父容器的100%)。这需要进一步调整 - 但当您在文本区域中输入时,高度会自动展开。
英文:

You need to explicitly set a height of the parent container (whether that is in px or rem etc) - that way the height: 100% of the textarea will fill to the container.

The expand on text content cannot be done with simple css - you need js to determine the heaight of the content and adjust the parent container accordingly.

The only way tyou can keep height: 100% on the parent container is its ancestor has a height set (eg 100vh) - that way the browser can determine the height of each DOM element and size the text area accrodingly.

UPDATE - I have added a js function to automatiucally increae the height of the parent container on the input. (the textarea autoincreases in height since it is 100% of the parentThis will need massaging - but when you type into the textarea the height will auto expand.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function setHeight(element) {
  const container = document.getElementById(&quot;answerBoxDiv&quot;);
  const height = element.scrollHeight;
  if(height &gt; 100) {
    container.style.height = (element.scrollHeight)+&quot;px&quot;;
    }
}

<!-- language: lang-css -->

#answerBoxDiv {
  width: 90%;
  height: 100px; **// this is needed - but can be in px / rem / vh or other but NOT % unless its parent has its height set.**
  margin: 5px auto;
  border: 4px solid #00bfb6;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
}

#answerBox {
  width: 100%;
  height: 100%; 
  box-sizing: border-box;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
  resize: vertical;
  overflow: hidden
}

<!-- language: lang-html -->

&lt;div id=&quot;answerBoxDiv&quot;&gt;
   &lt;textarea id=&quot;answerBox&quot; oninput=&quot;setHeight(this)&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

百分比高度在具有最小高度的父元素上不起作用,您需要为父元素指定高度,或者您可以使用flex属性。

英文:

Percentage height does not work with a min-height parent, you would either need to give your parent a height or you could use flex:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html,
body {
  height: 100%;
  margin: 0;
}

#answerBox {
  flex-grow:1;
  width: 100%;
  box-sizing: border-box;
  margin: 5px auto;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
}

#answerBoxDiv {
  width: 90%;
  min-height: 50%;
  margin: 5px auto;
  border: 4px solid #00bfb6;
  padding: 5px;
  font-size: 10px;
  text-align: left;
  position: relative;
  display: flex;
  flex-direction: column;
}

<!-- language: lang-html -->

&lt;div id=&quot;answerBoxDiv&quot;&gt;
  &lt;textarea id=&quot;answerBox&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

尝试将文本区域的最小高度设置为100%或接近该数值,或尝试在文本区域上使用 object-fit: contain(或cover)

英文:

try keeping the min-height of the textarea as 100% or nearby or try using
object-fit: contain(or cover) on your text area

答案4

得分: 0

#answerBox {
height: 100%;
resize: none;
}

英文:

You can try this

#answerBox {
  height: 100%;
  resize: none;
}

答案5

得分: 0

为 #answerBoxDiv 的父级 div 指定特定的 height,以便您获得此 div 的适当高度。

<div class="parent-div" style="height: 100%;">
    <div id="answerBoxDiv">
        <textarea id="answerBox"></textarea>
    </div>
</div>
英文:

Give specific height for #answerBoxDiv parent div. So you will get appropriate height for this div.

&lt;div class=&quot;parent-div&quot; style=&quot;height: 100%;&quot;&gt;
    &lt;div id=&quot;answerBoxDiv&quot;&gt;
        &lt;textarea id=&quot;answerBox&quot;&gt;&lt;/textarea&gt;
    &lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月14日 19:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定