[英]How to create a div that will automatically word wrap the contents so that the div will never grow horizontally larger than its container?
I have this structure: 我有这个结构:
<div id="container">
<select name="Country">
<option value="1">Argentina</option>
<option value="2">Australia</option>
...
</select>
<div id="description"></div>
<input ...
<input ...
<input ...
contents ...
contents ...
</div>
The #description
div will be filled in on the fly according to the selection option. #description
div将根据选择选项即时填写。 I want so that the #description
div will automatically wordwrap, and not forcefully expand the container, if one line is not enough to hold the content of #description
. 我想让#description
div自动换行,并且如果一行不足以容纳#description
的内容,则不强制展开容器。
The catch is, I don't want to give a fixed width to #container
because I want the #container
's width to be flexible according to all the elements inside the container, except this #description
div. 问题是,我不想给#container
固定宽度,因为我希望#container
的宽度根据容器内的所有元素( 除了 #description
div 之外) #description
。 I also don't want to give fixed width to #description
either, because I want the #description
div can grow / shrink depends on the #container
. 我也不想给#description
固定宽度,因为我希望#description
div可以增长/缩小取决于#container
。
The #container
itself indeed can grow or shrink, but it only happened when the page loads, as it fills up select's options with current values, and other stuffs. #container
本身确实可以增长或收缩,但是只有在页面加载时才会发生,因为它会用当前值和其他内容填充select的选项。 But once the page finish loaded, I don't want the width of the #container
to change, hence this dilemma. 但是,一旦页面加载完成,我不希望#container
的宽度发生变化,因此出现了这一难题。 I have tried to give #description
width 100%, but it does not work too. 我尝试将#description
宽度设置为100%,但它也无法正常运行。 How can I solve this? 我该如何解决? Thanks. 谢谢。
Use word-wrap CSS property in your description block. 在描述框中使用自动换行CSS属性。 In this case you can give width in percentage allowing the description div to widen or shrink according to the width of the container. 在这种情况下,您可以按百分比给出宽度,以使描述div根据容器的宽度扩大或缩小。 You can also try out the vw
for keeping the width of container and description in control and use word-wrap in parallel. 您也可以尝试使用vw
来控制容器的宽度和说明,并并行使用自动换行。 Check out this link for more information's on CSS vw
. 查看此链接以获取有关CSS vw
更多信息。
#description{
word-wrap: break-word;
}
Update with workaround: 解决方法更新:
preset = []; info = []; preset.push ("Select "); info.push (""); preset.push ("1. Top "); info.push ("Print Ten Members who got the top results. For awarding purposes."); preset.push ("2. New "); info.push ("Print all new members of the month."); preset.push ("3. Inactive "); info.push ("Print all inactive members of the month. Including members who delete their profile."); $( document ).ready(function() { for (var i = 0; i < preset.length; i++) { $('#options').append($('<option>', { value: i, text : preset[i] })); } }); $('#options').change(function(){ var idx = $('option:selected',$(this)).index(); $('#description').text(info[idx]); });
#container { background-color:#DDDDDD; border: black solid 1px; position:absolute; padding:20px; font-family: Arial, sans-serif; width: 40%; } #options { width: 50%; max-width: 200px; min-width: 80px; } input[type=button] { width: 50px; } #description { width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <select id="options"></select> <input type="button" value="Print"/> <div id="description"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.