Setting a max character length in CSS

Sharif1111 picture Sharif1111 · Nov 17, 2014 · Viewed 477.5k times · Source

I am making responsive website for school and my question is:

How do I set a max character length of the sentences (with CSS) on my website (like 75 characters) that when I have a very large screen, the sentences wont go further than 75 characters.

I have tried a max width but that messes up my layout. I am using flexbox and media queries to make it responsive.

Answer

Vangel Tzo picture Vangel Tzo · Nov 17, 2014

You could always use a truncate method by setting a max-width and overflow ellipsis like this

p {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

An example: http://jsfiddle.net/3czeyznf/

For a multi line truncation have a look at a flex solution. An example with truncation on 3 rows.

p {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

An example: https://codepen.io/srekoble/pen/EgmyxV