将绝对分为响应式img

时间:2017-12-14 11:26:44

标签: javascript html css

我有一个响应/高度图像,我想将绝对元素逐个放入img,但我不知道该怎么做,因为图像大小是动态的。

代码:



#contenedorimagen {
  width: 100%;
  background: #000;
}
#imagen {
  position: relative;
  width: 100vw;
  height: 90vh;
  vertical-align: middle;
  text-align: center;
}
#i {
  max-width: 100%;
  height: auto;
  max-height: 100%;
}
#imagen #c {
  position: absolute;
  top: 0;
  left: 0;
  color: #fff;
}
#imagen #h {
  position: absolute;
  top: 0;
  right: 0;
  color: #fff;
}
#imagen #pm {
  position: absolute;
  bottom: 0;
  left: 0;
  color: #fff;
}

<div id="contenedorimagen">
  <div id="imagen">
    <div id="c">0</div>
    <div id="h">0</div>
    <div id="pm">0</div>
    <img id="i" src="https://i.imgur.com/kLkrgKO.jpg" />
  </div>
</div>
&#13;
&#13;
&#13;

谢谢

示例图片 firefox

2 个答案:

答案 0 :(得分:0)

您需要将max-width提供给图像容器

#contenedorimagen {
    width: 100%;
    background: #000;
}

#imagen {
    position: relative;
    width: 100vw;
    height: 90vh;
    max-width: 100%;
    vertical-align: middle;
    text-align: center;
}

#i {
    max-width: 100%;
    height: auto;
    max-height: 100%;
}

#imagen #c {
    position: absolute;
    top: 0;
    left: 0;
    color: #fff;
}

#imagen #h {
    position: absolute;
    top: 0;
    right: 0;
    color: #fff;
}

#imagen #pm {
    position: absolute;
    bottom: 0;
    left: 0;
    color: #fff;
}
<div id="contenedorimagen">
    <div id="imagen">
        <div id="c">0</div>
        <div id="h">0</div>
        <div id="pm">0</div>
        <img id="i" src="https://i.imgur.com/kLkrgKO.jpg" />
    </div>
</div>

答案 1 :(得分:0)

也许您可以根据您的具体需求调整此示例:

#contenedorimagen {
  position: relative;
  width: 100%;
  height: 100%;
}

#contenedorimagen #i {
  width: 100%;
  height: auto;
  display: block;
}

#contenedorimagen div {
  background: #000;
  padding: 15px;
  color: #fff;
}

#c {
  position: absolute;
  top: 0;
  left: 0;
}

#h {
  position: absolute;
  top: 0;
  right: 0;
}

#pm {
  position: absolute;
  bottom: 0;
  left: 0;
}
<div id="contenedorimagen">
  <div id="c">0</div>
  <div id="h">0</div>
  <div id="pm">0</div>
  <img id="i" src="https://i.imgur.com/kLkrgKO.jpg" />
</div>