简体   繁体   English

为什么我的表格高度不会缩小到 170 像素以下?

[英]Why won't my table shrink below 170px in height?

I have the following code:我有以下代码:

 #superheader { width: auto; height: 100px; position: relative; } #superheader p { font-family: Sansation; font-size: 50px; } #superheader table { border: solid; height: 100px; } #header { height: 140px; box-shadow: 0em 0.5em 0.5em grey; position: relative; background-color: #E6E6E6; padding-left: 70px; padding-right: 70px; font-family: sans-serif; } .menuelement { width: 100px; height: 40px; text-align: center; padding-left: 10px; padding-right: 10px; float: left; position: relative; } .menuelement:hover { background-color: dimgrey; } #header>a { line-height: 2.5; }
 <div id="header"> <div id="superheader"> <div style="margin: auto; width: 630px;"> <table> <tr> <td><img src="images/memoji.png" width="100px" height="100px" alt="Icon"></td> <td> <p>A TITLE</p> </td> </tr> </table> </div> </div> <a href="template.html"> <div class="menuelement">Page1</div> </a> <a href="index.html"> <div class="menuelement">Page2</div> </a> <a href="not_found.html"> <div class="menuelement">Page3</div> </a> </div>

The table in this example should be 100px in height, however it won't get smaller than 170px.此示例中的表格高度应为 100 像素,但不会小于 170 像素。

Where is my mistake?我的错误在哪里?

Add the border-spacing: 0px;添加border-spacing: 0px; to your table style to remove spaces between cells:到您的表格样式以删除单元格之间的空格:

#superheader table{
    border: solid;
    height: 100px;
    border-spacing: 0px;
}

and then remove white spaces inside cells:然后删除单元格内的空格:

td, td *{
   display: table-cell;
   margin: 0px;
   padding: 0px;
}

Well, actually you can't control height or width of the elements with display: table;好吧,实际上你不能用display: table;控制元素的heightwidth display: table; because by default if their cells height or width is larger than the height or width of the element with display: table;因为默认情况下,如果它们的单元格heightwidth大于带有display: table;的元素的heightwidth display: table; the element will grow.元素会增长。 So you need to control the dimension of the cells instead of the table itself.所以你需要控制单元格的维度而不是表格本身。

In your particular case, there are two approaches to overcome this situation.在您的特定情况下,有两种方法可以克服这种情况。

  1. Controlling the cells: since you used p tag in your table and this tag has a default margin of 1em on top and bottom you can simply override it and because the inner cells are no longer larger than the table itself you can control its dimensions (As long as they do not smaller than cells).控制单元格:由于您在表格中使用了p标签,并且此标签在顶部和底部的默认边距为1em ,您可以简单地覆盖它,并且由于内部单元格不再大于表格本身,因此您可以控制其尺寸(如只要它们不小于细胞)。 Also, keep in mind the font-size and line-height will make your cells bigger than usual, but if you want to keep them as it is, you should drop the default margin.另外,请记住font-sizeline-height会使您的单元格比平时更大,但如果您想保持原样,您应该删除默认边距。

     p { font-family: Sansation; font-size: 50px; margin: 0; } table { border: solid; height: 100px; }
     <table> <tr> <td><img src="images/memoji.png" alt="Icon"></td> <td> <p>A TITLE</p> </td> </tr> </table>

  2. Using display: block;使用display: block; : You can simply override the display of the table and make it work as block but you still need to control the inner elements to do not override the table itself. :您可以简单地覆盖表格的display并使其作为block工作,但您仍然需要控制内部元素以不覆盖表格本身。 Also, keep in mind you should control the width of the table also because block element by default will fill the available space.另外,请记住,您还应该控制表格的width ,因为默认情况下块元素将填充可用空间。

     p { font-family: Sansation; font-size: 50px; margin: 0; } table { border: solid; height: 100px; display: block; }
     <table> <tr> <td><img src="images/memoji.png" alt="Icon"></td> <td> <p>A TITLE</p> </td> </tr> </table>

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

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