【问题标题】:Resize <img/> using absolute positioning使用绝对定位调整 <img/> 大小
【发布时间】:2014-03-26 19:06:18
【问题描述】:

div#ipsko 改变宽度和高度以满足绝对定位。 为什么 img#utas 没有?

JSFiddle:http://jsfiddle.net/pejh7/1/

HTML 代码:

<div id="upsko">
    <img id="utas" src="http://kharg.czystybeton.pl/108.png" />
    <div id="ipsko"></div>
</div>

CSS 代码:

div#upsko {
    position: relative;
    top: 200px; left: 200px; width: 100px; height: 100px;
    background: rgba(255,0,0,0.5);
}

img#utas {
    position: absolute;
    top: 10px; left: 10px; right: 10px; bottom: 10px;
}

div#ipsko {
    position: absolute;
    top: 0; left: 0; bottom: 0; right: 0;
    background: rgba(0,255,0,0.5);
}

【问题讨论】:

  • 在小提琴中你写了iv#ipsko
  • 我不确定你的意思,div#upsko 将是 100px x 100px。

标签: css image css-position image-resizing


【解决方案1】:

img标签放在一个div中,给图片100%宽高,然后绝对定位容器div,例如

HTML:

<div id="upsko">
    <div id="utas">
        <img src="http://kharg.czystybeton.pl/108.png" />
     </div>
    <div id="ipsko"></div>
</div>

CSS:

#upsko {
    position: relative;
    top: 200px; left: 200px; width: 100px; height: 100px;
    background: rgba(255,0,0,0.5);
}

#utas {
    position: absolute;
    top: 10px; left: 10px; right: 10px; bottom: 10px;
}
#utas img { height: 100%; width: 100%; }

#ipsko {
    position: absolute;
    top: 0; left: 0; bottom: 0; right: 0;
    background: rgba(0,255,0,0.5);
}

Fiddle

不幸的是,您描述的问题是由于未指定图像宽度(如其他答案所述)而没有说明图像大小的 px 值(或将顶部/左侧/底部/右侧和高度+宽度转换为 %)如果不添加额外的 div,就无法解决这个问题。

我知道添加额外的 div 通常被认为是不好的做法,但是当它为您提供上述灵活性时,我认为这样做通常没问题。

【讨论】:

  • 谢谢!按预期工作,但是我们为什么不能使用绝对距离定位 有什么原因吗?
  • 查看我添加的解释,希望对您有所帮助,如果没有,请告诉我,我会尝试为您修改解释:)
【解决方案2】:

看到 div "div#ipsko" 没有自己的高度和宽度,所以它继承了它的父级高度和宽度。但是图像有自己的高度和宽度。所以你必须指定图像的高度和宽度以适应 div。

img#utas {
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    width: 100%;
    height: 100%;
}

【讨论】:

    【解决方案3】:
        div#upsko {
        position: relative;
        top: 20px;
        left: 20px;
        width: 100px;
        height: 100px;
        background: rgba(255,0,0,0.5);
    }
    
    img#utas {
        position: absolute;
        top: 0px;
        left: 0px;
        right: 0px;
        bottom: 0px;
        width: 100%;
        height: 100%;
    }
    
    iv#ipsko {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background: rgba(0,255,0,0.5);
    }
    

    请尝试上面的代码。

    【讨论】:

      【解决方案4】:

      您图像的实际宽度和高度是压倒一切的。 (浏览器将调整 img 元素的尺寸以匹配实际图像的尺寸,一旦它被下载并可以分辨出它们是什么,如果没有将宽度和高度指定为 img 的属性或在 CSS 中。)

      使用普通的div 而不是图像,如果它们被设置在其他位置,您可以将宽度和高度重置回auto,但图像的auto 会将您带回实际的图像尺寸。如果您只是希望图像与容器的大小相匹配,则 100% 的宽度/高度可以解决问题,但如果您想要固定位置隐含的不同大小,这将不起作用。

      我唯一能想到的就是更改标记,以便您的图像加载到 div 内,并且 然后 具有 100% 的宽度。

      例如jsFiddle here:

      <div id="container">
         <img id="utas" src="http://kharg.czystybeton.pl/108.png" />
      </div>
      
      div#container {
          position: absolute;
          top: 10px;
          left: 10px;
          right: 10px;
          bottom: 10px;
          width: auto;
          height: auto;
      }
      
      img#utas {
          width: 100%;
          height: 100%;
      }
      

      【讨论】:

      • @SeanDunwoody 你愿意扩展一下吗?
      • 抱歉这么简短。但在你的小提琴中,图像在容器的右侧和底部溢出,而实际上它应该在每一侧都与容器相距 10 像素
      • @MattGibson img#utas 计算出宽度/高度:100px/100px,而它应该有 80px/80px。
      • @KarolMaciaszek 确切地说,问题是如果你在 px 中指定宽度和高度,如果你想改变一些东西,你的代码就不灵活
      • 啊!是的,好吧,我明白你在说什么。它扩展的原因是使用 100% 时,图像的高度和宽度设置为父容器的 100%。烦人的是,设置为“自动”(这将work with a div)会让你回到实际的图像大小。
      猜你喜欢
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多