21

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

<div style="height:42px;width:42px">
   <img src="http://someimage.jpg">
</div>

什么会重新调整它的大小(我无法编辑/访问 img 元素本身)?

提前致谢。

4

5 回答 5

38

我不确定“我无法访问图像”是什么意思,但是如果您可以访问父 div,则可以执行以下操作:

第一给你的 div id 或 class:

<div class="parent">
   <img src="http://someimage.jpg">
</div>

将其添加到您的 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%;
}
于 2012-03-04T16:26:35.770 回答
8

100%宽度和高度应用于您的图像:

<div style="height:42px;width:42px">
  <img src="http://someimage.jpg" style="width:100%; height:100%">
</div>

这样,它的大小将与其父级相同。

于 2012-03-04T16:16:49.637 回答
1

如果图像小于您指定的 div 大小,实际上使用 100% 不会使图像变大。您需要设置尺寸、高度或宽度之一,以使所有图像填满空间。以我的经验,最好设置高度,使每一行的大小相同,然后所有项目正确包装到下一行。这将产生类似于 fotolia.com(库存图片网站)的输出

用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: 42px;
}

没有:

<div style="height:42px;width:42px">
    <img style="height:42px" src="http://someimage.jpg">
</div>
于 2014-04-05T20:32:14.960 回答
1

我这样做:

<div class="card-logo">
    <img height="100%" width="100%" src="http://someimage.jpg">
</div>

和 CSS:

.card-logo {
    width: 20%;
}

我更喜欢这种方式,好像我需要升级 - 我也可以使用 150%

于 2019-12-03T12:59:48.680 回答
0

你的:

  <div style="height:42px;width:42px">
  <img src="http://someimage.jpg">

可以使用此代码吗?

  <div class= "box">
  <img src= "http://someimage.jpg" class= "img">
  </div>

  <style type="text/css">
  .box{width: 42; height: 42;}
  .img{width: 20; height:20;}
  </style>

只是尝试,虽然晚了。:3 对于其他阅读本文的人,如果我编写代码的方式不好,请告诉我。我是这种语言的新手。我仍然想了解更多。

于 2014-03-19T15:39:49.457 回答