7

I have a layout like this: Fiddle link

Between .td's is some white-space, even if margin and padding is set to 0.

Why is this happening and how to fix this? Negative margin-left maybe? Or any better solutions?

<style>
.tr {
    height: 20px;
    border: 1px solid black;
    overflow: hidden;
    white-space: nowrap;
    word-spacing: 0;
}
.td {
    display: inline-block;
    height: 20px;
    margin: 0;
    padding: 0;
}
</style>
<div class="tr" style="width: 150px;">
    <div class="td" style="width: 50px; background-color: #CCC;"></div>
    <div class="td" style="width: 50px; background-color: #AAA;"></div>
    <div class="td" style="width: 50px; background-color: #666;"></div>
</div>
Kristian
  • 3,283
  • 3
  • 28
  • 52

6 Answers6

12

Solution 1: Add comments:

<div class="tr" style="width: 150px;">
    <div class="td" style="width: 50px; background-color: #CCC;"></div><!--
    --><div class="td" style="width: 50px; background-color: #AAA;"></div><!--
    --><div class="td" style="width: 50px; background-color: #666;"></div>
</div>

You can write everything on the same line, too, but it looks cleaner with comments.


Solution 2: Add font-size:0 to the parent element. Don't forget to define the font-size for child elements:

.tr {
  font-size: 0;
}
.td {
  font-size: 15px;
}
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
2

use float

.td {
    float: left;
    height: 20px;
    margin: 0;
    padding: 0;
 }
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
  • I was using float, but it pushed third box to new line when width: 60px was applied. – Kristian Aug 16 '12 at 07:14
  • yes because the width of parent is less then the combine width of children so either you can increase width of parent or you can comment the extra space in the inline divs example: http://jsfiddle.net/bysMR/7/ – GajendraSinghParihar Aug 16 '12 at 07:21
0

The sapces/linebreakes between your inline-block elementes are displayed by the browser. You can remove them, but possible better use some sort of floating on your td class.

I am not sure what kind of data you are trying to display with your markup, but for tabular data the use of tables is perfectly finde! ;)

Jona
  • 2,087
  • 15
  • 24
0

Inline-blocks are treated as inline elements so empty spaces separate them as usual words in parent element.

I propose 2 solutions:

  1. You can fix it by removing spaces between inline-block divs, like this: http://jsfiddle.net/f3AKu/

  2. You can set font-size of container to 0 so spaces won't be noticeable. This way you won't modify HTML markup, only CSS rules. Example: http://jsfiddle.net/wC68h/

keaukraine
  • 5,315
  • 29
  • 54
0

To overcome this issue you have to hav your divs in one line:

<div class="td" style="width: 50px; background-color: #CCC;"></div><div class="td" style="width: 50px; background-color: #AAA;"></div><div class="td" style="width: 50px; background-color: #666;"></div>

Hope this helps

Razmig
  • 609
  • 1
  • 4
  • 10
0

My preferred way to accomplish zero space between inline-blocks was in CSS, with the parent having font-size:0 and child having font-size:16px. That said, there are a few other ways to do it with HTML and CSS. Here's a live pen:

http://codepen.io/chriscoyier/pen/hmlqF

MogSelf
  • 1
  • 3