Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let's say that I have two <div> elements, (or can be <li>) which has style attribute = "margin: 0; display: inline-block;". If there is space between <div> elements, like:
HTML
<div style="margin: 0; display: inline-block;">first div</div>
<div style="margin: 0; display: inline-block;">second div</div>

(In this case second element is described in different line), than still there is about 2-3x space between them. But when I change html code to look like this:
HTML
<div style="margin: 0; display: inline-block;">first div</div><div style="margin: 0; display: inline-block;">second div</div>


Everything is like it should be. Probably I should set up some attributes for the whole body? Why does it happen and how to avoid those spaces?
Posted
Updated 29-Sep-13 4:13am
v2

1 solution

Quote:
Why does it happen

Your browser treats the line break as whitespace: not as a line break, but as a space.
A simple example to prove this:
HTML
a
b

The above code returns a b in the browser, however, if you remove the line break, then the code is this:
HTML
ab

And the above code returns ab in the browser.

Quote:
how to avoid those spaces?

Add float: left; to every style attribute:
XML
<div style="margin: 0; display: inline-block; float: left;">first div</div>
<div style="margin: 0; display: inline-block; float: left;">second div</div>


Note: I suggest to create a CSS class instead of using the style attribute.
In your <head> tag, add this <style> tag:
HTML
<!-- in your head-tag -->
<style type="text/css"> 
  div.nospaces {
      margin: 0;
      display: inline-block;
      float: left;
  }
</style>

And now when creating your div, use class="nospaces" instead of style="...":
HTML
<div class="nospaces">first div</div>
<div class="nospaces">second div</div>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900