9

How do you to put spaces between text in html? For example,

<p>a b       c</p>

What do you put to have space between b and c?

weather api
  • 738
  • 2
  • 8
  • 17
  • 2
    ` ` might be what you're looking for. Or you can wrap a letter in a `` tag and apply margin or padding. Like `

    a b c

    `
    – Michael Coker Feb 26 '17 at 02:03
  • 3
    Possible duplicate of [How to create small SPACES in HTML?](http://stackoverflow.com/questions/2720770/how-to-create-small-spaces-in-html) – Jordan Running Feb 26 '17 at 02:07

6 Answers6

12

Try using white-space with value pre.

pre Sequences of whitespace are preserved. Lines are only broken at newline characters in the source and at <br> elements.

p {
  white-space: pre;
}
<p>a b       c</p>

Use a monospaced font if you need each black space to take exactly one character width.

p {
  white-space: pre;
  font-family: monospace;
}
<p>a b       c</p>
Stickers
  • 75,527
  • 23
  • 147
  • 186
11

You can try to put &nbsp; between them. Like this:

<p>a b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c</p>
Tân
  • 1
  • 15
  • 56
  • 102
  • 3
    Not a clean solution. – weather api Feb 26 '17 at 02:07
  • 2
    It's not clean, but your question doesn't specify what can and can't be done. You've asked how to maintain the spaces in `

    a b c

    ` (huh, inline doesn't keep the spacing), and that's exactly what this answer provides. Please try to clarify your answer a bit more
    – Matheus Avellar Feb 26 '17 at 02:31
2

There are several ways.

Pre Tag

One is to use the <pre> tag, which will render content with the whitespace intact.

Space Entities

Another is to add space entities, like &nbsp; (non-breaking space). Keep in mind with this approach, there are several space entities, and this particular entity does not word-wrap, which may not be the behavior you wanted. &#32; renders a normal space, which the browser should not collapse, and which will word-break as expected.

Here is a list of different Unicode spaces you can use.

CSS

Also, you usually should not use HTML entities to create space between text which is not part of one uniform sentence. This is because in this case, the better solution is to use margin or padding to separate the two, and keep each in its own element.

As another answer mentions, CSS letter-spacing is also an option if the spacing is consistent but larger than normal.

Here's an example of all these approaches (the CSS is not important here):

th, td {
  border: 1px solid black;
  padding: 3px;
}

th {
  background-color: darkgrey;
  color: white;
}

table {
  border-collapse: collapse;
}

body {
  font-family: sans-serif;
}
<table>
  <thead>
    <tr>
      <th colspan="2">HTML Spacing</th>
    </tr>
    <tr>
      <th>Method</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Normal</td><td><p>a b       c</p></td>
    </tr>
    <tr>
      <td>Pre Tag</td><td><pre>a b       c</pre></td>
    </tr>
    <tr>
      <td>Margin Left</td><td><span>a </span><span>b</span><span style="margin-left: 3.5em;">c</span></td>
    </tr>
    <tr>
      <td>Non-breaking Space</td><td><p>a b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c</p></td>
    </tr>
    <tr>
      <td>Em Space</td><td><p>a b&#8192;&#8192;&#8192;&#8192;&#8192;&#8192;&#8192;c</p></td>
    </tr>
    <tr>
      <td>Letter-Spacing</td><td><span>a </span><span style="letter-spacing: 3.5em;">bc</span></td>
    </tr>
  </tbody>
</table>
KthProg
  • 2,050
  • 1
  • 24
  • 32
2

Use the CSS property word-spacing to set space between words. You can also use <span> and apply margin or padding.

So:

span { margin-left:1em }
<p>a b <span>c</span></p>
Rob
  • 14,746
  • 28
  • 47
  • 65
1

Use &nbsp; . This will not breaks line too.

Yogi
  • 54
  • 8
1

As others have said, you can add a space with : &nbsp;

But also add tabs with : &emsp; and &ensp;

Julien J
  • 2,826
  • 2
  • 25
  • 28