Reputation: 1578
I'd like to know if there's a way to keep a text inside a div if the text exceeds the width
the ´div´'s height is stretched until all the text is fitted.
UPDATE:
Thank y'all for helping +1, so should I use ´Inherit´ or ´min-height´?
A plain div
:
Then I added a word-wrap: break-word;
, and I've got this:
The Ideal:
The div
's height
should be increased in order to fit the whole text
Thanks in advance.
div {
width: 50px;
height: 50px;
border: 1px solid red;
display: inline-block;
word-wrap: break-word;
}
<!DOCTYPE html>
<html>
<head>
<title>DIV</title>
</head>
<body>
<div>asjdkljaddddsadsssssssdfsdfsdfsf</div>
</body>
</html>
Upvotes: 1
Views: 359
Reputation: 6797
I've been fighting this issue for a long time. Here is one of pure-CSS ways to get that to work.
My example allows you to have a responsive(optionally) block of text, with a customize amount of displayed lines.
// Usually you want to have that everywhere.
* {
word-wrap: break-word;
}
div {
/* height / desired number of lines, must be higher than font-size */
line-height: 16.66px;
font-size: 14px;
width: 50px;
height: 50px;
border: 1px solid red;
overflow: hidden;
}
<div class="text">Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the
industry's standard dummy text ever since the 1500s</div>
Upvotes: 1
Reputation: 4100
Change height
to min-height
in your CSS:
The min-height
property is used to set the minimum height of an element.
This prevents the value of the height property from becoming smaller than min-height
.
div {
width: 50px;
min-height: 50px;
border: 1px solid red;
display: inline-block;
word-wrap: break-word;
}
<!DOCTYPE html>
<html>
<head>
<title>DIV</title>
</head>
<body>
<div>asjdkljaddddsadsssssssdfsdfsdfsf</div>
</body>
</html>
Upvotes: 3
Reputation: 758
If you want to set the div's height initially, then use:
div {
width: 50px;
min-height: 50px;
border: 1px solid red;
display: inline-block;
word-wrap: break-word;
}
This will ensure it is at least 50px high, but will still expand when you put more content in it.
Upvotes: 1
Reputation: 571
Remove the fixed height
div {
width: 50px;
border: 1px solid red;
display: inline-block;
word-wrap: break-word;
}
<!DOCTYPE html>
<html>
<head>
<title>DIV</title>
</head>
<body>
<div>asjdkljaddddsadsssssssdfsdfsdfsfqwqweqewqqweqweeqewqweeqw</div>
</body>
</html>
Upvotes: 1
Reputation: 142
div {
width: 50px;
height: 100%;
border: 1px solid red;
display: inline-block;
word-wrap: break-word;
}
<!DOCTYPE html>
<html>
<head>
<title>DIV</title>
</head>
<body>
<div>asjdkljaddddsadsssssssdfsdfsdfsf</div>
</body>
</html>
Upvotes: 1
Reputation: 337560
If you change height: 50px
to min-height: 50px
the div's height will grow to fit it's content. Similarly, you can set a limit on the allowed height using max-height
.
div {
width: 50px;
min-height: 50px;
border: 1px solid red;
display: inline-block;
word-wrap: break-word;
}
<div>asjdkljaddddsadsssssssdfsdfsdfsf</div>
Upvotes: 1
Reputation: 787
Try the css lines. You can check here https://jsfiddle.net/ogd5cruf/
div {
width: 50px;
height: auto;
border: 1px solid red;
display: inline-block;
word-wrap: break-word;
}
Upvotes: 1