Prevent text from overflowing a padded container in HTML

一曲冷凌霜 提交于 2020-01-01 07:39:13

问题


I have this situation:

<div style="width: 100px; padding: 5px 15px 5px">Some text longer than 100px</div>

If I set overflow: hidden on the div the text will still go outside the 15px padded area on the right:

++----------------------------+------+
++----------------------------+------+
||This text should stop here -| but i|
++----------------------------+------+
++----------------------------+------+

Can this be done without putting an extra element inside to hold the text. Any solution in any browser will do, I just want to know if it's possible.


回答1:


You can use a transparent border to make the margin you want. It won't be overflowed:

div {
    white-space: nowrap;
    overflow: hidden;
    max-width: 300px;
    border-left:1em solid transparent;
    border-right:1em solid transparent;
    text-overflow: ellipsis;
}



回答2:


did you try:

word-wrap:break-word;

it automatically breaks word to next line when end is reached.




回答3:


Hey I don't know about your requirement exactly, there is one way to truncate the text using css3 ellipsis property.

Example

a) Normal case

+-------------------------++
some text which is exceeding the container
+-------------------------++

b) After implementing ellipsis

+-------------------------++
some text which is exceed...
+-------------------------++

Here is the reference link: https://developer.mozilla.org/En/CSS/Text-overflow which have the browser support information also.

Hope this helps!



来源:https://stackoverflow.com/questions/7711490/prevent-text-from-overflowing-a-padded-container-in-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!