Add space between HTML elements only using CSS

Dan picture Dan · Nov 18, 2011 · Viewed 241.1k times · Source

I have several same HTML elements going one after another:

<span>1</span>
<span>2</span>
<span>3</span>

I'm looking for the best way of adding space BETWEEN the elements using CSS only

[no space]  [1]  [space 10px]  [2]  [space 10px]  [3]  [no space]

Additionally:

  • Please write down browser compatibility of your receipts

UPDATE

It looks like I was unclear. I don't want to use ANY ADDITIONAL HTML MARKUP like

<span></span>  <span></span>  <span class="last_span"></span>

I don't want to use tables

I want the first and last span to be targeted automatically by CSS

I don't want to use javascript

Optional requirement: last span can be NOT LAST CHILD of the parent tag, but it will be the LAST SPAN of the parent tag. Spans do not have any other tags between them.

Answer

thirtydot picture thirtydot · Nov 18, 2011

A good way to do it is this:

span + span {
    margin-left: 10px;
}

Every span preceded by a span (so, every span except the first) will have margin-left: 10px.

Here's a more detailed answer to a similar question: Separators between elements without hacks