Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Css break every 2 words

How to break every 2 words with css and make it responsive for each possible word

Example:
The community is here -> 
The community
is here

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would not suggest using CSS in this case.

Example of how you could achieve it with regex:

let text = 'The community is here';
// Regex for linebreak after each second space.
let lineBreakText = text.replace(/^((\S+\s+){1}\S+)\s+/, '$1<br>');
let element = document.getElementById('example');
element.innerHTML = lineBreakText;
<div id="example"></div>

S+ matches one or more non-space characters. \s+ matches one or more space characters. It will add a <br>tag for each second word.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading