Does CSS3 support using both an image and a gradient for the background of a single <div>?

南笙酒味 提交于 2019-12-10 16:39:23

问题


I know you can do multiple backgrounds on a single <div> in CSS3, but is it possible to mix an image-referencing background (i.e. url(...)) with a gradient generating background (e.g. -moz-linear-gradient(...))?

If so, what is the syntax?

If not, what is a best-practice to achieve the same result?

Thanks.


回答1:


You can!

background: -moz-linear-gradient(-45deg, rgba(0,0,255,0), rgba(0,0,255,1)), url("image");

which can be seen live at http://software.hixie.ch/utilities/js/live-dom-viewer/saved/675.

Note, of course, that CSS gradients are still in flux.




回答2:


I assume you are trying to conditionally use gradients where supported, but image otherwise?

If so, then one way would be to do it via Javascript -- for example:

document.getElementById("whatever").style = "url(...)";

I'm not sure exactly how to check for the browser type, but that should be a simple Google search.


However, if you are trying to make it so that the image is superimposed on the gradient, but transparent (so that some of the gradient is showing through), you could try what is described here: http://www.css3.info/introduction-opacity-rgba/

Note that it uses 0.0-1.0 for alpha, not 0-255.



来源:https://stackoverflow.com/questions/4005017/does-css3-support-using-both-an-image-and-a-gradient-for-the-background-of-a-sin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!