20

For the code below I want add spacing between "Discount" and $500. I don't want to add additional break tag. Here's the sample on jsbin.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Spacing Test</title>
<style type="text/css">
        .labelReadOnlyTB {
            font-size: 16px;
            font-family: Arial;
            padding: 1px;
        }
        .labelForTextbox
        {
        font-weight: bold;
        font-size: 12px;
        color: #000000;
        font-family: Arial;
        padding:8px 0px;
        margin-bottom:5px;
        }
    </style>
</head>
<body>
  <table>
      <tr>
         <td style="min-height:45px;vertical-align:top;">
        <span id="lblDiscount" class="labelForTextbox">Discount</span>
         <br />
        <span id="lblValue" class="labelReadOnlyTB">$500</span>
        </td>
      </tr>
  </table>

</body>
</html>
gbs
  • 7,126
  • 4
  • 42
  • 68

6 Answers6

23

If you want to add spacing in html you can also use.

&nbsp;

If you put three of these before your text, it will add 3 white spaces.

for example:

<label>&nbsp;<span>Test</span></label>
j3ff
  • 5,139
  • 7
  • 39
  • 50
tim
  • 611
  • 7
  • 10
  • I rarely use   to layout because it takes space according to font-size, which makes it harder for layout. – Eric Jul 19 '21 at 10:49
16

If I understand you correclty you want the spans to be on separate lines, but not have to use the <br> tag.

<span> is by default an inline element. Give it a property of display: block;

UPDATED with relevant code based on comment:

.labelForTextbox {
  ...
  display: block;
  margin-bottom: 10px; /** Change this value to whatever you wish **/
}
kunalbhat
  • 1,664
  • 10
  • 11
8

Unlike <div> or <p> (which are block-level elements), <span> is an inline element.

According to Spec:

margin-top, margin-bottom properties have no effect on non-replaced inline elements.

In order to use top/bottom margin properties, you need to change the element's display type to block or inline-block (or whatever else margin is applicable to).

span {
    display: inline-block; /* change the display type           */
    margin: 10px 0;        /* apply the needed vertical margins */
}

Here is the JSBin Demo

Or, Simply set line-height property on the table-cell instead:

td { /* change the selector to select your specific td */
    line-height: 1.5; /* <-- set a line-height */
}
Hashem Qolami
  • 93,110
  • 25
  • 145
  • 156
5

For the second span, you can use pading-left:somevalue

Example : <span>..</span><span style="padding-left:4px">..</span>

Simon H
  • 2,505
  • 4
  • 31
  • 38
Malatesh Patil
  • 4,055
  • 1
  • 13
  • 14
2

Since I see that there is a newline that puts "Discount" and "$500" on different lines I assume it will print on separate lines, and to get a little more room but not an entire new line you can use line-height.

In your css:

span#lblDiscount {
  line-height:180%
}

Try about 120-200% of normal line height and it will put a nice amount of spacing between your two lines. Hope this helps.

taronish4
  • 494
  • 3
  • 5
  • 13
0

Another solution:

span::after {
  content: ' ';
}

This adds a space behind any span (or whatever you want). You could also combine it with :last-child or css sibling + selector to only add this for elements between.

Dominik
  • 3,538
  • 1
  • 16
  • 32