使布局使用变换的坐标(CSS)

发布于 2025-01-27 21:32:42 字数 513 浏览 3 评论 0原文

有没有办法使HTML/CSS在布局计算中使用转换的坐标?例如,如果我缩放DIV,我希望下一个Div能够自动从缩放的Div边界开始。

body {
  padding:50px;
}

.a {
  width:100px;
  height:100px;
  background-color:red;
  transform:scale(1.2)
}

.b {
  width:100px;
  height:100px;
  background-color:blue;
}
<div class="a">
  hello
</div>

<div class="b">
  world
</div>

Is there a way to make HTML/CSS to use the transformed coordinates in layout calculations? E.g. if I scale a div, I want the next div to automatically start on the scaled div's boundary.

body {
  padding:50px;
}

.a {
  width:100px;
  height:100px;
  background-color:red;
  transform:scale(1.2)
}

.b {
  width:100px;
  height:100px;
  background-color:blue;
}
<div class="a">
  hello
</div>

<div class="b">
  world
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2025-02-03 21:32:42

是否有一种方法可以使HTML/CSS在布局计算中使用转换的坐标?

是的,这就是变换属性的目的。

但是,变换(与许多其他属性一样)仅适用于元素及其后代而不是其兄弟姐妹。这是副设计。此外,诸如transform之类的属性(和obacity和其他)创建一个新的堆叠上下文,基本上弄乱了z- index订购。

无论如何,要获得想要的东西,您可以使用其他方法:当您使用1.2的恒定尺度时,您可以使用它来计算的正底部边距.a to bump 兄弟姐妹像这样:

body {
  padding:50px;
}

.a {
  width: 100px;
  height: 100px;
  background-color: red;
  transform: scale(1.2); /* i.e. 1.0 + 0.2 */
  margin-bottom: 10px; /* ( 0.2 * 100px ) / 2 // the divide-by-2 is because the scale is centered, so a 20% total scale results in a 10% scale at each end of the axis */
}

.b {
  width: 100px;
  height: 100px;
  background-color: blue;
}
  <div class="a">
    hello
  </div>

  <div class="b">
    world
  </div>

Is there a way to make HTML/CSS to use the transformed coordinates in layout calculations?

Yes, that's what the transform property is for.

However, transform (like many other properties) only applies to an element and its descendants not its siblings. This is by-design. Furthermore, properties like transform (and opacity, and others) create a new stacking context which basically messes-up z-index ordering.

Anyway, to get what you want, you can use a different approach: as you're using a constant scale of 1.2 you can use that to calculate a positive bottom margin on .a to bump siblings down, like so:

body {
  padding:50px;
}

.a {
  width: 100px;
  height: 100px;
  background-color: red;
  transform: scale(1.2); /* i.e. 1.0 + 0.2 */
  margin-bottom: 10px; /* ( 0.2 * 100px ) / 2 // the divide-by-2 is because the scale is centered, so a 20% total scale results in a 10% scale at each end of the axis */
}

.b {
  width: 100px;
  height: 100px;
  background-color: blue;
}
  <div class="a">
    hello
  </div>

  <div class="b">
    world
  </div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文