57

I need to align two divs next to each other, so that each contains a title and a list of items, similar to:

<div>
    <span>source list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

<div>
    <span>destination list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

It's remarkably easy to do with tables, but I don't want to use tables. How can I achieve this?

TylerH
  • 20,799
  • 66
  • 75
  • 101
DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • 1
    @TylerH, how is this a duplicate of a question asked nearly a year after this was asked? – DaveDev Feb 24 '19 at 23:53
  • age of the question is only one aspect to consider when closing questions as a duplicate; the far more important aspect to consider is which question has better answers and covers not only the second question, but others as well. This is called a canonical question. In this case, there are 8 answers that say "float" and 2 that say "use flexbox". The other question contains solutions that cover both of those *and more*, making it a better target. – TylerH Feb 25 '19 at 00:19

10 Answers10

64

Float the divs in a parent container, and style it like so:

.aParent div {
    float: left;
    clear: none; 
}
<div class="aParent">
    <div>
        <span>source list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
    <div>
        <span>destination list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
</div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Robusto
  • 31,447
  • 8
  • 56
  • 77
39

Nowadays, we could use some flexbox to align those divs.

.container {
    display: flex;
}
<div class="container">
    <div>
        <span>source list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>

    <div>
        <span>destination list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
</div>
Antoine Neff
  • 446
  • 4
  • 7
21
<div>
<div style="float:left;width:45%;" >
    <span>source list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

<div style="float:right;width:45%;">
    <span>destination list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>
<div style="clear:both; font-size:1px;"></div>
</div>

Clear must be used so as to prevent the float bug (height warping of outer Div).

style="clear:both; font-size:1px;
coderex
  • 27,225
  • 45
  • 116
  • 170
4

You need to float the divs in required direction eg left or right.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
3

Wrap them both in a container like so:

.container{ 
    float:left; 
    width:100%; 
}
.container div{ 
    float:left;
}
<div class='container'>
    <div>
        <span>source list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
    <div>
        <span>destination list</span>
        <select size="10">
            <option />
            <option />
            <option />
        </select>
    </div>
</div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Brant
  • 5,721
  • 4
  • 36
  • 39
3

Add a class to each of the divs:

.source, .destination {
    float: left;
    width: 48%;
    margin: 0;
    padding: 0;
}
.source {
    margin-right: 4%;
}
<div class="source">
    <span>source list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>
<div class="destination">
    <span>destination list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

That's a generic percentages solution - using pixel-based widths is usually much more reliable. You'll probably want to change the various margin/padding sizes too.

You can also optionally wrap the HTML in a container div, and use this CSS:

.container {
  overflow: hidden;
}

This will ensure subsequent content does not wrap around the floated elements.

TylerH
  • 20,799
  • 66
  • 75
  • 101
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
3

float is obsolete, better use display: flex;:

example :

.parent-div{ display: flex; }

indicate the direction by flex-direction: row/column;. go down if no space by flex-wrap: wrap/nowrap;

more properties here.

Tim Barrass
  • 4,813
  • 2
  • 29
  • 55
infantry
  • 336
  • 1
  • 5
  • 15
1

if you have two divs, you can use this to align the divs next to each other in the same row:

#keyword {
    float:left;
    margin-left:250px;
    position:absolute;
}

#bar {
    text-align:center;
}
<div id="keyword">
Keywords:
</div>
<div id="bar">
    <input type = textbox   name ="keywords" value="" onSubmit="search()" maxlength=40>
    <input type = button   name="go" Value="Go ahead and find" onClick="search()">
</div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
A.B.
  • 51
  • 1
  • 5
-2
<html>
<head>
<style type="text/css">
#floatingDivs{float:left;}
</style>
</head>

<body>
<div id="floatingDivs">
    <span>source list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

<div id="floatingDivs">
    <span>destination list</span>
    <select size="10">
        <option />
        <option />
        <option />
    </select>
</div>

</body>
</html>
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
-2

For your purpose, I'd prefer using position instead of floating:

http://jsfiddle.net/aas7w0tw/1/

Use a parent with relative position:

position: relative;

And children in absolute position:

position: absolute;

In bonus, you can better drive the dimensions of your components.

Rémi Doolaeghe
  • 2,262
  • 3
  • 31
  • 50