Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you put two divs next to each other so they fill up the available space

Tags:

css

css-float

I have two divs, the right one is 80px wide the other should fill up the remaining space. So far I tried:

<!DOCTYPE html>

<html>
<head>
    <title>#{get 'title' /}</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }

        #left {
            position: relative;
            margin-right: 80px;
            background-color: red;
        }

        #right {
            float: right;
            position: relative;
            text-align: left;
            width: 80px;
            background-color: yellow;
        }
    </style>
</head>
<body>
<div id="left">
    Left
</div>
<div id="right">
    Right
</div>
</body>
</html>

However, the right box is always put below the left box and not right to it. I guess it is because of the margin. I also tried a margin-left: -80px on the right one but this doesnt seem to change anything. So how do I have to change the CSS so that the right div is in the same line as the left div?

like image 879
Jan Thomä Avatar asked Feb 09 '11 14:02

Jan Thomä


People also ask

How do I align items next to each other in a div?

If you want to place them next to each other you would require to use a CSS property float. As the name goes the float property specifies how an element should float in the webpage on the left or the right!. Values that a float property can have areas below, left - The element will float left w.r.t to its container.

How do you put a space between two divs vertically?

a (the fixed one) to the top of the page, add top: 0; and if you want it to stay on top of the rest of the content, include z-index: 2; . In order to add spacing between div.


1 Answers

Have the right div before the left.

<div id="right">
    Right
</div>
<div id="left">
    Left
</div>

Working Example

like image 80
Tim Cooper Avatar answered Nov 07 '22 11:11

Tim Cooper