How to remove spaces generated by span tag?

Viewed 542

I have inserted single letters in span tag. While running this web page on the browser, span tag insert 4 pixel space automatically before every letter. I want to remove that space. Please suggest me best solution.

<span>h</span>
<span>e</span>
<span>l</span>
<span>l</span>
<span>o</span>
<span></span>
<span>w</span>
<span>o</span>
<span>r</span>
<span>l</span>
<span>d</span>

4 Answers

Just Don’t Break Line Make It In Continuous Form In Order To Achieve Your Solution

<span>h</span><span>e</span><span>l</span><span>l</span><span>o</span><span></span><span>w</span><span>o</span><span>r</span><span>l</span><span>d</span>

By using display css properties, you can remove white space and align adjacently.

span {
  display: table-cell;
}
<span>h</span>
<span>e</span>
<span>l</span>
<span>l</span>
<span>o</span>
<span></span>
<span>w</span>
<span>o</span>
<span>r</span>
<span>l</span>
<span>d</span>

table-cell : These elements behave like <td> HTML elements

As noted this is occurring because of the white space between adjacent lines - you can remove by wrapping the spans in a div with display: flex on it. and by giving the empty span a width of 1ch

the ch - represents the width, or more precisely the advance measure, of the glyph "0" (zero, the Unicode character U+0030) in the element's font).

Although I would do this differently entirely and you do NOT need to use jQuery for such a simple thing.

.wrapper {
  display: flex
}

span:empty {
  width: 1ch
}
<div class="wrapper">
  <span>h</span>
  <span>e</span>
  <span>l</span>
  <span>l</span>
  <span>o</span>
  <span></span>
  <span>w</span>
  <span>o</span>
  <span>r</span>
  <span>l</span>
  <span>d</span>
</div>

You can use Jquery to remove the spaces between span tag.

        //jquery
        $('p').contents().filter(function() { return this.nodeType === 3; }).remove();
        
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <p>
        <span>h</span>
        <span>e</span>
        <span>l</span>
        <span>l</span>
        <span>o</span>
        <span>w</span>
        <span>o</span>
        <span>r</span>
        <span>l</span>
        <span>d</span>
        </p>

Related