Limiting the number of characters per line with CSS

后端 未结 7 1152
感情败类
感情败类 2020-12-13 18:38

I have a paragraph of text:

Lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum

相关标签:
7条回答
  • 2020-12-13 19:22

    A better solution would be you use in style css, the command to break lines. Works in older versions of browsers.

    p {
    word-wrap: break-word;
    }
    
    0 讨论(0)
  • 2020-12-13 19:27

    If you use CSS to select a monospace font, the problem of varying character length is easily solved.

    0 讨论(0)
  • 2020-12-13 19:34

    The latest way to go is to use the unit 'ch' which stands for character.

    You can simply write:

    p { max-width: 75ch; }

    The only trick is that whitespaces won't be counted as characters..

    Check also this post: https://stackoverflow.com/a/26975271/4069992

    0 讨论(0)
  • 2020-12-13 19:35

    Another approach to this would put a span element with a display:block style inside the p element each time you need the content to break. It would only be useful when your p content is static.

    <p>this is a not-dynamic text and I want to put<span style="display:block">the following words in the next line</span>and these other words in a third one</p>
    

    It would output:

    This is a not-dynamic text and I want to put

    the following words in the next line

    and these others in a third one

    This allows you to change your text line-breaks in different viewports without JS.

    0 讨论(0)
  • 2020-12-13 19:42

    That's not possible with CSS, you will have to use the Javascript for that. Although you can set the width of the p to as much as 30 characters and next letters will automatically come down but again this won't be that accurate and will vary if the characters are in capital.

    0 讨论(0)
  • 2020-12-13 19:42

    You could do this:
    (Note! This is CSS3 and the browser support = good!! )

       p {
        text-overflow: ellipsis; /* will make [...] at the end */
        width: 370px; /* change to your preferences */
        white-space: nowrap; /* paragraph to one line */
        overflow:hidden; /* older browsers */
        }
    
    0 讨论(0)
提交回复
热议问题