25

I'm getting some strange whitespace between two divs I have.

Each div has the css property display: inline-block and each have a set height and width.

I cannot find where the whitespace is.

Here is a Fiddle

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
Taras Lukavyi
  • 1,339
  • 2
  • 14
  • 29

12 Answers12

26

You get whitespace there because you have whitespace inbetween the divs. Whitespace between inline elements is interpreted as a space.

You have:

<div id="left_side">
    <div id="plan">
        <h1>div 1</h1>
    </div>
</div>
<div id="right_side">
    <div id="news">
        <h1>div 2</h1>
    </div>
</div>

Change for:

<div id="left_side">
    <div id="plan">
        <h1>div 1</h1>
    </div>
</div><div id="right_side">
    <div id="news">
        <h1>div 2</h1>
    </div>
</div>

However, this is a bad way to do what you want to do.

You should float the elements if thats what you want to do.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
23

Use:

float:left;
clear:none;  

In both div

check123
  • 1,989
  • 2
  • 22
  • 28
11

If you want to retain your coding layout, avoid floats and keep each div on it's own line entirely...

<div id="leftSide">Some content here</div><!-- --><div id="rightSide">Some more content here</div>

fuzzysearch
  • 846
  • 9
  • 15
6

Only add this to your CSS

h1 {
    padding:0;
    margin:0;
   }

Space between div is only due to h1 Margin and Padding

user3170223
  • 69
  • 1
  • 3
  • If you have two divs without padding and margin one near another there are two cases: a) if the html is minimzed (no space between tags) then the divs will be adjacent. b) if there is a single space/tab/enter then the browser shows a small space between those divs. Your answer does not respond user's question. I made time to write a small explanation so you might understand the difference. Kudos! – besciualex Aug 07 '20 at 19:52
  • Just `margin: 0;` seems to work in most cases. No need for `padding:0;` unless that element already has padding. – kmoser Sep 29 '22 at 17:25
5

You can also add display: flex; to the divs' parent container (in this case, body). Fiddle.

dodov
  • 5,206
  • 3
  • 34
  • 65
5

This does the trick:

<div id="left_side">
    ...
</div><div id="right_side">
    ...
</div>

Notice how the right-side div starts immediately after the closing tag of the left-side div. This works because any space between the elements, since they are now inline, would become a space in the layout itself. You can mirror this behavior with two span elements.

Demo.

Purag
  • 16,941
  • 4
  • 54
  • 75
2

best way is settings parent element's font-size to 0 then normal font-size to child elements inside that parent (otherwise inherits zero from parent)

gdarcan
  • 595
  • 5
  • 8
1

Floated both of the elements left, also made the 30% width into 40% to fill all the space, but this isn't necessary. Please be aware, "inline-block" isn't supported by IE7 but can be fixed with a workaround.

http://jsfiddle.net/RVAQp/3/

AlexKempton
  • 1,648
  • 15
  • 28
1

Move these statements onto the same line:

</div><div id="right_side">
Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
0

Tried using float instead of "inline-block", no problems. Just changed the display:inline-block to:

#left_side {float: left;}

and

#right_side {float: right; margin-right: 10%}

No apparent problems. Could be wrong.

user2613041
  • 43
  • 1
  • 8
  • 1
    Floats and inline-blocks (as well as table-cells and flexible boxes) are fundamentally different things. All four techniques *may be* suitable for horizontal alignment of items, but they have different limitations and downsides. In theory, flexboxes seem to be the most universal way of the four, but browsers support of the latest spec version is still rather limited. – Ilya Streltsyn Jul 24 '13 at 05:23
0

Don't know why but I resolved this problem by adding border: 1px solid red;(vertical) and float: left;(horizontal) to related DIV style statement and white-spaces removed.

0

Parent div set to font-size: 0px and chiilds to wanted size like 17px :)

Gavo
  • 21
  • 3