Last updated: Apr 5, 2024
Reading time·5 min
To set max character length in CSS:
max-width
CSS property on the element to N ch
.overflow
CSS property to hidden
.text-overflow
to ellipsis
to display an ellipsis.white-space
to nowrap
to collapse the whitespace and suppress line
breaks.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> p { max-width: 15ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } </style> </head> <body> <p>bobbyhadz.com</p> <p>This is a very long sentence.</p> </body> </html>
The example sets the maximum character length of the p
element to ~15
characters.
The
ch
relative unit represents the advance measure of the glyph 0
in the element's
font.
<style> p { max-width: 15ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } </style>
If it is impossible to determine the measure of the 0
glyph, it is assumed to
be 0.5em
wide by 1em
tall.
We also set the
overflow property
to hidden
.
When overflow
is set to hidden
, the overflowing content is clipped at the
element's padding box and the clipped content is not visible.
The
text-overflow
property can be set to ellipsis
to display an ellipsis ...
that represents
the clipped text.
If you don't want to display an ellipsis, remove the property.
<style> p { max-width: 15ch; overflow: hidden; white-space: nowrap; } </style>
When the
white-space CSS
property is set to nowrap
:
max-width
property to N pixelsYou can also set the max-width
of the element to N pixels to truncate the
text.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> p { max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } </style> </head> <body> <p>bobbyhadz.com</p> <p>This is a very long sentence.</p> </body> </html>
The example sets the max-width
CSS property on all p
elements to 100px
.
Note that the width of the ellipsis is also included in the 100px
.
If you want to remove the ellipsis, you can remove the text-overflow
property,
however, that wouldn't look good because a letter might be cut off.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> body { margin: 100px; } p { max-width: 100px; overflow: hidden; white-space: nowrap; } </style> </head> <body> <p>bobbyhadz.com</p> <p>This is a very long sentence.</p> </body> </html>
You can also use CSS grid to set a max character length in CSS.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .container { display: grid; grid-template-columns: 1fr 15ch 1fr; } .paragraph { grid-column: 2/3; } </style> </head> <body> <div class="container"> <p class="paragraph">bobby hadz com abc 123 xyz.</p> <p class="paragraph">This is a very long sentence.</p> </div> </body> </html>
We wrap the p
tags in a div
and set the element's
display property to
grid.
The grid
value enables us to align elements into columns and rows.
The grid-template-columns CSS property can be used to define the track sizing of the grid columns.
In this case, we have 3 columns where the middle column has a maximum width of 15 characters.
We then set the
grid-column CSS
property on the p
elements.
The property specifies the grid item's size and location within a grid column.
A similar solution would be to simply set the max-width
CSS property on the
p
elements to N characters.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .paragraph { max-width: 10ch; } </style> </head> <body> <p class="paragraph">bobby hadz com abc 123 xyz.</p> <p class="paragraph">This is a very long sentence.</p> </body> </html>
N characters are displayed and then the text is wrapped to the next line.
You can also set the overflow-wrap
property to anywhere
to achieve a similar
result.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .paragraph { max-width: 100px; word-break: normal; overflow-wrap: anywhere; } </style> </head> <body> <p class="paragraph">bobby hadz com abc 123 xyz.</p> <p class="paragraph">This is a very long sentence.</p> </body> </html>
When the
word-break CSS
property is set to normal
, then the default line break rule is used.
When
overflow-wrap
is set to anywhere
, unbreakable string of characters like long words or a URL
may be broken at any point if there are no acceptable break points in the line.
Notice that the characters are still broken up nicely.
There is also a break-all
value of the word-break
property.
The break-all
value inserts word breaks between any two characters to prevent
overflow.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> .paragraph { max-width: 100px; word-break: break-all; } </style> </head> <body> <p class="paragraph">bobby hadz com abc 123 xyz.</p> <p class="paragraph">This is a very long sentence.</p> </body> </html>
However, notice that this makes the text a bit difficult to read.
I've also written an article on how to limit text length to N lines using CSS.
You can learn more about the related topics by checking out the following tutorials: