简体   繁体   English

CSS 填充溢出 HTML 表,即使有边框大小但没有溢出其他任何东西

[英]CSS padding overflowing HTML table even with border-box sizing but not overflowing anything else

I've been reading non-stop about sizing and I still cannot seem to apply it to my code.我一直在不停地阅读有关调整大小的内容,但似乎仍然无法将其应用于我的代码。 I figured out that padding was causing my HTML table to overflow, but that it can be stopped with box-sizing: border-box;我发现填充导致我的 HTML 表溢出,但可以通过 box-sizing:border-box; 来停止它。

So I added border-box to the elements I thought needed it, then to everything, trying to change width settings, but to no avail.因此,我将边框框添加到我认为需要它的元素中,然后添加到所有内容中,尝试更改宽度设置,但无济于事。 When I remove border-box my header and navbar are affected, but the html table overflows the screen no matter what.当我删除边框框时,我的 header 和导航栏会受到影响,但无论如何 html 表都会溢出屏幕。

All my elements seem to fit on the screen perfectly fine, except the table.我的所有元素似乎都非常适合屏幕,除了桌子。 I assume that it is something I added that it countering the box sizing, but I commented out lines one by one and didn't find anything.我认为这是我添加的内容,它与盒子大小相反,但我一一注释掉了行并且没有找到任何东西。

I put the full code into fiddle: https://jsfiddle.net/atpzw32L/我把完整的代码放入小提琴: https://jsfiddle.net/atpzw32L/

The CSS in question:有问题的 CSS:

html, body, h1 {
    margin: 0;
    padding: 0;

}

html, body { min-height: 100%; }

  
body{
    background-image: url("agaricus.png");
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    font-size: 100%;
    padding: 20px;
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
    margin: 0;
    width: 100%;

}


  /* Style the header */
  .header {
    padding: 10px;
    text-align: center;
    background: #832bfd7c;
    color: white;
  }
  
  /* Increase the font size of the h1 element */
  .header h1 {
    font-size: 40px;
  }
  
  /* Style the top navigation bar */
  .navbar {
    overflow: hidden;
    background-color: #333;

    margin-bottom: 20px;
  }
  
  /* Style the navigation bar links */
  .navbar a {
    float: left;
    display: block;
    color: white;
    text-align: center;
    padding: 14px 20px;
    text-decoration: none;
  }
  
  /* Right-aligned link */
  .navbar a.right {
    float: right;
  }
  
  /* Change color on hover */
  .navbar a:hover {
    background-color: #ddd;
    color: black;
  }







/* CSS to hide key columns */
th:nth-child(4), th:nth-child(5), th:nth-child(6), th:nth-child(8), th:nth-child(11), th:nth-child(12), th:nth-child(13), th:nth-child(15) {
    display: none
}
td:nth-child(4), td:nth-child(5), td:nth-child(6), td:nth-child(8), td:nth-child(11), td:nth-child(12), td:nth-child(13), td:nth-child(15) {
    display: none
}


/* CSS for checkboxes*/

.dropdown-check-list {
    display: inline-block;
    position: relative;
    font-family: Verdana, sans-serif;
    padding: 5px;
    margin-top: 10px;
    background-color: rgb(255, 255, 255);


}

.dropdown-check-list .anchor {
    position: relative;
    cursor: pointer;
    display: inline-block;
    padding: 5px 50px 5px 10px;
    border: 1px solid #ccc;

    min-width: 100px;
}

.dropdown-check-list .anchor:after {
    position: absolute; 
    content: "";
    border-left: 2px solid black;
    border-top: 2px solid black;
    padding: 5px;
    right: 10px;
    top: 20%;
    -moz-transform: rotate(-135deg);
    -ms-transform: rotate(-135deg);
    -o-transform: rotate(-135deg);
    -webkit-transform: rotate(-135deg);
    transform: rotate(-135deg);
}

.dropdown-check-list .anchor:active:after {
    right: 8px;
    top: 21%;
}

.dropdown-check-list ul.items {
    padding: 5px;
    display: none;
    margin: none;
    border: 1px solid #ccc;
    border-top: none;

    position: absolute;
    background-color: #f1f1f1;
}

.dropdown-check-list ul.items li {
    list-style: none;
    padding: 5px;
}

.dropdown-check-list.visible .anchor {
    color: #0094ff;
}

.dropdown-check-list.visible .items {
    display: block;
}

input {
    float: right;
    height: 18px;
    width: 18px;
}



/* CSS for table */


table, th, td {
    border: 2px solid;
    box-sizing: border-box;
}

table {
    border-collapse: collapse;
    margin-top: 30px;
    font-family: Verdana, sans-serif;
    padding: 15px;
    background-color: rgb(255, 255, 255);
}

th{
    text-align: center;
    height: 80px;
    vertical-align: bottom;
    padding: 2px;
    border-bottom: 4px solid rgb(0, 0, 0);
    font-size: 16px;
    background-color: rgba(4, 100, 156, 0.278);
}

td {
    text-align: center;
    height: 100px;
    width: 90%;
    padding: 15px;

}

/* th{
    text-align: center;
    height: 100px;
    vertical-align: bottom;
    padding: 15px;
    border-bottom: 4px solid rgb(0, 0, 0);
    font-size: 16px;
    background-color: rgba(4, 100, 156, 0.278);

} */
/* 
td{
    text-align: center;
    height: 100px;
    vertical-align: left;
    padding: 15px;
    font-size: 15px;
}  */


tr:hover {background-color: rgba(4, 100, 156, 0.089);}

I aim for my end result to have all columns visible, and the table fitting within the screen, so when I add padding to the table it will fit within the screen.我的目标是让所有列都可见,并且表格适合屏幕,所以当我向表格添加填充时,它会适合屏幕。 I would also like to keep the padding within th and td.我还想将填充保持在 th 和 td 内。

Please let me know how to go about this, I am out of ideas.请让我知道如何 go 关于这个,我没有想法。 Thank you谢谢

Concerning the width and the horizontal overflow of your table, this is simply caused by its contents: The cells (for example the header cells) contain (single) words that stretch the width of those cells (and therefore the columns) to fit into them, and so the overall width of the table becomes as wide as it is.关于表格的宽度和水平溢出,这仅仅是由它的内容引起的:单元格(例如 header 单元格)包含(单个)单词,这些单词会拉伸这些单元格(以及列)的宽度以适应它们,所以表格的整体宽度变得和它一样宽。 Examples are the words "description", "information" and "identification" in the header cells.例如 header 单元格中的“描述”、“信息”和“标识”。

Words won't be divided automatically, so those wordlenghts determine your minimum widths, expanding the column width and therefore the overall width of the table.单词不会自动划分,因此这些 wordlenghts 确定您的最小宽度,扩大列宽,从而扩大表格的整体宽度。

There are three methods to change that:有三种方法可以改变它:

1.) (IMO the best) Use a smaller font size for the text. 1.) (IMO 最好)使用较小的文本字体 That way those longer words and with them the cells and columns won't be that wide and (maybe) fit into the screen.这样,那些较长的单词以及它们的单元格和列就不会那么宽并且(也许)适合屏幕。

2.) (also okay) Use hyphenation : Apply hyphens: auto; 2.)(也可以)使用连字符:应用hyphens: auto; to td and th .tdth The automatic hyphenation will divide words at know possible hyphenation positions.自动断字将在已知可能的断字位置划分单词。

3.) (IMO a bad solution) Apply word-break: break-word; 3.)(IMO 一个不好的解决方案)应用word-break: break-word; to the cells.到细胞。 This will divide longer words, but at "incorrect" positions, ie simply at the end of the given cell width.这将分割较长的单词,但在“不正确”的位置,即只是在给定单元格宽度的末尾。 As I wrote, not good, only to be used if nothing else works.正如我所写,不好,只有在没有其他方法的情况下才能使用。

I would combine methods #1 and #2 .我会结合方法 #1 和 #2

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

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