264

I want to put two <div>s next to each other. The right <div> is about 200px; and the left <div> must fill up the rest of the screen width? How can I do this?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Martijn
  • 24,441
  • 60
  • 174
  • 261

13 Answers13

475

You can use flexbox to lay out your items:

#parent {
  display: flex;
}
#narrow {
  width: 200px;
  background: lightblue;
  /* Just so it's visible */
}
#wide {
  flex: 1;
  /* Grow to rest of container */
  background: lightgreen;
  /* Just so it's visible */
}
<div id="parent">
  <div id="wide">Wide (rest of width)</div>
  <div id="narrow">Narrow (200px)</div>
</div>

This is basically just scraping the surface of flexbox. Flexbox can do pretty amazing things.


For older browser support, you can use CSS float and a width properties to solve it.

#narrow {
  float: right;
  width: 200px;
  background: lightblue;
}
#wide {
  float: left;
  width: calc(100% - 200px);
  background: lightgreen;
}
<div id="parent">
  <div id="wide">Wide (rest of width)</div>
  <div id="narrow">Narrow (200px)</div>
</div>
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
M.N
  • 10,899
  • 13
  • 47
  • 49
  • 14
    This is actually the correct answer, is concise, and - unlike the two currently above it - is completely self-contained. Best answers on SO should be just like this, IMO. +1 – Bobby Jack Jun 02 '10 at 10:30
  • Care to elaborate why the left needs to be `float:left`? Your comment to my answer says 'the lft div is required the span all of the left area', but `float:left` will cause it to wrap the content tightly. – falstro Feb 14 '11 at 07:54
  • 14
    This answer is not entirely correct and it is rather upsetting to see it be upvoted so much. This creates a layout that is very unstable. I would advise putting the two divs in a container, and using the display: inline-block property in order to have the divs align, as some of the other answers have suggested. I am not criticizing anyone, as we're all here to help each other, so beside my nit picking, thank you M.N. for your contributions to this community. – Mussser Jul 17 '14 at 16:37
  • 1
    BUT one thing to note is that inline-block will not work in older versions of IE... – Mussser Jul 17 '14 at 16:41
  • 4
    @Mussser I agree with your comment but keep in mind this answer comes from 2009... a time when what you call "older versions of Ie" (i.e : IE8 and under) were "current" versions of IE... – Laurent S. Feb 06 '15 at 14:14
  • @Bartdude truuuuu, I must have not been paying such close attention to the dates. You are correct – Mussser Feb 10 '15 at 19:07
  • Am I missing something here? Adding `float: left;` to `div2` breaks the layout: http://jsfiddle.net/2fmwmrh3/. Remove it and it works fine. _Side note: div2 cannot be to the left of itself as this answer suggests._ – Nateowami Mar 21 '15 at 08:13
  • Just use 2 spans. Simple. – sproketboy Feb 24 '21 at 11:09
152

I don't know if this is still a current issue or not but I just encountered the same problem and used the CSS display: inline-block; tag. Wrapping these in a div so that they can be positioned appropriately.

<div>
    <div style="display: inline-block;">Content1</div>
    <div style="display: inline-block;">Content2</div>
</div>

Note that the use of the inline style attribute was only used for the succinctness of this example of course these used be moved to an external CSS file.

russholio
  • 1,529
  • 1
  • 9
  • 2
  • 1
    This was the solution for me. I wanted two divs adjacent like [][] (man it's been a while since I've done this..) =) kudos. – Partack Jul 29 '11 at 03:17
  • This one worked for me, too. I had some problems with another float:right'd div pushing my right column div down below the left one, so I also made the container use display:inline-block, and that' solved it. – Mar Dec 06 '11 at 22:04
  • 5
    This does not work when the content is large in Content1 DIV. The result is that the DIVs are on two separate lines instead of side-by-side. – RidingTheRails Apr 17 '12 at 10:02
  • @RichardHollis I was able to set the `width` property of all my 2 divs next to each other to prevent wrapping the second one to a new line. – Trindaz May 16 '12 at 18:10
  • This is better than the float when you don't want to specify a width for the inner divs. – Paul Syfrett Oct 25 '12 at 18:14
  • 1
    add the css vertical-align:top; to both also, else they'll push each other down if the content length differs – prospector Sep 10 '14 at 06:25
  • This works in simple cases, but fails badly in complex cases. I came here because if you nest divs INSTEAD of having the text "content1", then browsers get confused about where the line "starts" and everything breaks. If you put even a single letter of content in the outermost inline-block div ... it works fine. AFAICS inline-block isn't trustable (autumn 2014) – Adam Oct 03 '14 at 15:10
  • @Adam : inline-block is totally trustable but have some issues you need to know in order to fix it right away; The main issue is the "carriage return" character which is rendered as a space between your blocks, but there are several easy fixes for this. Of course there are complex cases, but `float` is far from easy in complex cases and can easilly break too. – Laurent S. Feb 06 '15 at 14:17
  • @Bartdude how do fix the "carriage return" issue? – gaitat Jun 24 '15 at 02:20
  • @gaitat cfr [this article](https://css-tricks.com/fighting-the-space-between-inline-block-elements/) containing all the possible fixes. The "comment" one is by far my favourite and I only use the others when this one isn't an option (the only example I can think of is a CMS - Sharepoint not to name it - stripping comments out) – Laurent S. Jun 24 '15 at 07:13
  • This works, but in case of longer text, I had to put a max-width on the elements. – kodu Jun 23 '17 at 10:45
29

Unfortunately, this is not a trivial thing to solve for the general case. The easiest thing would be to add a css-style property "float: right;" to your 200px div, however, this would also cause your "main"-div to actually be full width and any text in there would float around the edge of the 200px-div, which often looks weird, depending on the content (pretty much in all cases except if it's a floating image).

EDIT: As suggested by Dom, the wrapping problem could of course be solved with a margin. Silly me.

falstro
  • 34,597
  • 9
  • 72
  • 86
  • 1
    'float: left' will be more suitable, the lft div is required the span all of the left area. No offences meant, just consider. – M.N Jan 15 '09 at 09:03
20

The method suggested by @roe and @MohitNanda work, but if the right div is set as float:right;, then it must come first in the HTML source. This breaks the left-to-right read order, which could be confusing if the page is displayed with styles turned off. If that's the case, it might be better to use a wrapper div and absolute positioning:

<div id="wrap" style="position:relative;">
    <div id="left" style="margin-right:201px;border:1px solid red;">left</div>
    <div id="right" style="position:absolute;width:200px;right:0;top:0;border:1px solid blue;">right</div>
</div>

Demonstrated:

left right

Edit: Hmm, interesting. The preview window shows the correctly formatted divs, but the rendered post item does not. Sorry then, you'll have to try it for yourself.

David Hanak
  • 10,754
  • 3
  • 31
  • 39
15

I ran into this problem today. Based on the solutions above, this worked for me:

<div style="width:100%;"> 
    <div style="float:left;">Content left div</div> 
    <div style="float:right;">Content right div</div> 
</div> 

Simply make the parent div span the full width and float the divs contained within.

Wickyd
  • 151
  • 1
  • 2
9

UPDATE

If you need to place elements in a row, you can use Flex Layout. Here you have another Flex tutorial. It's a great CSS tool and even though it is not 100% compatible, each day its support is getting better. This works as simple as:

HTML

<div class="container">
    <div class="contentA"></div>
    <div class="contentB"></div>
</div>

CSS

.container {
    display: flex;
    width: 100%;
    height: 200px;
}

.contentA {
    flex: 1;
}

.contentB {
    flex: 3;
}

And what you get here is a container with a total size of 4 units, that share the space with its children in a relation of 1/4 and 3/4.

I have done an example in CodePen that solves your problem. I hope it helps.

http://codepen.io/timbergus/pen/aOoQLR?editors=110

VERY OLD

Maybe this is just a nonsense, but have you tried with a table? It not use directly CSS for positioning the divs, but it works fine.

You can create a 1x2 table and put your divs inside, and then formatting the table with CSS to put them as you want:

<table>
  <tr>
    <td>
      <div></div>
    </td>
    <td>
      <div></div>
    </td>
  </tr>
</table>

Note

If you want to avoid using the table, as said before, you can use float: left; and float: right;and in the following element, don't forget to add a clear: left;, clear: right; or clear: both; in order to have the position cleaned.

Timbergus
  • 3,167
  • 2
  • 36
  • 35
6
div1 {
    float: right;
} 
div2 {
    float: left;
}

This will work OK as long as you set clear: both for the element that separates this two column block.

Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38
ccpizza
  • 61
  • 1
  • 1
  • 3
    When using floats, you have to set the width property. So I don't think this is a good solution.. – Martijn Dec 04 '09 at 13:59
4

I ran into the same problem and Mohits version works. If you want to keep your left-right order in the html, just try this. In my case, the left div is adjusting the size, the right div stays at width 260px.

HTML

<div class="box">
<div class="left">Hello</div>
<div class="right">World</div>
</div>

CSS

.box {
    height: 200px;
    padding-right: 260px;
}    

.box .left {
    float: left;
    height: 200px;
    width: 100%;
}

.box .right {
    height: 200px;
    width: 260px;
    margin-right: -260px;
}

The trick is to use a right padding on the main box but use that space again by placing the right box again with margin-right.

andreas
  • 7,844
  • 9
  • 51
  • 72
3

I use a mixture of float and overflow-x:hidden. Minimal code, always works.

https://jsfiddle.net/9934sc4d/4/ - PLUS you don't need to clear your float!

.left-half{
    width:200px;
    float:left;
}
.right-half{
    overflow-x:hidden;
}
Tony Ray Tansley
  • 684
  • 5
  • 13
  • 1
    Is useful when you know the height of the div and you know the content is not longer then this height. However, when there is more content than the height of the div, it is hidden, because of the overflow... – Martijn Aug 14 '15 at 07:12
  • 1
    That's better indeed :) – Martijn Aug 14 '15 at 07:15
  • 1
    Nice! good to see people feeding backing with useful and positive feedback, rather than unconstructive negative feedback. Good man @Martijn – Tony Ray Tansley Aug 14 '15 at 07:18
  • Thank you for this. I do most of my UI work in React Native and flex box works so much better there than in HTML+CSS (in my opinion). This made my life much easier. – Eric Wiener Apr 07 '20 at 16:21
2

As everyone has pointed out, you'll do this by setting a float:right; on the RHS content and a negative margin on the LHS.

However.. if you don't use a float: left; on the LHS (as Mohit does) then you'll get a stepping effect because the LHS div is still going to consume the margin'd space in layout.

However.. the LHS float will shrink-wrap the content, so you'll need to insert a defined width childnode if that's not acceptable, at which point you may as well have defined the width on the parent.

However.. as David points out you can change the read-order of the markup to avoid the LHS float requirement, but that's has readability and possibly accessibility issues.

However.. this problem can be solved with floats given some additional markup

(caveat: I don't approve of the .clearing div at that example, see here for details)

All things considered, I think most of us wish there was a non-greedy width:remaining in CSS3...

Community
  • 1
  • 1
annakata
  • 74,572
  • 17
  • 113
  • 180
2

This won't be the answer for everyone, since it is not supported in IE7-, but you could use it and then use an alternate answer for IE7-. It is display: table, display: table-row and display: table-cell. Note that this is not using tables for layout, but styling divs so that things line up nicely with out all the hassle from above. Mine is an html5 app, so it works great.

This article shows an example: http://www.sitepoint.com/table-based-layout-is-the-next-big-thing/

Here is what your stylesheet will look like:

 .container {
    display: table;
    width:100%;
 }

 .left-column {
    display: table-cell;
 }

 .right-column {
    display: table-cell;
    width: 200px;
 }
dwaz
  • 644
  • 1
  • 8
  • 21
-1

To paraphrase one of my websites that does something similar:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <style TYPE="text/css"><!--

.section {
    _float: right; 
    margin-right: 210px;
    _margin-right: 10px;
    _width: expression( (document.body.clientWidth - 250) + "px");
}

.navbar {
    margin: 10px 0;
    float: right;
    width: 200px;
    padding: 9pt 0;
}

  --></style>
 </head>
 <body>
  <div class="navbar">
  This will take up the right hand side
  </div>
  <div class="section">
  This will fill go to the left of the "navbar" div
  </div>
 </body>
</html>
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • 4
    Not me, but I'd guess the CSS hacks, the transitional doctype, or the lack of explanation? – annakata Jan 21 '09 at 10:47
  • At least it had a doctype :) I guess it might be people not liking the browser hacks for the margins on IE. At least I'd tested it... – Rowland Shaw Jan 26 '09 at 13:04
  • It's too hacky. There are plenty of solutions out there which are much more concise. – John Bell Jun 06 '17 at 14:48
  • @JohnBell maybe that's the problem with time - it was a reasonable solution at the time (over 8 years ago), but browser technology has moved on since. Wierdly some of the other answers contemporary with mine that propose the same idea scored better (such as David's, two minutes after mine) – Rowland Shaw Jun 07 '17 at 14:01
-6

just use a z-index and everything will sit nice. make sure to have positions marked as fixed or absolute. then nothing will move around like with a float tag.

Ginger
  • 11