从给定单元格开始rowspan和colspan并减少rowspan的填充

时间:2017-03-14 16:31:06

标签: html css

我正在编写一个动态创建HTML表格的PHP脚本。

有两件事情遗失,无法找到信息。

我想减少"填充"在行间隔单元格中,它在水平方向上太大了。您可以使用什么来修改单元格内部和结尾之间的间距?

我还想从3行到最后一行开始rowpan,并从第3列到最后一行启动colspan。可以这样做吗?

HTML源代码,如果你想看看表格是什么样的:



table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
}

<table style="width:50%">
  <tr>
    <th colspan="17">Weight, in Kg</th>
  </tr>
  <tr>
    <th rowspan="11">Height, in cm</th>
  </tr>
  <tr>
    <th></th>
    <th>40</th>
    <th>45</th>
    <th>50</th>
    <th>55</th>
    <th>60</th>
    <th>65</th>
    <th>70</th>
    <th>75</th>
    <th>80</th>
    <th>85</th>
    <th>90</th>
    <th>95</th>
    <th>100</th>
    <th>105</th>
    <th>110</th>
  </tr>
  <tr>
    <th>160</th>
    <td>15</td>
    <td>17</td>
    <td>19</td>
    <td>21</td>
    <td>23</td>
    <td>25</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>33</td>
    <td>35</td>
    <td>37</td>
    <td>39</td>
    <td>41</td>
    <td>42</td>
  </tr>
  <tr>
    <th>165</th>
    <td>14</td>
    <td>16</td>
    <td>18</td>
    <td>20</td>
    <td>22</td>
    <td>23</td>
    <td>25</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>33</td>
    <td>34</td>
    <td>36</td>
    <td>38</td>
    <td>40</td>
  </tr>
  <tr>
    <th>170</th>
    <td>13</td>
    <td>15</td>
    <td>17</td>
    <td>19</td>
    <td>20</td>
    <td>22</td>
    <td>24</td>
    <td>25</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>32</td>
    <td>34</td>
    <td>36</td>
    <td>38</td>
  </tr>
  <tr>
    <th>175</th>
    <td>13</td>
    <td>14</td>
    <td>16</td>
    <td>17</td>
    <td>19</td>
    <td>21</td>
    <td>22</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>32</td>
    <td>34</td>
    <td>35</td>
  </tr>
  <tr>
    <th>180</th>
    <td>12</td>
    <td>13</td>
    <td>15</td>
    <td>16</td>
    <td>18</td>
    <td>20</td>
    <td>21</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>30</td>
    <td>32</td>
    <td>33</td>
  </tr>
  <tr>
    <th>185</th>
    <td>11</td>
    <td>13</td>
    <td>14</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>20</td>
    <td>21</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>30</td>
    <td>32</td>
  </tr>
  <tr>
    <th>190</th>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td>15</td>
    <td>16</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td>22</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>30</td>
  </tr>
  <tr>
    <th>195</th>
    <td>10</td>
    <td>11</td>
    <td>13</td>
    <td>14</td>
    <td>15</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>21</td>
    <td>22</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>28</td>
  </tr>
  <tr>
    <th>200</th>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>20</td>
    <td>21</td>
    <td>22</td>
    <td>23</td>
    <td>25</td>
    <td>26</td>
    <td>27</td>
  </tr>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

创建一个2x2 “填充”单元格。然后在<tr>之后启动列标签单元格<tr>。您基本上有两个水平运行的标题行,因此在第三个table, th, td { border: 1px solid black; border-collapse: collapse; }中开始垂直标题行。

<table style="width:50%">
  <tr>
    <th colspan="2" rowspan="2"></th>
    <th colspan="17">Weight, in Kg</th>
  </tr>
  <tr>
    <th>40</th>
    <th>45</th>
    <th>50</th>
    <th>55</th>
    <th>60</th>
    <th>65</th>
    <th>70</th>
    <th>75</th>
    <th>80</th>
    <th>85</th>
    <th>90</th>
    <th>95</th>
    <th>100</th>
    <th>105</th>
    <th>110</th>
  </tr>
  <tr>
    <th rowspan="11">Height, in cm</th>
    <th>160</th>
    <td>15</td>
    <td>17</td>
    <td>19</td>
    <td>21</td>
    <td>23</td>
    <td>25</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>33</td>
    <td>35</td>
    <td>37</td>
    <td>39</td>
    <td>41</td>
    <td>42</td>
  </tr>
  <tr>
    <th>165</th>
    <td>14</td>
    <td>16</td>
    <td>18</td>
    <td>20</td>
    <td>22</td>
    <td>23</td>
    <td>25</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>33</td>
    <td>34</td>
    <td>36</td>
    <td>38</td>
    <td>40</td>
  </tr>
  <tr>
    <th>170</th>
    <td>13</td>
    <td>15</td>
    <td>17</td>
    <td>19</td>
    <td>20</td>
    <td>22</td>
    <td>24</td>
    <td>25</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>32</td>
    <td>34</td>
    <td>36</td>
    <td>38</td>
  </tr>
  <tr>
    <th>175</th>
    <td>13</td>
    <td>14</td>
    <td>16</td>
    <td>17</td>
    <td>19</td>
    <td>21</td>
    <td>22</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>31</td>
    <td>32</td>
    <td>34</td>
    <td>35</td>
  </tr>
  <tr>
    <th>180</th>
    <td>12</td>
    <td>13</td>
    <td>15</td>
    <td>16</td>
    <td>18</td>
    <td>20</td>
    <td>21</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>30</td>
    <td>32</td>
    <td>33</td>
  </tr>
  <tr>
    <th>185</th>
    <td>11</td>
    <td>13</td>
    <td>14</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>20</td>
    <td>21</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>30</td>
    <td>32</td>
  </tr>
  <tr>
    <th>190</th>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td>15</td>
    <td>16</td>
    <td>18</td>
    <td>19</td>
    <td>20</td>
    <td>22</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>29</td>
    <td>30</td>
  </tr>
  <tr>
    <th>195</th>
    <td>10</td>
    <td>11</td>
    <td>13</td>
    <td>14</td>
    <td>15</td>
    <td>17</td>
    <td>18</td>
    <td>19</td>
    <td>21</td>
    <td>22</td>
    <td>23</td>
    <td>24</td>
    <td>26</td>
    <td>27</td>
    <td>28</td>
  </tr>
  <tr>
    <th>200</th>
    <td>10</td>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td>15</td>
    <td>16</td>
    <td>17</td>
    <td>18</td>
    <td>20</td>
    <td>21</td>
    <td>22</td>
    <td>23</td>
    <td>25</td>
    <td>26</td>
    <td>27</td>
  </tr>
</table>
letter-spacing: 0.25em;

当您说“padding”时,我将其理解为 Wight,Kg 两侧的空白区域。如果是这样的话:

  1. 增加字体大小,这可能看起来很难看。
  2. 增加letter spacingtable, th, td { border: 1px solid black; border-collapse: collapse; text-align: center; } th, td { min-width: 1.85rem; },这可能看起来很难看。
  3. 如果你在 100 105 110 列中的数字之后谈论空格,你可以在上面做,但我建议让所有列的宽度相同,然后将所有数字居中,以便将所有数据都集中在一起。

    请注意,使用此方法时,三位数的空白空间少于两位数。

    <table style="width:50%">
      <tr>
        <th colspan="2" rowspan="2"></th>
        <th colspan="17">Weight, in Kg</th>
      </tr>
      <tr>
        <th>40</th>
        <th>45</th>
        <th>50</th>
        <th>55</th>
        <th>60</th>
        <th>65</th>
        <th>70</th>
        <th>75</th>
        <th>80</th>
        <th>85</th>
        <th>90</th>
        <th>95</th>
        <th>100</th>
        <th>105</th>
        <th>110</th>
      </tr>
      <tr>
        <th rowspan="11">Height, in cm</th>
        <th>160</th>
        <td>15</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
        <td>23</td>
        <td>25</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>33</td>
        <td>35</td>
        <td>37</td>
        <td>39</td>
        <td>41</td>
        <td>42</td>
      </tr>
      <tr>
        <th>165</th>
        <td>14</td>
        <td>16</td>
        <td>18</td>
        <td>20</td>
        <td>22</td>
        <td>23</td>
        <td>25</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>33</td>
        <td>34</td>
        <td>36</td>
        <td>38</td>
        <td>40</td>
      </tr>
      <tr>
        <th>170</th>
        <td>13</td>
        <td>15</td>
        <td>17</td>
        <td>19</td>
        <td>20</td>
        <td>22</td>
        <td>24</td>
        <td>25</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>32</td>
        <td>34</td>
        <td>36</td>
        <td>38</td>
      </tr>
      <tr>
        <th>175</th>
        <td>13</td>
        <td>14</td>
        <td>16</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
        <td>22</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>32</td>
        <td>34</td>
        <td>35</td>
      </tr>
      <tr>
        <th>180</th>
        <td>12</td>
        <td>13</td>
        <td>15</td>
        <td>16</td>
        <td>18</td>
        <td>20</td>
        <td>21</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>30</td>
        <td>32</td>
        <td>33</td>
      </tr>
      <tr>
        <th>185</th>
        <td>11</td>
        <td>13</td>
        <td>14</td>
        <td>16</td>
        <td>17</td>
        <td>18</td>
        <td>20</td>
        <td>21</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>30</td>
        <td>32</td>
      </tr>
      <tr>
        <th>190</th>
        <td>11</td>
        <td>12</td>
        <td>13</td>
        <td>15</td>
        <td>16</td>
        <td>18</td>
        <td>19</td>
        <td>20</td>
        <td>22</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>30</td>
      </tr>
      <tr>
        <th>195</th>
        <td>10</td>
        <td>11</td>
        <td>13</td>
        <td>14</td>
        <td>15</td>
        <td>17</td>
        <td>18</td>
        <td>19</td>
        <td>21</td>
        <td>22</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>28</td>
      </tr>
      <tr>
        <th>200</th>
        <td>10</td>
        <td>11</td>
        <td>12</td>
        <td>13</td>
        <td>15</td>
        <td>16</td>
        <td>17</td>
        <td>18</td>
        <td>20</td>
        <td>21</td>
        <td>22</td>
        <td>23</td>
        <td>25</td>
        <td>26</td>
        <td>27</td>
      </tr>
    </table>
    table,
    th,
    td {
      border: 1px solid black;
      border-collapse: collapse;
      text-align: center;
    }
    th,
    td {
      padding: 0.125rem 0.25rem;
    }

    或者您可以在列和中心设置特定数量的填充。我个人喜欢上面代码片段的结果。在这里,您可以获得一致的空白数字,同时牺牲相等的列宽。

    <table style="width:50%">
      <tr>
        <th colspan="2" rowspan="2"></th>
        <th colspan="17">Weight, in Kg</th>
      </tr>
      <tr>
        <th>40</th>
        <th>45</th>
        <th>50</th>
        <th>55</th>
        <th>60</th>
        <th>65</th>
        <th>70</th>
        <th>75</th>
        <th>80</th>
        <th>85</th>
        <th>90</th>
        <th>95</th>
        <th>100</th>
        <th>105</th>
        <th>110</th>
      </tr>
      <tr>
        <th rowspan="11">Height, in cm</th>
        <th>160</th>
        <td>15</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
        <td>23</td>
        <td>25</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>33</td>
        <td>35</td>
        <td>37</td>
        <td>39</td>
        <td>41</td>
        <td>42</td>
      </tr>
      <tr>
        <th>165</th>
        <td>14</td>
        <td>16</td>
        <td>18</td>
        <td>20</td>
        <td>22</td>
        <td>23</td>
        <td>25</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>33</td>
        <td>34</td>
        <td>36</td>
        <td>38</td>
        <td>40</td>
      </tr>
      <tr>
        <th>170</th>
        <td>13</td>
        <td>15</td>
        <td>17</td>
        <td>19</td>
        <td>20</td>
        <td>22</td>
        <td>24</td>
        <td>25</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>32</td>
        <td>34</td>
        <td>36</td>
        <td>38</td>
      </tr>
      <tr>
        <th>175</th>
        <td>13</td>
        <td>14</td>
        <td>16</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
        <td>22</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>31</td>
        <td>32</td>
        <td>34</td>
        <td>35</td>
      </tr>
      <tr>
        <th>180</th>
        <td>12</td>
        <td>13</td>
        <td>15</td>
        <td>16</td>
        <td>18</td>
        <td>20</td>
        <td>21</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>30</td>
        <td>32</td>
        <td>33</td>
      </tr>
      <tr>
        <th>185</th>
        <td>11</td>
        <td>13</td>
        <td>14</td>
        <td>16</td>
        <td>17</td>
        <td>18</td>
        <td>20</td>
        <td>21</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>30</td>
        <td>32</td>
      </tr>
      <tr>
        <th>190</th>
        <td>11</td>
        <td>12</td>
        <td>13</td>
        <td>15</td>
        <td>16</td>
        <td>18</td>
        <td>19</td>
        <td>20</td>
        <td>22</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>29</td>
        <td>30</td>
      </tr>
      <tr>
        <th>195</th>
        <td>10</td>
        <td>11</td>
        <td>13</td>
        <td>14</td>
        <td>15</td>
        <td>17</td>
        <td>18</td>
        <td>19</td>
        <td>21</td>
        <td>22</td>
        <td>23</td>
        <td>24</td>
        <td>26</td>
        <td>27</td>
        <td>28</td>
      </tr>
      <tr>
        <th>200</th>
        <td>10</td>
        <td>11</td>
        <td>12</td>
        <td>13</td>
        <td>15</td>
        <td>16</td>
        <td>17</td>
        <td>18</td>
        <td>20</td>
        <td>21</td>
        <td>22</td>
        <td>23</td>
        <td>25</td>
        <td>26</td>
        <td>27</td>
      </tr>
    </table>
    end_date

    如果这些都不是你所引用的,请澄清。