How to make multiple divs display in one line but still retain width?

Css

Css Problem Overview


Normally, you set elements to display: inline if you want them to display in the same line. However setting an element to inline means that the width attribute would be meaningless.

How do you make divs to be in the same line without making them inline?

Css Solutions


Solution 1 - Css

You can use display:inline-block.

This property allows a DOM element to have all the attributes of a block element, but keeping it inline. There's some drawbacks, but most of the time it's good enough. Why it's good and why it may not work for you.

EDIT: The only modern browser that has some problems with it is IE7. See Quirksmode.org

Solution 2 - Css

I used the property

display: table;

and

display: table-cell;

to achieve the same.Link to fiddle below shows 3 tables wrapped in divs and these divs are further wrapped in a parent div

<div id='content'>
   <div id='div-1'><!-- Contains table --></div>
   <div id='div-2'><!-- contains two more divs that require to be arranged one below other --></div>
</div>

Here is the jsfiddle: http://jsfiddle.net/vikikamath/QU6WP/1/ I thought this might be helpful to someone looking to set divs in same line without using display-inline.

Solution 3 - Css

Flex is the better way. Just try..

display: flex;

Solution 4 - Css

You can float your column divs using float: left; and give them widths.

And to make sure none of your other content gets messed up, you can wrap the floated divs within a parent div and give it some clear float styling.

Hope this helps.

Solution 5 - Css

You can use float:left in DIV or use <span> tag, like

<div style="width:100px;float:left"> First </div> 
<div> Second </div> 
<br/>

or

<span style="width:100px;"> First </span> 
<span> Second </span> 
<br/>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeveloparvinView Question on Stackoverflow
Solution 1 - CsslomegorView Answer on Stackoverflow
Solution 2 - CssVikramView Answer on Stackoverflow
Solution 3 - CssAsitha YomalView Answer on Stackoverflow
Solution 4 - Cssninty9notoutView Answer on Stackoverflow
Solution 5 - Css莊世坤View Answer on Stackoverflow