【问题标题】:Child image(height, width in px dynamically) fit to parent div子图像(以 px 为单位的高度、宽度动态)适合父 div
【发布时间】:2017-06-20 06:35:56
【问题描述】:

我的问题是子图像元素必须适合父 div,但图像的高度和宽度是动态设置的。

.people-image-right-parent { height: 150px; width: 150px;background: #EAEAEA; overflow: auto; }
.people-image-right{    width: 220px;
    object-fit: contain;
    height: 220px;
    background: url(http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png) -491px -235px;}
<div class="people-image-right-parent">
    <img class="people-image-right"  />
</div>

图像的高度和宽度是动态的,不是固定的,应该以像素 (px) 为单位。我不希望图像可滚动或隐藏。它必须适合父元素或必须包含在父元素中。

请指导我如何使图像适合其父 div。

提前致谢。

【问题讨论】:

  • people-image-right 在 html 中是 id,但在 css 中你的目标是类
  • @jakob 我修改了我的问题。
  • 你设置的图像高度宽度大于 div 标签这就是为什么不正确可见@AkashChavda
  • @Bhargav 是的,你是对的,但这是我的情况,所以子图像元素如何适合父 div 元素。

标签: html css


【解决方案1】:

试试这个

.people-image-right-parent {
  height: 150px;
  width: 150px;
  background: #EAEAEA;
  overflow: hidden;
}

.people-image-right {
  width: 100%;
  object-fit: contain;
  height: 100%;
  background: url(http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png) -491px -235px;
}
<div class="people-image-right-parent">
  <img class="people-image-right" class="" />
</div>

【讨论】:

  • 子图像元素的高度宽度不是百分比,应该是像素。图像高度宽度有时是 50 像素,有时是 400 像素,它不固定。
  • 所以它也适用于像素宽度高度尝试@AkashChavda
【解决方案2】:

这就是你要找的吗?

.people-image-right-parent {
    height: 150px;
    width: 150px;
    background: #EAEAEA;
    overflow: hidden; /* If needed you can change this
                      value to "scroll" or "visible". */
}
.people-image-right{
    height: inherit; /* If needed you can change these */
    width: inherit;  /* values to pixel values. */
    object-fit: cover;
    margin: 0;
}
<div class="people-image-right-parent">
    <img class="people-image-right" src="http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png" />
</div>

【讨论】:

  • 图像有一定的像素高度宽度,比如它应该是 50 像素或 450 像素。它不是固定的。
  • 然后可以将heightwidth的值改为像素值。例如:jsfiddle.net/0ao4y91g 但是,如果您使用小于people-image-right-parents 的值,您最终会得到一些空白。
  • 我不想隐藏图片。如果图像高度宽度为 250 像素,则此图像适合父 div,但不隐藏任何像素
  • 然后将overflow的值设置为scrollvisible
  • 我不想滚动或隐藏图像。我想图像适合父 div。
【解决方案3】:

这里试试这个

  1. 如果你的图片比你的 div 低(以 PX 为单位),你必须使用 min-width:100% & min-height:100%;

  2. 如果你的图片比你的 div 高(以 PX 为单位),你必须使用 max-width:100% & max-height:100%;

这是示例,您的图像高于或低于父 div,它应该适合父 div。

.people-image-right-parent {
    height: 150px;
    width: 150px;
    background: #EAEAEA;
    
}
.people-image-right{
    height: 350px;
    width: 345px;
    max-width:100%;
    max-height:100%;
    min-width:100%;
    min-height:100%;
    margin: 0;
}
<div class="people-image-right-parent">
    <img class="people-image-right" src="http://ndl.mgccw.com/mu3/000/488/937/sss/68ad9b3f576e42399021560560fb3a16_small.png" />
</div>

【讨论】:

  • 如果高度大于父 div 则不要设置为 100%。如果高度大于父 div 则需要将图像设置为父 div 而不滚动或隐藏
  • 根据需要改变身高。你试过这个吗。即使您的图像高度高于父 div,它也会起作用。使用图像高度更新了代码段。
  • 即使你不需要为图像元素设置宽度和高度。我添加只是为了演示。图像应该修复父 div 甚至图像的宽度和高度是低 r 高。
  • 你能更新我在问题中定义的 jsfiddle 吗?
  • 检查这个小提琴jsfiddle.net/523ta69j 查看显示图像部分。这部分我想在没有滚动和隐藏的情况下放入父 div。前父 div 宽度为 150 像素,子图像宽度为 220 像素。
【解决方案4】:

我不知道修改 html 是否是一种选择,但是...

在父 div 中使用 img 标签会更好

在下面的示例中,图像是完全动态的,无论纵横比如何,它们都会调整以适应 div。它也永远不会拉伸图像。

.people-image-right-parent {
  display: inline-block;
  width: 150px;
  height: 150px;
  background: darkred;
  line-height: 150px; /* should match your div height */
  text-align: center;
  font-size: 0;
}

img.people-image-right {
  max-width: 100%;
  max-height: 100%;
  vertical-align: middle;
}
<div class="people-image-right-parent">
  <img class="people-image-right" src="https://unsplash.it/300/300">
</div>

<div class="people-image-right-parent">
  <img class="people-image-right" src="https://unsplash.it/100/300">
</div>

<div class="people-image-right-parent">
  <img class="people-image-right" src="https://unsplash.it/300/100">
</div>

编辑:

如果您可以设置内联 img 的高度和宽度 - 即使它是动态生成的 - 但无法控制其他任何内容,请参见下面的示例。只要在 img 标签中以 px 声明宽度和高度,无论是手动还是动态完成,这将起作用。

.people-image-right-parent {
  display: inline-block;
  width: 150px;
  height: 150px;
  background: darkred;
  line-height: 150px; /* should match your div height */
  text-align: center;
  font-size: 0;
}
/* demonstration backgrounds */
.bg1 {background: url(https://unsplash.it/500/400);}
.bg2 {background: url(https://unsplash.it/200/600);}
.bg3 {background: url(https://unsplash.it/900/300);}

.people-image-right {
  max-width: 100%;
  max-height: 100%;
  vertical-align: middle;
  background-size: contain;
  background-position: center;
  background-repeat:no-repeat;
}
<div class="people-image-right-parent">
  <img class="people-image-right bg1" style="width:300px ;height:300px">
</div>

<div class="people-image-right-parent">
  <img class="people-image-right bg2" style="width:100px ;height:300px">
</div>

<div class="people-image-right-parent">
  <img class="people-image-right bg3" style="width:300px ;height:100px">
</div>

【讨论】:

  • 在我的情况下,图像标签不包含 src 属性。图像具有样式,并且此样式包含背景属性,其中包含一些减号(-)高度宽度(以像素为单位)。
  • 所以不能修改html?
  • 在我的情况下,我是动态生成图像样式,并且在样式中,我仅以像素值而不是百分比值设置宽度高度。
  • 是的,我在我的 jsfiddle 中更新了你的 CSS,但它的隐藏图像 jsfiddle.net/523ta69j/3
  • @AkashChavda 您应该将定义背景 url 的背景属性放在其他背景属性之前,否则它们将被覆盖。看到这个更新的小提琴:jsfiddle.net/523ta69j/5
猜你喜欢
  • 2013-11-05
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
  • 2015-06-12
  • 2014-04-22
  • 1970-01-01
相关资源
最近更新 更多