CSS 渐变不适用于 iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16816648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:03:19  来源:igfitidea点击:

CSS gradient not working on iOS

iossafarimobile-safaricss

提问by Forza

I have created a gradient backgroundusing a CSS generator. This works perfectly in all major browsers and on Android. However in iOS i get this.

我使用 CSS 生成器创建了渐变背景。这在所有主要浏览器和 Android 上都可以完美运行。但是在 iOS 中我得到了这个

What do I need to add to this gradient in order to make it working on iOS?

我需要向这个渐变添加什么才能使其在 iOS 上运行?

Edit:Because this question isn't gaining enough attention, I'd like to ask a different question: What do I need for css tag to create a linear gradient for safari/ios, when, as in this case, -webkit-linear-gradient is not working? Are there any other css gradient tags, specifically for safari?

编辑:因为这个问题没有得到足够的关注,我想问一个不同的问题:我需要什么 css 标签来为 safari/ios 创建线性渐变,在这种情况下,-webkit-linear -梯度不工作?是否还有其他 css 渐变标签,专门用于 safari?

Here's the code for my background:

这是我的背景代码:

.gradient {
/* Legacy browsers */
background: #FF7701 url("gradient-bg.png") repeat-x top;
-o-background-size: 100% 100%;
-moz-background-size: 100% 100%;
-webkit-background-size: 100% 100%;
background-size: 100% 100%;
/* Internet Explorer */
*background: #FF7701;
background: #FF7701
background: #ffad26; /* Old browsers */
background: -moz-linear-gradient(top,  #ffad26 0%, #fea026 50%, #ff8b00 51%, #ff7701 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffad26), color-stop(50%,#fea026), color-stop(51%,#ff8b00), color-stop(100%,#ff7701)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #ffad26 0%,#fea026 50%,#ff8b00 51%,#ff7701 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #ffad26 0%,#fea026 50%,#ff8b00 51%,#ff7701 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #ffad26 0%,#fea026 50%,#ff8b00 51%,#ff7701 100%); /* IE10+ */
background: linear-gradient(to bottom,  #ffad26 0%,#fea026 50%,#ff8b00 51%,#ff7701 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffad26', endColorstr='#ff7701',GradientType=0 ); /* IE6-9 */
/; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="gradient-bg.png", sizingMethod="scale"); } @media all and (min-width: 0px) { .gradient { /* Opera */ background: #FF7701 url("gradient-bg.svg"); /* Recent browsers */ background-image: -webkit-gradient( linear, left top, left bottom, from(#FFAD26), to(#FF7701), color-stop(0.5, #FEA026), color-stop(0.5, #FFFFFF), color-stop(0.5, #FFFFFF), color-stop(0.5, #FFFFFF), color-stop(0.5, #FF8B00) ); background-image: -webkit-linear-gradient( top, #FFAD26, #FEA026 50%, #FFFFFF 50%, #FFFFFF 50%, #FFFFFF 50%, #FF8B00 50%, #FF7701 ); background-image: -moz-linear-gradient( top, #FFAD26, #FEA026 50%, #FFFFFF 50%, #FFFFFF 50%, #FFFFFF 50%, #FF8B00 50%, #FF7701 ); background-image: -o-linear-gradient( top, #FFAD26, #FEA026 50%, #FFFFFF 50%, #FFFFFF 50%, #FFFFFF 50%, #FF8B00 50%, #FF7701 ); background-image: linear-gradient( top, #FFAD26, #FEA026 50%, #FFFFFF 50%, #FFFFFF 50%, #FFFFFF 50%, #FF8B00 50%, #FF7701 ); } }

采纳答案by rob_mccann

Do give this a check in iOS but it ought to work:

请在 iOS 中检查一下,但它应该可以工作:

background-color:#FF8B00;
-webkit-box-shadow: inset 0px 100px 0px 0px rgba(255, 255, 255, 0.5);
box-shadow: inset 0px 100px 0px 0px rgba(255, 255, 255, 0.5);

I'd also recommend looking into a pre-processor like SASS which will generate a lot of these things for you.

我还建议您研究像 SASS 这样的预处理器,它会为您生成很多这些东西。

Alternative 1

备选方案 1

As an alternative, you could try using an inset box shadow. It's not exact, and it has it's limitations but it's just an option :)

作为替代方案,您可以尝试使用插入框阴影。它不准确,它有它的局限性,但它只是一种选择:)

<div data-role="page">

    <div data-role="header">
        <h1>Page Title</h1>
    </div><!-- /header -->

    <div data-role="content">

       <div class="ui-grid-a">
           <div class="ui-block-a"><div class="ui-bar gradient" style="height:200px">Block A</div></div>
            <div class="ui-block-b"><div class="ui-bar gradient" style="height:200px">Block B</div>
       </div>

</div><!-- /grid-a -->

    </div><!-- /content -->


</div><!-- /page -->

Alternative 2

备选方案 2

If you know the height, either use the box shadow above or just use a background image. That way you'll get cross-browser support without the mess that is a hundred prefixed CSS properties like above :)

如果您知道高度,请使用上方的框阴影或仅使用背景图像。这样你就可以获得跨浏览器的支持,而不会像上面那样有一百个前缀的 CSS 属性:)

回答by Pencilcheck

In mobile Safari at least, you can't use the keyword transparent, you have to use rgba(255, 255, 255, 0)instead.

至少在移动 Safari 中,您不能使用关键字transparentrgba(255, 255, 255, 0)而必须使用。

This is described here: https://developer.apple.com/library/safari/documentation/internetweb/conceptual/safarivisualeffectsprogguide/Gradients/Gradient.html

这在这里描述:https: //developer.apple.com/library/safari/documentation/internetweb/conceptual/safarivisualeffectsprogguide/Gradients/Gradient.html

As you can see, even in Apple's own official document, rgba(255, 255, 255, 0)is used instead of transparent.

可以看到,即使在苹果自己的官方文档中,rgba(255, 255, 255, 0)也是使用transparent.

回答by yeyene

Working DEMO here http://jsfiddle.net/yeyene/akRHX/

在这里工作演示http://jsfiddle.net/yeyene/akRHX/

And its iPhone screenshot...

还有它的iPhone截图...

enter image description here

在此处输入图片说明

add your css class to the element.

将您的 css 类添加到元素。

HTML

HTML

.gradient {
    /* Legacy browsers */
    background: #FF7701 url("gradient-bg.png") repeat-x top;
    -o-background-size: 100% 100%;
    -moz-background-size: 100% 100%;
    -webkit-background-size: 100% 100%;
    background-size: 100% 100%;
    /* Internet Explorer */
    *background: #FF7701;
    background: #FF7701##代码##/;
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="gradient-bg.png", sizingMethod="scale");
    }
    @media all and (min-width: 0px) {
        .gradient {
            /* Opera */
            background: #FF7701 url("gradient-bg.svg");
            /* Recent browsers */
            background-image: -webkit-gradient(
                linear,
                left top, left bottom,
                from(#FFAD26),
                to(#FF7701),
        color-stop(0.5, #FEA026),
        color-stop(0.5, #FFFFFF),
        color-stop(0.5, #FFFFFF),
        color-stop(0.5, #FFFFFF),
        color-stop(0.5, #FF8B00)
            );
            background-image: -webkit-linear-gradient(
                top,
                #FFAD26,
                #FEA026 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FF8B00 50%,
        #FF7701
            );
            background-image: -moz-linear-gradient(
                top,
                #FFAD26,
                #FEA026 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FF8B00 50%,
        #FF7701
            );
            background-image: -o-linear-gradient(
                top,
                #FFAD26,
                #FEA026 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FF8B00 50%,
        #FF7701
            );
            background-image: linear-gradient(
                top,
                #FFAD26,
                #FEA026 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FFFFFF 50%,
        #FF8B00 50%,
        #FF7701
            );
        }
    }

CSS

CSS

##代码##