简体   繁体   English

CSS和表格边框问题

[英]Issue with CSS and table borders

I am trying to make a grid with a blue background that has 10x10 tiles. 我正在尝试制作一个具有10x10瓷砖的蓝色背景的网格。 All the tiles need to be square, and they need to be filled with blue, with a small black line seperating each tile. 所有图块都必须是正方形的,并且需要用蓝色填充,并用细黑线分隔每个图块。 When I am formatting the table in CSS, it has edges that are too wide, which is an a minor, yet fairly irratating issue. 当我在CSS中格式化表格时,它的边缘太宽,这是一个较小的问题,但很恼人。 I cannot see what the issue is, can anyone else? 我看不出问题是什么,其他人可以吗?

 var boatStatusClient = ""; var x_client = 0; var y_client = 0; var battlefield_client = ""; var source_client; var boatGrid = { placeBoat_client: function() { source_client = event.target || event.srcElement; source_client = source_client.id source_client.id = document.getElementById(source_client.id); document.getElementById(source_client).style.backgroundColor = "orange"; }, } for (y_client = 1; y_client < 11; y_client++) { battlefield_client += "<tr>"; for (x_client = 1; x_client < 11; x_client++) { battlefield_client += "<td onclick = '' class = 'tile' style='border: 3px solid black;' id=" + "cell_client_" + x_client + "_" + y_client + ">&nbsp</td>"; } battlefield_client += "</tr>"; } $(document).ready(function() { $("#tableGrid_client").html(battlefield_client); //loads table for (y_client = 1; y_client < 11; y_client++) { for (x_client = 1; x_client < 11; x_client++) { boatStatusClient = document.getElementById('cell_client_' + x_client + "_" + y_client); boatStatusClient.addEventListener("click", function() { boatGrid.placeBoat_client() }); } } }); 
 table { border-collapse: collapse; border: none; } .tile { background-color: #34B0D9; cursor: pointer; } .tile:hover { background-color: #6fcdec; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="hideGames"> <table style="position:absolute; top: 20px; left: 20px; border:6px solid #ff5050; width: 500px; height: 500px;" id="tableGrid_client"></table> 

You just need to add table-layout:fixed to your table 您只需要添加table-layout:fixed到您的table

fixed 固定

Table and column widths are set by the widths of table and col elements or by the width of the first row of cells. 表和列的宽度由table和col元素的宽度或单元格第一行的宽度设置。 Cells in subsequent rows do not affect column widths. 后续行中的单元格不会影响列宽。

Under the "fixed" layout method, the entire table can be rendered once the first table row has been downloaded and analyzed. 在“固定”布局方法下,一旦下载并分析了第一行表,就可以呈现整个表。 This can speed up rendering time over the "automatic" layout method, but subsequent cell content may not fit in the column widths provided. 与“自动”布局方法相比,这可以加快渲染时间,但是后续的单元格内容可能不适合所提供的列宽。 Any cell that has content that overflows uses the overflow property to determine whether to clip the overflow content. 任何具有溢出内容的单元格都将使用溢出属性来确定是否剪辑溢出内容。

Note: avoid inline styles. 注意:避免使用内联样式。

  var boatStatusClient = ""; var x_client = 0; var y_client = 0; var battlefield_client = ""; for (y_client = 1; y_client < 11; y_client++) { battlefield_client += "<tr>"; for (x_client = 1; x_client < 11; x_client++) { battlefield_client += "<td onclick = '' class = 'tile' style='border: 3px solid black;' id=" + "cell_client_" + x_client + "_" + y_client + ">&nbsp</td>"; } battlefield_client += "</tr>"; } $(document).ready(function() { $("#tableGrid_client").html(battlefield_client); //loads table for (y_client = 1; y_client < 11; y_client++) { for (x_client = 1; x_client < 11; x_client++) { boatStatusClient = document.getElementById('cell_client_' + x_client + "_" + y_client); boatStatusClient.addEventListener("click", function() { boatGrid.placeBoat_client() }); } } }); 
 body { font-size: 118%; font-family: calibri light; background-color: #E8E8E8 } table { border-collapse: collapse; border: none; table-layout: fixed; position: absolute; top: 20px; left: 20px; border: 6px solid #ff5050; width: 500px; height: 500px; } .tile { background-color: #34B0D9; cursor: pointer; } .tile:hover { background-color: #6fcdec; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <body> <!--START OF GAMES PART--> <div class="hideGames"> <table style="" id="tableGrid_client"></table> </div> <!--END OF GAMES PART --> 

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

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