【问题标题】:How to scale/move child images in a container relative to scaling of father image in container?相对于容器中父图像的缩放,如何缩放/移动容器中的子图像?
【发布时间】:2019-09-07 18:02:09
【问题描述】:

我在容器中有一个父亲图像,该图像顶部有几个具有绝对位置的可点击图标。

我现在想将父亲图像缩放到最大宽度:100%

现在我希望以绝对位置放置的图标根据父图像表现相对,以便它们始终位于父图像上的同一位置。

到目前为止,这是我的代码:

.container {
  position: relative;
  top: 0;
  left: 0;
}

.father-image {
  position: relative;
  top: 0;
  left: 0;
}

.icon_pos {
  position: absolute;
  top: 50px;
  left: 50px;
}

.icon {
  width: 20em;
  height: 5em;
  background: url("icon.png") no-repeat;
  background-size: 20em;
  display: fill;
}

.icon:hover {
  width: 20em;
  height: 5em;
  background: url("icon_hover.png") no-repeat;
  background-size: 20em;
}
<div class="container">
  <img class="father-image" src="father-image.png">

  <div class="icon_pos">
    <a href="SOME_LINK">
      <div class="icon"></div>
    </a>
  </div>
</div>

此代码按预期工作。显示父图,图标为 50/50 位置,可点击。

不,我想将父亲图像缩放到 100%(将“最大宽度:100%”添加到“父亲图像”类)。如果我这样做,图像在屏幕上完全可见,但图标保持在 50/50,这不是所需的行为,因为它也应该相对于父图像移动/缩放。

有人有想法吗?

【问题讨论】:

    标签: html css css-position


    【解决方案1】:

    如果我猜对了,您只需将“.icon_pos”位置设置为“.container”的 %

    .icon_pos {
        position: absolute;
        top: 5%;
        left: 5%;
    }
    

    更新 1

    这将根据“.container”更改容器“icon_pos”的宽度和高度

    .icon_pos 
    {
          position: absolute;
          top: 5%;
          left: 5%;
          width: 20%;
          height: 20%;
     }
    

    您还应该将图标大小设置为“icon_pos”的 100%

    .icon 
    {
         width: 100%;
         height: 100%;
    }
    

    【讨论】:

    • 感谢您的回复。这解决了定位问题,但图标保持相同大小。您是否也知道如何缩放图标?
    • 尝试为 icon_pos 添加宽度和高度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多