【问题标题】:Margin-Top < 0 leads to wrong zoomingMargin-Top < 0 导致缩放错误
【发布时间】:2019-06-05 07:11:12
【问题描述】:

我希望子元素可以缩放和居中。如果将孩子的缩放比例更改为 200%,您可以看到元素的顶部是紫色的,但您无法放大。我怎样才能解决这个问题?我认为这是因为 margin-top 低于零。

100% 缩放:

#child {
  background-image: linear-gradient(red, violet, yellow, green, pink);
  width:50px;
  height:400px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    zoom: 100%;
}

#parent {
  background-color:blue;
  width:500px;
  height:500px;
  position: relative;
  transform: scale();
  overflow: auto;
}
<div id="parent">
  <div id="child"></div>
</div>

放大 200%:

#child {
  background-image: linear-gradient(red, violet, yellow, green, pink);
  width:50px;
  height:400px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    zoom: 200%;
}

#parent {
  background-color:blue;
  width:500px;
  height:500px;
  position: relative;
  transform: scale();
  overflow: auto;
}
<div id="parent">
  <div id="child"></div>
</div>

【问题讨论】:

    标签: html css overflow zooming margin


    【解决方案1】:

    通过将'display: flex; align-items: center;' 添加到父级并删除绝对位置(包括位置)来解决此问题

    #child {
      background-image: linear-gradient(red, violet, yellow, green, pink);
      width: 50px;
      height: 400px;
      margin: auto;
      zoom: 200%;
    }
    
    #parent {
      background-color:blue;
      width: 500px;
      height: 500px;
      overflow: auto;
      display: flex;
      align-items: center;
    }
    <div id="parent">
      <div id="child"></div>
    </div>

    【讨论】:

      【解决方案2】:

      因为#child 的高度是以像素为单位的,所以缩放功能会放大像素数。如果您使用height: ..%,高度百分比将在您的zoom 上缩放。

      【讨论】:

      • 然后高度保持在..% 它没有放大。
      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2013-09-22
      • 1970-01-01
      • 2012-12-19
      相关资源
      最近更新 更多