Khor Shao Liang

I am a final year student in School of Computing at National University of Singapore. I am pursuing a Bachelor of Computing (Computer Science) specializing in Database Systems.

Limit characters displayed in HTML span

06 Aug 2019 » html, css

Background / Context

Unlike input tag, span tag do not have the property of maxlength. To limit the characters in a span tag we need to use some CSS tricks.

Fix / Solution

Create a CSS class e.g. search__result-title and do the following:

.search__result-title {
  display: inline-block;
  width: 70%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

Expected Example Result

limit-span-tag-characters