Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

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?

like image 305
developarvin Avatar asked Jan 19 '12 02:01

developarvin


People also ask

How do I show multiple divs on the same line?

To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values. The value left indicates the div tag will be made to float on the left and right to float the div tag to the right.

How do you keep divs in one line?

Set size and make inline Because they're block elements, when reducing the size of Div one to make room for the other div, you're left with space next to Div one and Div two below Div one. To move the div up to the next line both div's need to have the inline-block display setting as shown below.

How do I put 3 divs in a row?

Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.


2 Answers

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

like image 105
lomegor Avatar answered Sep 25 '22 17:09

lomegor


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.

like image 27
Vikram Avatar answered Sep 25 '22 17:09

Vikram