能否使用CSS/CSS3来翻转文本?
具体而言,我有这个剪刀符号“✂”(✂
),我想将其显示为向左指而不是向右。
能否使用CSS/CSS3来翻转文本?
具体而言,我有这个剪刀符号“✂”(✂
),我想将其显示为向左指而不是向右。
您可以使用CSS变换来实现此操作。水平翻转将涉及按如下方式缩放div:
-moz-transform: scale(-1, 1);
-webkit-transform: scale(-1, 1);
-o-transform: scale(-1, 1);
-ms-transform: scale(-1, 1);
transform: scale(-1, 1);
如果要进行垂直翻转,需要按如下方式缩放div:
-moz-transform: scale(1, -1);
-webkit-transform: scale(1, -1);
-o-transform: scale(1, -1);
-ms-transform: scale(1, -1);
transform: scale(1, -1);
span{ display: inline-block; margin:1em; }
.flip_H{ transform: scale(-1, 1); color:red; }
.flip_V{ transform: scale(1, -1); color:green; }
<span class='flip_H'>Demo text ✂</span>
<span class='flip_V'>Demo text ✂</span>
padding-right: 10px;
将在左侧显示10px填充),但不包括外边距。 - Marcel-moz-transform: scale(-1, 1);
-webkit-transform: scale(-1, 1);
-o-transform: scale(-1, 1);
-ms-transform: scale(-1, 1);
transform: scale(-1, 1);
这两个参数分别代表X轴和Y轴,-1将会成为一个镜像,但是您可以按照自己的需求缩放到任何大小。上下翻转则为(-1, -1)
。
如果您对于2011年跨浏览器支持的最佳选项感兴趣,请查看我的旧答案。
scale
而不是rotate(-180deg)
。 - keeri真实的镜像:
.mirror{
display: inline-block;
font-size: 30px;
-webkit-transform: matrix(-1, 0, 0, 1, 0, 0);
-moz-transform: matrix(-1, 0, 0, 1, 0, 0);
-o-transform: matrix(-1, 0, 0, 1, 0, 0);
transform: matrix(-1, 0, 0, 1, 0, 0);
}
<span class='mirror'>Mirror Text<span>
fa-flip-horizontal
和fa-flip-vertical
属性。 - lolbackface-visibility: visible; transform-origin: center center; transform-style: preserve-3d; transform: matrix(-1, 0, 0, 1, 0, 0) rotateY(180deg) ;
这对于保证元素的背面文本方向正确非常有帮助。 - parliament.your-class{
position:absolute;
-moz-transform: scaleX(-1);
-o-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH;
}
或者 .your-class{
position:absolute;
transform: rotate(360deg) scaleX(-1);
}
请注意将 position
设置为 absolute
非常重要!如果不设置,则需要设置 display: inline-block;
我通过搜索互联网,包括:
这个解决方案似乎适用于所有浏览器,包括IE6+,使用 scale(-1,1)
(一个合适的镜像)并在必要时使用适当的 filter
/-ms-filter
属性(IE6-8):
/* Cross-browser mirroring of content. Note that CSS pre-processors
like Less cough on the media hack.
Microsoft recommends using BasicImage as a more efficent/faster form of
mirroring, instead of FlipH or some kind of Matrix scaling/transform.
@see http://msdn.microsoft.com/en-us/library/ms532972%28v=vs.85%29.aspx
@see http://msdn.microsoft.com/en-us/library/ms532992%28v=vs.85%29.aspx
*/
/* IE8 only via hack: necessary because IE9+ will also interpret -ms-filter,
and mirroring something that's already mirrored results in no net change! */
@media \0screen {
.mirror {
-ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(mirror=1)";
}
}
.mirror {
/* IE6 and 7 via hack */
*filter: progid:DXImageTransform.Microsoft.BasicImage(mirror=1);
/* Standards browsers, including IE9+ */
-moz-transform: scale(-1,1);
-ms-transform: scale(-1,1);
-o-transform: scale(-1,1); /* Op 11.5 only */
-webkit-transform: scale(-1,1);
transform: scale(-1,1);
}
为了实现跨浏览器兼容性,请创建这个类
.mirror-icon:before {
-webkit-transform: scale(-1, 1);
-moz-transform: scale(-1, 1);
-ms-transform: scale(-1, 1);
-o-transform: scale(-1, 1);
transform: scale(-1, 1);
}
并将其添加到您的图标类中,例如
<i class="icon-search mirror-icon"></i>
将搜索图标与句柄放在左侧
还有一个真正的镜像效果可以使用rotateY
:
transform: rotateY(180deg);
您可以使用“transform”来实现这一点。 http://jsfiddle.net/aRcQ8/
CSS:
-moz-transform: rotate(-180deg);
-webkit-transform: rotate(-180deg);
transform: rotate(-180deg);
这里提供一个水平和垂直镜像翻转的工作演示。
.horizontal-flip {
-moz-transform: scale(-1, 1);
-webkit-transform: scale(-1, 1);
-o-transform: scale(-1, 1);
-ms-transform: scale(-1, 1);
transform: scale(-1, 1);
}
.vertical-flip {
-moz-transform: scale(1, -1);
-webkit-transform: scale(1, -1);
-o-transform: scale(1, -1);
-ms-transform: scale(1, -1);
transform: scale(1, -1);
}
<div class="horizontal-flip">
Hello, World
<input type="text">
</div>
<hr>
<div class="vertical-flip">
Hello, World
<input type="text">
</div>
使用像s7 stroke icons和font-awesome这样的字体图标可以很好地实现此目标:
.mirror {
display: inline-block;
transform: scaleX(-1);
}
然后,在目标元素上:
<button>
<span class="s7-back mirror"></span>
<span>Next</span>
</button>