Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap two spans into one line with CSS

Tags:

html

css

line

I want to wrap two spans in one line with CSS.

Here is my code:

<div style="width:60px;">
    <span id="span1" style="float:left; display:inline;"></span>
    <span id="span2" style="float:left; display:inline; "></span>
</div>

But it doesn't work.

How to do that?

Edit:

I want to use the "id", either use div or span, I just want them to be in one line.

When I just use span without style, the content are not in the same line. The second line will go down.

like image 573
SUN Jiangong Avatar asked Dec 23 '10 17:12

SUN Jiangong


People also ask

How do you put two spans in the same line?

Usually with a float to work you need a width with it as well. It can't float them against each other because it doesn't know how much space each span will occupy in relation to the div. Spans are inherently inline elements unless you define them otherwise, so they should just display that way without the float.

How do I align two spans side by side?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

Can I put a span inside a span?

Yes. You can have a span within a span .

How do you put a space between two spans in CSS?

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 .


5 Answers

Here is the working example:

<div style="float:left;">
    <span style="display:inline; color: red;">First Span</span>
    <span style="display:inline; color: blue;">Second Span</span>
</div>
like image 193
Pradeep Singh Avatar answered Sep 25 '22 13:09

Pradeep Singh


The float will mess things up. Usually with a float to work you need a width with it as well. It can't float them against each other because it doesn't know how much space each span will occupy in relation to the div. Spans are inherently inline elements unless you define them otherwise, so they should just display that way without the float.

like image 40
jbwharris Avatar answered Sep 23 '22 13:09

jbwharris


<div style="float:left;">
    <span style="display:contents; color: red;">First Span</span>
    <span style="display:contents; color: blue;">Second Span</span>
</div>

'display:contents' Makes the container disappear, making the child elements children of the element the next level up in the DOM which I believe is the right answer.

Another way which works on ie too is this:

<div style="float:left; display:flex;">
    <span style="color: red;">First Span</span>
    <span style="color: blue;">Second Span</span>
</div>
like image 35
Manudeep Avatar answered Sep 24 '22 13:09

Manudeep


In some cases display:inline does not work, in such case try adding both spans in one parent span like below

<span>
  <span id="span1">Span 1</span>
  <span id="span2">Span 2</span>
</span>
like image 24
Yuvraj Patil Avatar answered Sep 22 '22 13:09

Yuvraj Patil


It's the float left that is causing it to be on separate lines. Maybe try a &nbsp; (non breaking space) in between the spans.

like image 26
Samuel Avatar answered Sep 23 '22 13:09

Samuel