通过父DIV更改图像大小

fun*_*err 16 html image-size

我试图这样做,但它没有改变大小:

<div style="height:42px;width:42px">
   <img src="http://someimage.jpg">
</div>
Run Code Online (Sandbox Code Playgroud)

什么会重新调整大小(我不能编辑/触摸img元素本身).

提前致谢.

Ilj*_*lja 29

我不确定你的意思是"我无法访问图像"但是如果你有权访问父div,你可以执行以下操作:

冷杉给你的div提供id或class:

<div class="parent">
   <img src="http://someimage.jpg">
</div>
Run Code Online (Sandbox Code Playgroud)

然后将此添加到您的CSS:

.parent {
   width: 42px; /* I took the width from your post and placed it in css */
   height: 42px;
}

/* This will style any <img> element in .parent div */
.parent img {
   height: 100%;
   width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

  • 这正是萨弗拉兹所说的:) (2认同)

Sar*_*raz 7

100%为图像应用宽度和高度:

<div style="height:42px;width:42px">
  <img src="http://someimage.jpg" style="width:100%; height:100%">
</div>
Run Code Online (Sandbox Code Playgroud)

这样它的大小与其父级相同.