简体   繁体   English

调整 div 宽度以适应换行后的文本

[英]Ajust div width to fit text after line-break

I would like to resize a div width to make it always fit the text inside even when there is a line-break.我想调整一个 div 宽度的大小,以使其始终适合里面的文本,即使有换行符也是如此。

Is there a way to modify the width to make it fit the text after a line break using javascript?有没有办法修改宽度以使其在使用 javascript 换行后适合文本?

 body{ width: 600px; margin: 0px auto; } #container { border: 15px solid orange; display: flex; align-items: center; justify-content: space-between; } #firstDiv{ border: 10px solid brown; width: 130px; margin: auto 20px; } #secondDiv{ border: 10px solid skyblue; display: flex; align-items: center; margin: auto 20px; } #icon{ height: 24px; width: 24px; background-color: yellow; margin-right: 10px; } #thirdDiv{ border: 5px solid yellowgreen; width: 200px; margin: auto 20px; }
 <div id="container"> <div id="firstDiv">FIRST</div> <div id="secondDiv"> <span id="icon"></span> <span id="legend">I want the #secondDiv width to fit this text</span> </div> <div id="thirdDiv">THIRD</div> </div>

Edits:编辑:

The problem is because there is some blank space at the end of the #secondDiv the space between #firstDiv and #secondDiv appears not the same than between #secondDiv and #thirDiv when there is no borders.问题是因为#secondDiv末尾有一些空白,# #firstDiv#secondDiv之间的空间看起来与#secondDiv#thirDiv之间没有边界时的空间不同。

If you instruct that element to use all of the remaining space ( width:100% ) the text will fill whatever space it can.如果您指示该元素使用所有剩余空间 ( width:100% ),则文本将尽可能填充所有空间。

 body{ width: 600px; margin: 0px auto; } #container { border: 15px solid orange; display: flex; align-items: center; justify-content: space-between; } #firstDiv{ border: 10px solid brown; width: 130px; margin: auto 20px; } #secondDiv{ border: 10px solid skyblue; display: flex; align-items: center; margin: auto 20px; width:100%; /* Instruct element to take 100% of the remaining space */ } #icon{ height: 24px; width: 24px; background-color: yellow; margin-right: 10px; } #thirdDiv{ border: 5px solid yellowgreen; width: 200px; margin: auto 20px; }
 <div id="container"> <div id="firstDiv">FIRST</div> <div id="secondDiv"> <span id="icon"></span> <span id="legend">I want the #secondDiv width to fit this text</span> </div> <div id="thirdDiv">THIRD</div> </div>

I have just changed some css code to make this three div be inline and deleted display:flex我刚刚更改了一些 css 代码,使这三个 div inline并删除了display:flex

 body{ width: 600px; margin: 0px auto; } #container { border: 15px solid orange; text-align: center; justify-content: space-between; } #firstDiv{ border: 10px solid brown; width: 130px; margin: auto 5px; display:inline-block; } #secondDiv{ border: 10px solid skyblue; text-align: center; margin: auto 5px; width:auto; display:inline-block; } #icon{ height: 24px; width: 24px; background-color: yellow; margin-right: 10px; } #thirdDiv{ border: 5px solid yellowgreen; margin: auto 5px; display:inline-block; width:auto; text-align: center; }
 <div id="container"> <div id="firstDiv">FIRST</div> <div id="secondDiv"> <span id="icon"></span> <span id="legend">I want the #secondDiv width to fit this text</span> </div> <div id="thirdDiv">THIRD</div> </div>

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

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