3

How can I make two div appear side by side? I want the first div appear on left, and second to appear right beside that.

<div class="first">first</div>
<div class="second">second</div>

What I want:

first   second

What I get:

first

second

Workaround with tables:

<table>
    <tr>
        <td>first</td>
        <td>second</td>
    </tr>
</table>

How to make two divs behave like these two table cells?

Bob Stein
  • 16,271
  • 10
  • 88
  • 101
Hitesh Shrimali
  • 112
  • 1
  • 1
  • 5
  • 2
    Use `float` with left and right values. – slash197 Sep 20 '13 at 06:00
  • See [this post](http://stackoverflow.com/a/10240515/727208) and explore the source. All of "answers" here will fail when left column has no content. – tereško Sep 20 '13 at 06:21
  • Remember if you are using CSS float property to align div side by side don't forget to clear float otherwise parent elements will be collapsed. Check this page to learn perfect alignment of div tag using float property - http://www.tutorialrepublic.com/css-tutorial/css-alignment.php – Alex Sep 20 '13 at 06:33
  • This question is not a genuine issue, instead it is the result of not learning the basics of HTML or CSS. Also, there are multiple duplicate questions on SO that answer this question perfectly – DividedByZero Sep 26 '14 at 13:42

5 Answers5

8

You just need to add float left to your first div and it will work!

Jayesh Amin
  • 314
  • 2
  • 12
7

you can use

 float:left

DEMO here

Anusha Honey
  • 887
  • 4
  • 11
  • 26
6

basically there are 2 techniques.

  1. using float. assign a floating value (left | right) to your elements, and they will span one after each other like inline elements based on available space. Downsize: floating breaks the normal flow of the page, so you'll have to use clearfix methods to make the parent container long as his content. (actually not so bad)

  2. using inline-block. assign display:inline-block to your block elements to make them span one after each other like 'inline' elements based on available space. Downsize: each 'new-line' in the markup is treated like a white-space with inline-block elements, causing gapes between elements in the view. and also, assigning 'inline-block' to 'inline-block' elements isn't supported in IE7, so you'll have to use methods to overcome the gap, and IE7 Hack.

I actually prefer the inline-block method.

avrahamcool
  • 13,888
  • 5
  • 47
  • 58
  • IIRC, the `inline-block` in IE7 will be actually rendered as `inline`, which can (and usually - will) cause additional problems. Anyway, IE7 in most projects is not supported an the 3px gap has [known solutions](http://haslayout.net/css/3px-Gap-Bug-aka-Text-Jog-Bug). – tereško Sep 20 '13 at 06:27
  • I already linked to good articles about those problems and how to address them. – avrahamcool Sep 20 '13 at 06:32
4

simple, Use float:left; to the divs css,

<div style="float:left" > </div>
<div style="float:left" > </div>

and CSS Floats 101, best place to make an understandind of it, very helpful.

Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43
1

Many ways. You could float the "first"/leftmost div like in this fiddle.

http://jsfiddle.net/2ENMK/

CSS:

div.left
{
    float: left;
    border: 1px solid red;
}

div.lefthugger
{
    border: 1px solid blue;
}
andrewb
  • 2,995
  • 7
  • 54
  • 95