Different output for same box-shadow size in Chrom

2019-06-16 05:40发布

Why size of box-shadow in Chrome and Firefox are different?

box-shadow: 0 0 4px #aaa inset;

Chrome:
enter image description here

Firefox:
enter image description here

I've already try the following but it's not working in latest Firefox.

-moz-box-shadow: 0 0 2px #aaa inset;
-webkit-box-shadow: 0 0 4px #aaa inset;

How can I have cross browser box shadows in a same size?

4条回答
叛逆
2楼-- · 2019-06-16 05:53

I'm faced the same problem.
Try to think that all browsers have the bigger shadow and only the webkit have smaller.
So solution is:

box-shadow: 0 0 2px #aaa inset; /* for all browsers */
-moz-box-shadow: 0 0 2px #aaa inset; /* for old firefox */
-webkit-box-shadow: 0 0 4px #aaa inset; /* override for Chrome & Safari */

Hopefully Chrome and Safari will continue to support -webkit-box-shadow.

查看更多
闹够了就滚
3楼-- · 2019-06-16 05:55

w3schools says there are 6 values to box shadow:

box-shadow: h-shadow v-shadow blur spread color inset;

Blur and spread are optional and my guess is that as you only defined three values before the colour, the 2 browsers were interpreting differently.

I got them to look the same (to my eye at least) with the following on js fiddle:

-moz-box-shadow: 0px 0px 3px 0px #aaa inset;
-webkit-box-shadow: 0px 0px 5px 0px #aaa inset;

Hope this helps

查看更多
趁早两清
4楼-- · 2019-06-16 05:57

Firefox and Chrome use different renderers, and there's no easy way around it. As -moz-box-shadow no longer works, you need a different way to write a FF-only style:

.myThing {
    box-shadow: 0 0 4px #aaa inset;
}
@-moz-document url-prefix() { 
    .myThing {
        box-shadow: 0 0 2px #aaa inset;
    }
}

See also this answer.

查看更多
淡お忘
5楼-- · 2019-06-16 06:15

Shadows are different because Chrome is buggy since many years: https://bugs.chromium.org/p/chromium/issues/detail?id=179006

This is expected to be fixed in Chrome 73 (March 2019): https://www.chromestatus.com/feature/6569666117894144

The simplest thing you can do for cross browser shadows is just to create it with Firefox or Safari. Chrome 73 and later will display it correct.

Please don't use prefix-hacks for box-shadow. Box-shadow is unprefixed in all browsers since 2011.

查看更多
登录 后发表回答