267

I am trying to place two divs side by side and using the following CSS for it.

#left {
  float: left;
  width: 65%;
  overflow: hidden;
}

#right {
  overflow: hidden;
}

The HTML is simple, two left and right div in a wrapper div.

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

I have tried so many times to search for a better way on StackOverflow and other sites too, But couldn't find the exact help.

So, the code works fine at first glance. Problem is this, that the left div gets padding/margin automatically as I increase width in (%). So, at 65% width, the left div is having some padding or margin and is not perfectly aligned with the right div, I tried to padding/margin 0 but no luck. Secondly, If I zoom in the page, the right div slides below the left div, Its like not fluid display.

Note: I am sorry, I have searched a lot. This question has been asked many times but those answers aren't helping me. I have explained what the problem is in my case.

I hope there is a fix for that.

Thank you.

EDIT: Sorry, me HTML problem, There were two "box" divs in both left and right sides, They had padding in %, So left side showed more padding because of greater width. Sorry, The above CSS works perfect, its fluid display and fixed, Sorry for asking the wrong question...

Oliver Hader
  • 4,093
  • 1
  • 25
  • 47
Waleed
  • 3,703
  • 3
  • 14
  • 11

10 Answers10

296

Try a system like this instead:

.container {
  width: 80%;
  height: 200px;
  background: aqua;
  margin: auto;
  padding: 10px;
}

.one {
  width: 15%;
  height: 200px;
  background: red;
  float: left;
}

.two {
  margin-left: 15%;
  height: 200px;
  background: black;
}
<section class="container">
  <div class="one"></div>
  <div class="two"></div>
</section>

You only need to float one div if you use margin-left on the other equal to the first div's width. This will work no matter what the zoom and will not have sub-pixel problems.

Matt-the-Marxist
  • 164
  • 1
  • 1
  • 12
dezman
  • 18,087
  • 10
  • 53
  • 91
  • 1
    It is not helping, the zooming thing is fixed now, it says fixed, but the right div is now slided down and fixed at that position – Waleed Jun 20 '13 at 15:51
  • You probably messed something up, check your code, or tell me the link to the jsFiddle and ill look at it. – dezman Jun 20 '13 at 15:55
  • aww man, I am sorry. The divs were already fixed by my above CSS which I gave, its just the "box" divs in both left and right side, had padding and margin in %, because right div was short thats why, It had enqual padding and margins. Sorry... – Waleed Jun 20 '13 at 15:56
  • Shouldn't the `
    ` be a `
    ` instead?
    – jvriesem Mar 18 '20 at 16:59
275

This is easy with a flexbox:

#wrapper {
  display: flex;
}

#left {
  flex: 0 0 65%;
}

#right {
  flex: 1;
}
<div id="wrapper">
  <div id="left">Left side div</div>
  <div id="right">Right side div</div>
</div>
Christopher Moore
  • 3,071
  • 4
  • 30
  • 46
  • 7
    Nice, flexbox is definitely the way to go – julian soro Dec 28 '15 at 01:38
  • 4
    According to this site, flex should work on 94% of browsers. http://caniuse.com/#search=flex – Adrian May 16 '16 at 12:19
  • 1
    @Adrian That is 1 in 20 people unable to use your site... Is that really an option? – Mr. Hugo Jun 08 '16 at 19:41
  • 8
    @JoostS isn't that 94% of the different browsers available (as in it always works on chrome, Mozilla, IE etc etc), not that it works 94% of the time regardless of browser? – Joe Jul 24 '16 at 11:54
  • @Adrian No. It is the amount of users of the browsers that do or do not support the feature. – Mr. Hugo Jul 24 '16 at 15:56
  • 7
    Currently it stands at 97+%. Basically, I'd say if you don't need to target IE8, go with flexbox, in this case and others. Flexbox solutions are almost always more elegant and easier to reason about. – Alan Thomas Jan 19 '17 at 21:27
  • @AlanThomas even then, targetting IE11 means you need to be aware of the Known Issues (there's 8 on caniuse.com at the moment) – shmup Jun 28 '17 at 17:19
  • 6
    If you have an existing website, don't ever go by browser share, look at your own traffic logs. On most of my sites, IE8 accounts for only about 0.01% of traffic. However...I have seen specific sites where the audience is users in corporations, government, or non-profit organizations using a lot of legacy software, and then old IE browser usage can be surprisingly high. – cazort Aug 23 '17 at 14:53
  • Totally the way to go in 2019! – Robert Moskal Oct 30 '19 at 19:25
  • Shouldn't we use `class` rather than `id` so that this could be replicated on other parts of the page? – jvriesem Mar 18 '20 at 17:08
100

Using this CSS for my current site. It works perfect!

#sides{
margin:0;
}
#left{
float:left;
width:75%;
overflow:hidden;
}
#right{
float:left;
width:25%;
overflow:hidden;
} 
Waleed
  • 3,703
  • 3
  • 14
  • 11
  • 55
    Glad to hear you found and accepted your own answer, but what is #sides? It's not in your original question. – JMD Jan 12 '15 at 17:55
  • 4
    Using float:left on the both child (#right) will kill the height of parent div (#wrapper). So this solution depends on the requirement . Better to give float on one child only.(#left in your case) – Rahul Gandhi Apr 06 '16 at 17:23
8

Here's my answer for those that are Googling:

CSS:

.column {
    float: left;
    width: 50%;
}

/* Clear floats after the columns */
.container:after {
    content: "";
    display: table;
    clear: both;
}

Here's the HTML:

<div class="container">
    <div class="column"></div>
    <div class="column"></div>
</div>
Malachi Bazar
  • 1,695
  • 1
  • 19
  • 21
8

Make both divs like this. This will align both divs side-by-side.

.my-class {
  display : inline-flex;
} 
Coen Damen
  • 2,009
  • 5
  • 29
  • 51
2

You can also use the Grid View its also Responsive its something like this:

#wrapper {
   width: auto;
    height: auto;
    box-sizing: border-box;
    display: grid;
    grid-auto-flow: row;
    grid-template-columns: repeat(6, 1fr);
}

#left{
    text-align: left;
    grid-column: 1/4;
}

#right {
    text-align: right;
    grid-column: 4/6;
}

and the HTML should look like this :

<div id="wrapper">
<div id="left" > ...some awesome stuff </div>
<div id="right" > ...some awesome stuff </div>
</div>

here is a link for more information:

https://www.w3schools.com/css/css_rwd_grid.asp

im quite new but i thougt i could share my little experience

Jaba
  • 99
  • 1
  • 8
2

#wrapper{
  display: grid;
  grid-template-columns: 65% 1fr;
}
#left {
  grid-column:1;
  overflow: hidden;
  border: 2px red solid;
}

#right {
  grid-column:2;
  overflow: hidden;
  border: 2px blue solid;
}
<div id="wrapper">
  <div id="left">Left side div</div>
  <div id="right">Right side div</div>
</div>
Metagrapher
  • 8,602
  • 1
  • 24
  • 31
1

#sides{
margin:0;
}
#left{
float:left;
width:75%;
overflow:hidden;
}
#right{
float:left;
width:25%;
overflow:hidden;
} 
<h1 id="left">Left Side</h1>
<h1 id="right">Right Side</h1>
<!-- It Works!-->
CoolPerson879
  • 60
  • 1
  • 12
0

<div style="height:50rem; width:100%; margin: auto;">
  <div style="height:50rem; width:20%; margin-left:4%; margin-right:0%; float:left; background-color: black;"></div>
  <div style="height:50rem; width:20%; margin-left:4%; margin-right:0%; float:left;  background-color: black;"></div>
  <div style="height:50rem; width:20%; margin-left:4%; margin-right:0%; float:left;  background-color: black;"></div>
  <div style="height:50rem; width:20%; margin-left:4%; margin-right:0%; float:left;  background-color: black;"></div>
</div>

margin-right isn't needed though.

katalin_2003
  • 787
  • 1
  • 16
  • 30
0

.container {
  display: flex;
}

.box1, .box2 {
  flex: 1;
  padding: 20px;
  border: 1px solid #000;
}
<div class="container">
  <div class="box1">Div 1 </div>
  <div class="box2">Div 2 </div>
</div>
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '23 at 06:29