Jagadeesh
Jagadeesh

Reputation: 2800

How to apply space in span element

I have been trying to apply one white space inside the span element, but couldn't do it.

In my application, I have the following two span elements:

<span style='color:red;margin-right:1.25em'>*</span> 
<span style='color:red;margin-right:1.25em'>&nbsp;</span>

Applied these two spans to different fields to get them into the same alignment level, but I have the following problem:

enter image description here

Is there anything wrong with the above code ?

The first name field should move a bit to the right for proper alignment.

Upvotes: 31

Views: 159137

Answers (4)

Shankar Ganesh Jayaraman
Shankar Ganesh Jayaraman

Reputation: 1481

This solution worked for me,

<span>\u00A0</span>

or

<span>&nbsp;</span>

Upvotes: 9

Try add this CSS rule to your span-space element:

white-space: pre;

Upvotes: 13

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201568

Since span elements are inline elements by default, horizontal margins are ignored for them by the spec. You can make them inline blocks (supported by most, but not all, browsers), or use padding instead of margin.

This probably won’t solve the ultimate problem, as the characters “*” and the no-break space are not of the same width, except by accident. To set up a table of data consisting of form fields and associated labels and explanations, use an HTML table, and then just add a little styling in CSS.

Upvotes: 6

jwchang
jwchang

Reputation: 10864

margin is applied to block or inline-block elements

but not inline element like

span tags

try this

<span style='color:red;margin-right:1.25em; display:inline-block;'>&nbsp;</span>

Upvotes: 57

Related Questions