Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent text from overflowing a padded container in HTML

Tags:

html

css

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.

like image 954
disc0dancer Avatar asked Oct 10 '11 10:10

disc0dancer


1 Answers

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;
}
like image 116
Jokesterfr Avatar answered Sep 20 '22 17:09

Jokesterfr