【问题标题】:How to dynamically fit a rotated element to the parent element with CSS?如何使用 CSS 将旋转元素动态匹配到父元素?
【发布时间】:2020-03-05 17:52:26
【问题描述】:

我有 2 个 div:.container 及其子 .element
容器以页面为中心,两个元素都有position: absolutevwvh 的宽度和高度。

它们之间的区别在于,父元素的高度为25vh,宽度为25vw,而子元素的则相反:宽度为25vh,高度为25vw。这意味着一个的宽度等于另一个的高度。

然后,我使用transform: rotate(90deg) 来旋转子div。现在它们看起来一样了。

我想只移动子元素,以便它动态地适应父元素。但我没有完成它。 我尝试使用百分比或vwvh 单位的定位属性、变换原点、翻译等,但没有任何效果。

如何将此 div 放入父 div,同时在任何屏幕尺寸上保持这种方式?

body {
  background-color: #333;
}
.container {
  background-color: #000;
  position: absolute;
  height: 25vh;
  width: 25vw;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.element {
  position: absolute;
  background-color: #abc;
  transform: rotate(90deg);
  height: 25vw;
  width: 25vh;
}
<div class="container">
  <div class="element"></div>
</div>

【问题讨论】:

    标签: css css-transforms


    【解决方案1】:

    您还需要考虑transform-origin 和一些translation

    body {
      background-color: #333;
    }
    
    .container {
      background-color: #000;
      position: absolute;
      height: 25vh;
      width: 25vw;
      margin: auto;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    
    .element {
      position: absolute;
      background-color: #abc;
      height: 25vw;
      width: 25vh;
      transform: translateY(-100%) rotate(90deg);
      transform-origin: bottom left;
    }
    <div class="container">
      <div class="element"></div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-05
      • 2013-06-05
      • 2014-02-27
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多