4

Could somebody explain why the following code doesn't work?

http://jsfiddle.net/eL9hpcL9/

html

<div id="content">
    <div class="sidebar">1</div>
    <div class="sidebar">2</div>
</div>

css

#content {
    padding:0;
    margin:0;
}


.sidebar {
    width: 50%;
    display: inline-block;
    margin: 0;
    padding: 0;
}

I would expect the sidebars to be side by side, but they are not. I don't even know where to start. I know I can use float: left, but how can I get this working with inline-block?

Damien Roche
  • 13,189
  • 18
  • 68
  • 96

3 Answers3

6

You have to remove white space between divs as it is also counted and don't let 50% width work.

<div id="content">
    <div class="sidebar">1</div><!--
    --><div class="sidebar">2</div>
</div>

Fiddle: http://jsfiddle.net/eL9hpcL9/1/

emmanuel
  • 9,607
  • 10
  • 25
  • 38
0

"display: inline-block" property adding an extra space. Much better if you use the "float: left" instead of "display: inline-block", but you can add a negative margin to the left and right to kill the extra spaces. eg. "margin: 0 -2px;".

  • You really don't want to start introducing negative margins on layout just to accommodate white-space. It's a bad precedent. Especially since white-space can render differently based on the font being displayed on a user's screen. – Josh Burgess Oct 23 '14 at 14:38
0

So just style the content with display table and then the sidebar with display as table-cell and the width you need

#content {
   padding : 0;
   margin : 0;
   display : table;
 }


 .sidebar {
     display : table-cell;
     width: 50%;
     margin: 0;
     padding: 0;
 }
Edward Manda
  • 559
  • 3
  • 7