0

I am using Fluid web design taking widths in "Percentages". I also reset all Margins and Paddings to default 0px. There is no border in any div. Still, why be default these are not aligned in same line???

Example:

<html> 
<head>
<style>
*{margin:0px;padding:0px;}
div{display:inline-block;}
.divOne{background:yellow;width:20%;}
.divTwo{background:green;width:60%;}
.divThree{background:red;width:20%;}
</style> 
</head>
<body> 

<div class="divOne">One</div>
<div class="divTwo">Two</div>
<div class="divThree">Three</div>

</body> 
</html> 

Please, someone help me out. How in world is this not aligned in same line. But if I reduce % of any of these to go below 100% (like 98% or less), then these get aligned. But, if there is no margin, no padding. Why is this gap of width required?

Deadpool
  • 6,416
  • 5
  • 32
  • 64
  • Just drop that old `inline-block` hassle and embrace [Flex almighty](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) – Jeremy Thille Dec 11 '17 at 08:34
  • I've provided a practical demonstration on how to remove "white spaces" between `inline-block` elements, with explanations on why this occurs and alternative methods worth exploring (spoiler: they *don't* include `flex-box`). Take a look: https://codepen.io/UncaughtTypeError/pen/ypBMXB – UncaughtTypeError Dec 11 '17 at 08:44

3 Answers3

1

You need to add HTML comments between your divs to cancel the whitespace interpreted by the browser :

*{margin:0px;padding:0px;}
div{display:inline-block;}
.divOne{background:yellow;width:20%;}
.divTwo{background:green;width:60%;}
.divThree{background:red;width:20%;}
<div class="divOne">One</div><!--
--><div class="divTwo">Two</div><!--
--><div class="divThree">Three</div>
Zenoo
  • 11,719
  • 4
  • 38
  • 57
1

Weird space that inline-blocks give. https://css-tricks.com/fighting-the-space-between-inline-block-elements/

You can have those three divs in one row to avoid any hacks/tricks.

https://jsfiddle.net/dghkcg04/

<div class="divOne">One</div><div class="divTwo">Two</div><div class="divThree">Three</div>
Gezzasa
  • 1,177
  • 6
  • 17
0

The browser interprete de div like a white space so doing this to get it:

<!DOCTYPE html>
<html> 
<head>
<style>
*{
    margin:0;
    padding:0;
}
div{
display:inline-block;
margin:0;
padding:0;
}
.divOne{background:yellow;width:20%;}
.divTwo{background:green;width:60%;}
.divThree{background:red;width:20%;}
</style> 
</head>
<body> 

<div class="divOne">One</div><div class="divTwo">Two</div><div class="divThree">Three</div>

</body> 
</html>