Add space between HTML elements only with CSS

I have several identical HTML elements going one after another:

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

I am looking for a better way to add space BETWEEN elements using CSS

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

Additionally:

  • Please write down browser compatibility with your receipts.



UPDATE

Looks like I was unclear. I do not want to use ANY ADDITIONAL HTML MARKER, for example

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

I do not want to use tables

I want the first and last range to be automatically adjusted using CSS

I do not want to use javascript

Additional requirements: the last range may NOT be the LAST CHILD of the parent tag, but it will be the LAST SPAN of the parent tag. There are no other tags in between.

+63
css layout
Nov 18 '11 at 15:43
source share
9 answers

A good way to do this:

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

Each span preceded by a span (therefore, each span except the first) will have margin-left: 10px .

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

+141
Nov 18 2018-11-11T00:
source share

Just use margin or padding.

In your specific case, you can use margin:0 10px only on the second <span> .

UPDATE

Here is a good CSS3 solution ( jsFiddle ):

 span { margin: 0 10px; } span:first-of-type{ margin-left: 0; } span:last-of-type{ margin-right: 0; } 

Unfortunately, an extended selection of elements using selectors of the type :nth-child() :last-child :first-of-type , etc. not supported by Internet Explorer 8 and previous versions.

+20
Nov 18 '11 at 15:45
source share

You can take advantage of the fact that span is an inline element

 span{ word-spacing:10px; } 

However, this solution will break if there is more than one word of text in your range.

+7
May 27 '13 at 15:55
source share

It is so simple.

You can style elements with the exception of the first one in only one line of code:

 span ~ span { padding-left: 10px; } 

and done, without manipulating the class.

+4
May 26 '14 at 12:51
source share

You can write like this:

 span{ margin-left:10px; } span:first-child{ margin-left:0; } 
+2
Nov 18 2018-11-11T00:
source share
 span:not(:last-child) { margin-right: 10px; } 
+2
Dec 11 '14 at 8:13
source share

<span> is an inline element, so you cannot span them without making it a block level. try it

Horizontally

 span{ margin-right: 10px; float: left; } 

Vertical

 span{ margin-bottom: 10px; } 

Compatible with all browsers.

+1
Nov 18 '11 at 15:50
source share
 span.middle { margin: 0 10px 0 10px; /*top right bottom left */ } <span>text</span> <span class="middle">text</span> <span>text</span> 
0
Nov 18 '11 at 15:53
source share

Or instead of setting the marker and redefining it, you can immediately set it correctly with the following combination:

 span:not(:first-of-type) { margin-left: 5px; } span:not(:last-of-type) { margin-right: 5px; } 
0
Nov 14 '14 at 13:02
source share



All Articles