简体   繁体   English

如何从css表中删除边距和填充

[英]How to remove margin and padding from css table

I want to remove the spacing between the 2 rows. 我想删除2行之间的间距。 The table is a css only table. 该表是仅限css的表。

JS Fiddle: https://jsfiddle.net/n841wvwn/ JS小提琴: https//jsfiddle.net/n841wvwn/

CSS: CSS:

* {
    padding: 0px;
    margin: 0px;
}

div.table {
    display: table;
    border-collapse: collapse;
    table-layout:fixed;
}

div.tr {
    display: table-row;
}

div.td {
    display: table-cell;
    padding:0; margin:0;
}

.button {
    width: 88px;
    height: 55px;
    border: none;
    margin: 0px;
    padding: 0px;
}



#ac {
    background-image: url('http://i.imgur.com/u9znwQK.png');
    background-size: 100%;
}

#ce {
    background-image: url('http://i.imgur.com/azae5uQ.png');
    background-size: 100%;
}

#multiplication {
    background-image: url('http://i.imgur.com/veBf82o.png');
    background-size: 100%;
}


#division {
    background-image: url('http://i.imgur.com/i6KcXGr.png');
    background-size: 100%;
}


#number7 {
    background-image: url('http://i.imgur.com/0GixHJ5.png');
    background-size: 100%;
}

HTML: HTML:

<div class="container">
        <div class="table" id="calculator">
            <div class="tr">
                <div class="td" id="display"></div>
            </div>
            <div class="tr">
                <div class="td">
                    <button class="button" id="ac"></button>
                </div>
                <div class="td">
                    <button class="button" id="ce"></button>
                </div>
                <div class="td">
                    <button class="button" id="division"></button>
                </div>
                <div class="td">
                    <button class="button" id="multiplication"></button>
                </div>
            </div>
            <div class="tr">
                <div class="td">
                    <button class="button" id="number7"></button>
                </div>
            </div>
        </div>
    </div>

Between this circle I want no spacing. 在这个圆圈之间我想要没有间距。 http://i.imgur.com/a3wMyLA.png http://i.imgur.com/a3wMyLA.png

The layout is based on a table div, which has table-layout fixed and display table. 该布局基于一个表格div,它具有固定的表格布局和显示表格。 Also I have border-collapse set to collapse and every padding and margin to 0px. 此外,我将边框折叠设置为折叠,每个填充和边距为0px。

For some reason the button is 55 px but the div is 59 pixel in height. 由于某种原因,按钮为55像素,但div的高度为59像素。

add font-size:0; 添加font-size:0; as below : 如下 :

div.td {
  display: table-cell;
  padding:0; margin:0;
  font-size:0;
}

This will solve your problem. 这将解决您的问题。 But this will only work if u do not have any text inside the td element like in the case u showed. 但是只有在你显示的情况下你没有在td元素中有任何文本时,这才有效。

Apparently, it is due to the line-height of the table-cell . 显然,这是由于table-cellline-height You can explicitly set it to 0.8 to solve the problem: 您可以将其明确设置为0.8以解决问题:

div.td {
    line-height: 0.8;
}

This happens only in Chrome and IE (not in Firefox, interesting...), and you can fix it on this way, too: 这只发生在Chrome和IE浏览器中(不在Firefox中,有趣......),您也可以通过这种方式修复它:

.button {
  display:block;
}

So, button is by default - inline element (and that's the cause of this small gap), and this way it will fill 100% height... 因此,默认情况下,按钮是 - 内联元素 (这是这个小间隙的原因),这样它将填充100%的高度......

https://jsfiddle.net/n841wvwn/12/ https://jsfiddle.net/n841wvwn/12/

As an alternative to all the solutions given, you can also align the buttons to the top of the table cells. 作为所有解决方案的替代方案,您还可以将按钮对齐到表格单元格的顶部。 That will work as well. 那也行。

.button {
  width: 88px;
  height: 55px;
  border: none;
  margin: 0px;
  padding: 0px;
  vertical-align: top; /* added */
}

 * { padding: 0px; margin: 0px; } div.table { display: table; border-collapse: collapse; table-layout: fixed; } div.tr { display: table-row; } div.td { display: table-cell; padding: 0; margin: 0; } .button { width: 88px; height: 55px; border: none; margin: 0px; padding: 0px; vertical-align: top; /* added */ } #ac { background-image: url('http://i.imgur.com/u9znwQK.png'); background-size: 100%; } #ce { background-image: url('http://i.imgur.com/azae5uQ.png'); background-size: 100%; } #multiplication { background-image: url('http://i.imgur.com/veBf82o.png'); background-size: 100%; } #division { background-image: url('http://i.imgur.com/i6KcXGr.png'); background-size: 100%; } #number7 { background-image: url('http://i.imgur.com/0GixHJ5.png'); background-size: 100%; } 
 <div class="container"> <div class="table" id="calculator"> <div class="tr"> <div class="td" id="display"></div> </div> <div class="tr"> <div class="td"> <button class="button" id="ac"></button> </div> <div class="td"> <button class="button" id="ce"></button> </div> <div class="td"> <button class="button" id="division"></button> </div> <div class="td"> <button class="button" id="multiplication"></button> </div> </div> <div class="tr"> <div class="td"> <button class="button" id="number7"></button> </div> </div> </div> </div> 

Update your css as follows 按如下方式更新您的CSS

 #number7 {
        background-image: url('http://i.imgur.com/0GixHJ5.png');
        background-size: 100%;
            position:relative;
            top:-4px;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM