html - 图片未填入表格

标签 html css

您好,我有一张 table ,我正在尝试创建以显示产品,但我无法让图像以适当的大小显示,我们将不胜感激。

#tbldesktops {
  width: 100%;
  height: 100%;
  margin-top: 50px;
}

#tbldesktops tr :nth-child(3) {
  width: 60%;
}

#tbldesktops tr :nth-child(1) {
  width: 15%;
}

#tbldesktops,
td {
  text-align: left;
  border: 1px solid black;
  border-collapse: collapse;
  background-color: #fde981;
  font-size: 12pt;
  height: 8vh;
}

#tbldesktops th {
  color: #fde981;
  background-color: #4f41dc;
  border: 1px solid black;
  border-collapse: collapse;
}

.productimg {
  height: auto;
  width: 100%;
}
<table id="tbldesktops">
  <tr>
    <th class="pctype" colspan="3">Desktop Computers</th>
  </tr>
  <tr>
    <th>Image</th>
    <th>Model</th>
    <th>Description</th>
  </tr>
  <tr>
    <td><img class="productimg" src="https://via.placeholder.com/200.jpg" alt="product name"></td>
    <td>Product</td>
    <td>Description</td>
  </tr>
</table>

无论我尝试过手动设置宽度或高度,它都无法正确显示。这是 fiddle https://jsfiddle.net/w5rvdzk1/1/

最佳答案

你的 CSS

#tbldesktops tr :nth-child(1) {
  width: 15%;
}

选择 <img>在表中,因为它实际上是 <td> 的第一个 child 因为它比 .productimg 具有更高的特异性对这个选民的规则是有效的。

改成

#tbldesktops tr > :nth-child(1) {
  width: 15%;
}

让它发挥作用

#tbldesktops {
  width: 100%;
  height: 100%;
  margin-top: 50px;
}

#tbldesktops tr > :nth-child(3) {
  width: 60%;
}

#tbldesktops tr > :nth-child(1) {
  width: 15%;
}

#tbldesktops,
td {
  text-align: left;
  border: 1px solid black;
  border-collapse: collapse;
  background-color: #fde981;
  font-size: 12pt;
  height: 8vh;
}

#tbldesktops th {
  color: #fde981;
  background-color: #4f41dc;
  border: 1px solid black;
  border-collapse: collapse;
}

.productimg {
  height: auto;
  width: 100%;
}
<table id="tbldesktops">
  <tr>
    <th class="pctype" colspan="3">Desktop Computers</th>
  </tr>
  <tr>
    <th>Image</th>
    <th>Model</th>
    <th>Description</th>
  </tr>
  <tr>
    <td><img class="productimg" src="https://via.placeholder.com/200.jpg" alt="product name"></td>
    <td>Product</td>
    <td>Description</td>
  </tr>
</table>

关于html - 图片未填入表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53630087/

相关文章:

javascript - Div 标签背景颜色 CSS HTML

html - 使用伪类 :before 时避免 DOM 之间的空格

html - 如何向此 &lt;textarea&gt; 添加填充?

css - opacity 属性有一些问题

css - 表格和表格单元格在 Chrome 和 Firefox 中的工作方式不同

HTML CSS 布局与 float 元素的父级中断

javascript - 是否可以获取从源加载的脚本文本而不是行内代码?

javascript - 单击电子邮件验证按钮未打开新窗口

html - 添加过渡到背景 :linear-gradient

javascript - 我想做一个内存游戏。创建卡片的最佳方式是什么