【发布时间】:2020-08-12 03:42:09
【问题描述】:
我知道 CSS transform: rotate 属性,以及用于制作有角度的 div 元素的方法,但有一个非常重要的设计元素我在任何地方都找不到:
如何保持边界框的角度
当我创建有角度的 div 元素时,我使用旋转 n 度的包装元素,然后使用旋转的子元素(通常是 span)-n em> 度。不幸的是,这会导致子元素有自己的矩形边界,独立于其父元素。
下图是一个非常基本的示例,每个人似乎都可以接受,其中子元素的文本位于无角矩形内,但这不是我想要的:
.parent {
width: 200px;
height: 200px;
transform: rotate(30deg);
background-color: blue; /* for clarity in img */
}
.child {
display: inline-block;
transform: rotate(-30deg);
background-color: rgba(250, 80, 80, 0.4); /* for clarity in img */
}
值得注意的是子元素必须有display: inline-block才能真正旋转。
这大概就是我要找的东西:
Some
text goes here
and automatically just-
ifies to the shape of the parent
element which in this case hap-
pens to be a rectangle. Here's
more text to show the
full shape..
--
我尝试过使用text-orientation、不同的位置值组合以及transform 的其他技巧,但我无法让文本表现得好像父元素是它的边界;相反,它只是创建自己的!
我可以使用 JavaScript 解决方案,但纯 CSS 肯定会很好。希望这些信息足以让合格的个人解决问题。
感谢您的宝贵时间!
【问题讨论】:
-
您希望它用于特定角度的情况,还是通用解决方案?
-
希望是通用解决方案,绝对适用于非直角。谢谢!
标签: html css css-transforms