简体   繁体   English

CSS 变换适用于 Firefox 但不适用于 Chrome

[英]CSS transform works on Firefox but not on Chrome

I am trying a thing in CSS where the background of a text is an image and when we hover over the text, it scales to a very large number that the the image used as the font color shows up completely.我正在 CSS 中尝试一个东西,其中文本的背景是图像,当我们在文本上使用 hover 时,它会缩放到一个非常大的数字,以至于用作字体颜色的图像完全显示出来。 It works perfectly on Mozilla Firefox, but the transform property of CSS seems to be broken on Chrome and the text just vanishes on hover on Chrome.它在 Mozilla Firefox 上完美运行,但 CSS 的变换属性在 Chrome 上似乎被破坏了,文本在 Chrome 上的 hover 上消失了。

 /* font */ @import url('https://fonts.googleapis.com/css2?family=Rubik:wght@900&display=swap'); /* reset */ * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Rubik', sans-serif; } body, html { min-height: 100vh; width: 100vw; overflow-x: hidden; background: #999; } /* styling */.container { background: url("https://picsum.photos/900/800"); background-repeat: no-repeat; background-size: cover; color: transparent; -webkit-background-clip: text; background-clip: text; height: 100vh; display: flex; justify-content: center; align-items: center; overflow: hidden; }.container.image-text { font-size: 5em; font-weight: normal; text-align: center; transition: transform 2s linear; }.container.image-text:hover { transform: scale(200); }
 <div class="container"> <div class="image-text"> Hover </div> </div>

The problem is that any sort of transform creates a new stacking context so the clip text isn't effective.问题是任何类型的转换都会创建一个新的堆叠上下文,因此剪辑文本无效。 [That is one of the things that the CSS property scale does for you - takes it away from being a transform - but it isn't supported by Chrome/Edge]. [这是 CSS 属性比例为您所做的事情之一 - 将其从转换中移除 - 但 Chrome/Edge 不支持它]。

If we do away with a transform of scale and instead increase the font-size and transition on that you get what I think is the effect you want:如果我们取消比例变换,而是增加字体大小和过渡,你会得到我认为你想要的效果:

 /* font */ @import url('https://fonts.googleapis.com/css2?family=Rubik:wght@900&display=swap'); /* reset */ * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Rubik', sans-serif; } body, html { min-height: 100vh; width: 100vw; overflow-x: hidden; background: #999; } /* styling */.container { background: url("https://picsum.photos/900/800"); background-repeat: no-repeat; background-size: cover; color: transparent; -webkit-background-clip: text; background-clip: text; height: 100vh; display: flex; justify-content: center; align-items: center; overflow: hidden; }.container.image-text { font-size: 5em; font-weight: normal; text-align: center; transition: font-size 2s linear; }.container.image-text:hover { font-size: 200em; }
 <div class="container"> <div class="image-text"> Hover </div> </div>

Looks like Chrome doesn't render it correctly.看起来 Chrome 无法正确呈现它。 It does, when you use zoom , you just lose the transition then...确实,当您使用zoom时,您只会失去过渡......

 @import url('https://fonts.googleapis.com/css2?family=Rubik:wght@900&display=swap'); .container { font-family: 'Rubik', sans-serif; background: url("https://picsum.photos/900/800"); background-repeat: no-repeat; background-size: cover; color: transparent; -webkit-background-clip: text; background-clip: text; height: 100vh; display: flex; justify-content: center; align-items: center; overflow: hidden; }.container.image-text { font-size: 5em; font-weight: normal; text-align: center; transition: transform 2s linear; }.container.image-text:hover { -moz-transform: scale(200); zoom: 200; }
 <div class="container"> <div class="image-text"> Hover </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM