from.. 2010-03-23 22:30 采纳率: 100%
浏览 346
已采纳

我如何结合的背景图像和 CSS3的梯度相同的元素?

How do I use CSS3 gradients for my background-color and then apply a background-image to apply some sort of light transparent texture?

转载于:https://stackoverflow.com/questions/2504071/how-do-i-combine-a-background-image-and-css3-gradient-on-the-same-element

  • 写回答

16条回答 默认 最新

  • 撒拉嘿哟木头 2010-03-30 16:59
    关注

    Multiple backgrounds!

    body {
      background: #eb01a5;
      background-image: url("IMAGE_URL"); /* fallback */
      background-image: url("IMAGE_URL"), linear-gradient(#eb01a5, #d13531); /* W3C */
    }

    These 2 lines are the fallback for any browser that doesn't do gradients. See notes for stacking images only IE < 9 below.

    • Line 1 sets a flat background color.
    • Line 2 sets the background image fallback.

    The final line sets a background image and gradient for browsers that can handle them.

    • Line 3 is for all relatively modern browsers.

    Nearly all current browsers have support for multiple background images and css backgrounds. See http://caniuse.com/#feat=css-gradients for browser support. For a good post on why you don't need multiple browser prefixes, see http://codepen.io/thebabydino/full/pjxVWp/

    Layer Stack

    It should be noted that the first defined image will be topmost in the stack. In this case, the image is on TOP of the gradient.

    For more information about background layering see http://www.w3.org/TR/css3-background/#layering.

    Stacking images ONLY (no gradients in the declaration) For IE < 9

    IE9 and up can stack images this same way. You could use this to create a gradient image for ie9, though personally, I wouldn't. However to be noted when using only images, ie < 9 will ignore the fallback statement and not show any image. This does not happen when a gradient is included. To use a single fallback image in this case I suggest using Paul Irish's wonderful Conditional HTML element along with your fallback code:

    .lte9 #target{ background-image: url("IMAGE_URL"); }
    

    Background position, sizing etc.

    Other properties that would apply to a single image may also be comma separated. If only 1 value is supplied, that will be applied to all stacked images including the gradient. background-size: 40px; will constrain both the image and the gradient to 40px height and width. However using background-size: 40px, cover; will make the image 40px and the gradient will cover the element. To only apply a setting to one image, set the default for the other: background-position: 50%, 0 0; or for browsers that support it use initial: background-position: 50%, initial;

    You may also use the background shorthand, however this removes the fallback color and image.

    body{
        background: url("IMAGE_URL") no-repeat left top, linear-gradient(#eb01a5, #d13531);
    }
    

    The same applies to background-position, background-repeat, etc.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(15条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)