有问题的旋转()函数〜转换旋转和翻译不适用于单个元素
我正在尝试将按钮旋转 180 度。我发现,当在“translate()”函数之前调用“rotate()”函数时,旋转不起作用。
我的问题是:为什么会这样?
第一个代码(不工作):
.btn--right {
right: 0;
top: 50%;
transform: rotate(180deg);
transform: translate(50%, -50%);
}
第二个代码(工作):
.btn--right {
right: 0;
top: 50%;
transform: translate(50%, -50%);
transform: rotate(180deg);
}
I am trying to rotate the button 180 degrees. I found out that when the 'rotate()' function is called before the 'translate()' one, the rotation doesn't work.
My question is: Why it works like that?
1st code(not working):
.btn--right {
right: 0;
top: 50%;
transform: rotate(180deg);
transform: translate(50%, -50%);
}
2nd code(working):
.btn--right {
right: 0;
top: 50%;
transform: translate(50%, -50%);
transform: rotate(180deg);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您无法在一个元素上指定两个
transform
。第二组 CSS 正在工作,因为rotate
位于 DOM 顺序的最后。因此它会覆盖translate
。您会注意到,如果将!important;
添加到您的第一组 CSS(不起作用的那组),它将正常工作,如代码片段中所示。您可以在单个元素上实现
rotate
和translate
,您可以像这样编写:Because you cannot specify two
transform
's on one element. The second set of CSS is working because therotate
is positioned last in the DOM order. Therefore it is overriding thetranslate
. You'll notice if you add!important;
to your first set of CSS (the one not working), it will work just fine as demonstrated in the snippet.You can achieve
rotate
andtranslate
on a single element you can write it like so:第二个变换属性覆盖第一个
变换:转换(50%,-50%);
适用transform:rotate:rotate(180deg);
适用The second transform property overrides first one
transform: translate(50%, -50%);
appliestransform: rotate(180deg);
applies