Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force spans to be on the same line as a div?

Tags:

html

css

I have a simple structure of:

<div></div><span></span><span></span>

but I want to force them all on one line! at the moment, they appear:

<div />
<span /><span />

unfortunately, I HAVE to have the first element as a div; the div is acting as a bar from a bar chart (so rounded corners, width = jquery stuff, no content and block colour inside), the next span is the value the bar represents and the last span is what the value is associated to.

so I want

[____________] 25% ObjectA
[________________________] 50% ObjectB
[______] 12.5% ObjectC
[______] 12.5% ObjectD

and not

[____________] 
25% ObjectA
[________________________] 
50% ObjectB
[______] 
12.5% ObjectC
[______] 
12.5% ObjectD
like image 722
Phish Avatar asked Sep 26 '13 08:09

Phish


People also ask

How do you get span to stay on one line?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.

How do I display a div and SPAN in one line?

In the wrapping div you should have: display: flex; align-items: center; and remove the display property from both elements.

How do I force HTML elements to stay on the same line?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

How do you make a span behave like a div?

A <span style="display:block"></span> will indeed behave like a div element. In such case the difference is a semantic one, as divs are meant to be containers while spans are wrappers for sections of text or other inline content.


1 Answers

Put the CSS property display: inline-block on the div to make it act like an inline element instead of taking up the whole line.

Edit:

@Mr_Green's suggestion to use the after pseudo-element to clear each line is absolutely necessary to prevent a broken layout.

like image 186
Dylan MacKenzie Avatar answered Sep 20 '22 18:09

Dylan MacKenzie