0

For example, i want exact 9 characters (with no extra chars/whitespaces) in the text paragraph below with CSS. (i set width to 9rem, fontsize to 1rem below)

Tried letter-spacing, inline-block, monospace font etc without luck.

.texts {
  display: inline-block;
  width: 9rem;
  font-size: 1rem;
  font-family: monospace;
  white-space: nowrap;
  overflow: hidden;
  border: 1px solid palevioletred;
  padding: 0;
  line-height: 1;
  letter-spacing: 0;
}
<p class='texts'>ninechars with extra chars</p>
Allen
  • 4,431
  • 2
  • 27
  • 39

2 Answers2

2

Yes you can achieve what you're after using the code below

p.texts { overflow: hidden; max-width: 9ch; }
<p class='texts'>ninechars</p>

And you can check the compatibility here - https://caniuse.com/#search=ch

Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
1
   <style> 
   #para1 {
        white-space: nowrap; 
        max-width: 9ch;
        overflow: hidden;
        text-overflow: ellipsis; 
        border: 1px solid #000000;
    }
    </style>

    <p id="para1">Lorem ipsum dolor sit amet, consectetur adipiscing </p>
AsankaJ
  • 82
  • 7