【问题标题】:Image within TD elementTD 元素内的图像
【发布时间】:2017-05-25 20:18:45
【问题描述】:

我正在用 JavaScript 创建一个表格。

我面临的问题是最后一列的图片 使用过 display=block 和 height=10% 但是这两个图像的大小不同,并且爆炸超出了细胞的正常高度 有什么帮助吗? 图片链接:https://drive.google.com/open?id=0B-erVHxfjF5MSllSUi1UZWNKeVU

代码:

TD = document.createElement("TD");
TD.style.borderWidth="1px";
TD.style.borderStyle="none none inset none";
TD.style.borderColor="#54514e";
TD.style.textAlign="center";
/*
tForTD=document.createTextNode("submit");
*/
tForTD = document.createElement("IMG");
tForTD.setAttribute("src", "Img/submit_2.png");
tForTD.style.backgroundColor="white";
/*
tForTD.style.position="absolute";
tForTD.style.top="0%";
tForTD.style.display="block";
*/
tForTD.style.height="10%";
tForTD.style.borderRadius="50%";
tForTD.style.filter="grayscale(80%)";
tForTD.style.boxShadow="0px 1px 3px 0px black";
/*
tForTD.onclick=  ( function(index) {
return function() { OnClickAddBill(index); };
} )(i);
*/
TD.appendChild(tForTD);
TR.appendChild(TD);
DBT.appendChild(TR);

【问题讨论】:

  • 添加标记/代码、当前输出和预期输出。
  • 请提供您的代码和您正在谈论的图像的链接。这将使您的帮助变得更加容易。
  • 欢迎来到 Stack Overflow!提问前请阅读what this site is about 和“How to ask”。
  • 我希望第二行和第三行的高度与标题行的高度完全相同

标签: javascript html


【解决方案1】:

为了适合 img inside 块添加一个类,如 img-fit:

.img-fit {
  max-width: 100%;
  max-height: 100%;
}

另外,尝试使用 10vh 而不是 10%:

参考号:https://developer.mozilla.org/en-US/docs/Web/CSS/@viewport

td {
  height: 10vh;
}

.img-fit {
  max-width: 100%;
  max-height: 100%;
}
<table>
  <td><img class="img-fit" src="http://placehold.it/350x350">111</td>
  <td><img class="img-fit" src="http://placehold.it/350x350">222</td>
</table>

【讨论】:

  • 已经尝试过 tForTD.style.maxHeight="50%";但不工作;完全适用于 tForTD.style.maxWidth="50%";但我需要限制高度
  • @MrC &lt;table&gt; 有点棘手,如果您确定高度,可以使用 50px 吗?
  • 不幸的是我不确定身高:(
  • 我很困惑的一件事:为什么 maxWidth 有效但 maxHeight 无效?
  • @MrC 使用 % heigh,你需要定义高度,也许尝试使用vh
猜你喜欢
  • 2011-11-16
  • 2012-01-30
  • 2017-10-16
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多