Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the space between two <span> tags? [duplicate]

I have a list of spans. In the HTML file, it is nice to list these <span> tags in different lines for readability and maintenance as follows:

<span><a href="some link">A link</a></span>
<span><a href="some link">A link</a></span>
<span><a href="some link">A link</a></span>
<span><a href="some link">A link</a></span>
<span><a href="some link">A link</a></span>

However, these create a space between these <span> tags. I am wondering if there is any CSS or other way to remove this space between two <span> tags without putting all above spans in one line without any space in between?

Note that we assume that the above html structure cannot change.

Thanks and regards.

-------- UPDATE ------------

After reading dfsq's solution and other related posts, I feel there is no perfect answer. It really depends on the specific need. dfsq's solution is clever and should work but get things over complicated.

What I have regarding the specific need is having a border between two spans and the space between the border and span should be the same. For my particular need, I found the two acceptable solutions too:

  1. Use CSS to float span

  2. Add an extra space as follows: <span><a href="some link">A link</a>&nbsp;</span>

Hope this helps.

like image 398
curious1 Avatar asked Sep 04 '14 13:09

curious1


People also ask

How do I change the space between two spans?

We can use the padding-left property in a <span> element to add spaces. For example, inside the HTML body, type the text of your desire. Where you want to add the blank space, create a <span> element and apply the style in it. Set the padding-left property to 30px .

How do I remove spaces between sections?

Remove the Default Space Between Paragraphs:Click on the Line and Paragraph Spacing icon in the Home Ribbon. Select "Remove Extra Space" to remove the extra space. This has to be done in each document unless you adjust your default settings.

Can you have nested span tags?

The HTML span TagYou shouldn't nest span unless you thoroughly know what you're doing – but you can put multiple span tags within a block-level element.


1 Answers

I usually set font-size of the parent container to zero, which makes white spaces causing gaps to disappear. You then just need to set font-size back to necessary value for spans, for example:

.container {
    font-size: 0;     // whitespaces go away
}
.container span {
    font-size: 16px;  // spans text please stay
    background: #DDD;
    padding: 2px 4px;
}

Demo: http://jsfiddle.net/we9bvrpe/

like image 150
dfsq Avatar answered Sep 18 '22 20:09

dfsq