HTML - 如何使图像填充各种大小的表格单元格

标签 html css html-table image-resizing

我正在为一个学校元素制作一个网站,并实现了一个包含图像的表格,这些图像应该调整大小以完全填满每个表格单元格。这在大多数情况下都按预期工作,但是,当表格单元格的 rowspan > 1 时,图像不会按预期调整大小。 colspan > 1 时不会出现此问题。我们将不胜感激。

相关代码如下:

th {
  width: 200px;
  height: 200px;
  padding: 0;
}

.grid_img {
  width: 100%;
  height: 100%;
  display: table-header-group;
}
<table cellspacing="30">
  <tr>
    <th class="box"><img id="img_01" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_1.jpg"></th>
    <th rowspan="2"><img id="img_02" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_2.jpg"></th>
    <th class="box"><img id="img_03" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_3.jpg"></th>
    <th class="box"><img id="img_04" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_4.jpg"></th>
    <th class="box"><img id="img_05" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_5.jpg"></th>
  </tr>
  <tr>
    <th class="box"><img id="img_06" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_6.jpg"></th>
    <th class="long_box" colspan="2"><img id="img_07" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_7.jpg"></th>
    <th class="box"><img id="img_08" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_8.jpg"></th>
  </tr>
  <tr>
    <th class="long_box" colspan="2"><img id="img_09" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_9.jpg"></th>
    <th class="box"><img id="img_10" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_10.jpg"></th>
    <th class="long_box" colspan="2"><img id="img_11" class="grid_img" src="https://asherwolfphotography.000webhostapp.com/images/image_11.jpg"></th>
  </tr>
</table>

最佳答案

试试这个

将图片设置为标签的背景

th {
  min-width: 200px;
  width: 200px;
  height: 200px;
  padding: 0;
  background-repeat: no-repeat !important;
  background-size: cover !important;
}
<table cellspacing="30">
  <tr>
    <th class="box" id="img_01" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_1.jpg)"></th>
    <th rowspan="2" id="img_02" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_2.jpg)"></th>
    <th class="box" id="img_03" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_3.jpg)"></th>
    <th class="box" id="img_04" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_4.jpg)"></th>
    <th class="box" id="img_05" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_5.jpg)"></th>
  </tr>
  <tr>
    <th class="box" id="img_06" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_6.jpg)"></th>
    <th class="long_box" colspan="2" id="img_07" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_7.jpg)"></th>
    <th class="box" id="img_08" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_8.jpg)"></th>
  </tr>
  <tr>
    <th class="long_box" colspan="2" id="img_09" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_9.jpg)"></th>
    <th class="box" id="img_10" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_10.jpg)"></th>
    <th class="long_box" colspan="2" id="img_11" class="grid_img" style="background: url(https://asherwolfphotography.000webhostapp.com/images/image_11.jpg)"></th>
  </tr>
</table>

关于HTML - 如何使图像填充各种大小的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44533482/

相关文章:

html - Div 宽度等于其他两个 div 之间的间隙

javascript - 使用 jQuery 按标签中的内容过滤标签

css - 如何将div的高度设置为与屏幕高度相同

asp.net - 如何设置表单 View 寻呼机的样式

html-table - Hotmail中的时事通讯html表中的行与Gmail之间的间距

html - 在 HTML 中控制表格单元格的边框

javascript - Jssor 动态缩略图不起作用

CSS - 等高列?

html - CSS如何使DIV边框在宽度上动态变化

javascript - 可折叠面板无法正常工作