Html 如何使长文本适合小div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20565201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make the long text to fit inside a small div?
提问by sun
I have the div with a specified width but the text inside it are not breaking down and fitting into the div accordingly.
我有指定宽度的 div,但其中的文本没有分解并相应地适合 div。
This might be a wrong question. How to make it fit inside the div?
这可能是一个错误的问题。如何使它适合在 div 内?
I believe its not possible for fit completely inside at least can it be fitted inside the div according to the width not height.
我相信它不可能完全安装在里面,至少它可以根据宽度而不是高度安装在 div 内。
Css
css
.limit{
width:50px;
}
HTML
HTML
<div class="limit">
<p>fasfhfhsjdhkjhdkjhfjkhdsfjkhdfjhdfiuhadfhjdfhjadskf kjahsdjfahdfuahsdf dhsf</p>
</div>
回答by Mr. Alien
All you need is word-wrap: break-word;
所有你需要的是 word-wrap: break-word;
.limit{
width:50px;
word-wrap: break-word;
}
<div class="limit">
<p>fasfhfhsjdhkjhdkjhfjkhdsfjkhdfjhdfiuhadfhjdfhjadskf kjahsdjfahdfuahsdf dhsf</p>
</div>
On the other hand, if you are not looking to break the sentence, you can also use overflow
property set to auto
or overflow-x: scroll;
- Demo
另一方面,如果你不想打破句子,你也可以使用overflow
属性设置为auto
或overflow-x: scroll;
- Demo
回答by Ricardo BRGWeb
If you are looking for a way to resize the div's font-size to fit the entire text without word break or scroll, you should use JavaScript to detect if the text is overflowing and adjust font-size accordly:
如果您正在寻找一种方法来调整 div 的字体大小以适应整个文本而无需分词或滚动,您应该使用 JavaScript 来检测文本是否溢出并相应地调整字体大小:
function fitText(outputSelector){
// max font size in pixels
const maxFontSize = 50;
// get the DOM output element by its selector
let outputDiv = document.getElementById(outputSelector);
// get element's width
let width = outputDiv.clientWidth;
// get content's width
let contentWidth = outputDiv.scrollWidth;
// get fontSize
let fontSize = parseInt(window.getComputedStyle(outputDiv, null).getPropertyValue('font-size'),10);
// if content's width is bigger then elements width - overflow
if (contentWidth > width){
fontSize = Math.ceil(fontSize * width/contentWidth,10);
fontSize = fontSize > maxFontSize ? fontSize = maxFontSize : fontSize - 1;
outputDiv.style.fontSize = fontSize+'px';
}else{
// content is smaller then width... let's resize in 1 px until it fits
while (contentWidth === width && fontSize < maxFontSize){
fontSize = Math.ceil(fontSize) + 1;
fontSize = fontSize > maxFontSize ? fontSize = maxFontSize : fontSize;
outputDiv.style.fontSize = fontSize+'px';
// update widths
width = outputDiv.clientWidth;
contentWidth = outputDiv.scrollWidth;
if (contentWidth > width){
outputDiv.style.fontSize = fontSize-1+'px';
}
}
}
}
This code is part of a test that I have uploaded to Github https://github.com/ricardobrg/fitText/
此代码是我上传到 Github https://github.com/ricardobrg/fitText/的测试的一部分
回答by Omar bakhsh
you can style it like this it's very easy way look to number 4is Scrooled
你可以这样设计它非常简单的方法看起来数字 4是Scrooled
#ol_list li {
width: 380px;
height: 80px;
background-color: silver;
justify-content: center;
align-items: center;
text-align: center;
margin-top: 0.5cm;
border: 2px solid purple;
border-radius: 12%;
}
#ol_list li span {
display: block;
max-width: 370px;
max-height: 20px;
overflow-x:hidden;
}
<ol id="ol_list">
<li><span>text1</span></li>
<li><span>text2</span></li>
<li><span>text3</span></li>
<li><span>scrooled ghghghfghfjhfghghhfghhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhghghghfghfjhfghghhfghhfghfghfghfghfghfghfghfhhhfghfghfghfghfghfghfghfhhh</span></li>
<li><span>text5</span></li>
</ol>
回答by Akhil
Modified the class limit. You can show the entire text in a single line.
修改了等级限制。您可以在一行中显示整个文本。
CSS
CSS
.limit p {
overflow-x: scroll;
white-space: nowrap;
width: 50px;
}