我想要做的是顯示background-color
和background-image
,這樣我的div
的一半將覆蓋右側的陰影背景圖像,另一個左側部分將覆蓋背景顏色。爲什麼我不能一起使用背景圖像和顏色?
但是當我使用background-image
時,顏色消失。
我想要做的是顯示background-color
和background-image
,這樣我的div
的一半將覆蓋右側的陰影背景圖像,另一個左側部分將覆蓋背景顏色。爲什麼我不能一起使用背景圖像和顏色?
但是當我使用background-image
時,顏色消失。
完全可以將顏色和圖像用作元素的背景。
您設置了background-color
和background-image
樣式。如果圖像比元素小,你需要使用background-position
風格將其放置到右邊,並保持它的重複和覆蓋整個背景使用background-repeat
風格:
background-color: green;
background-image: url(images/shadow.gif);
background-position: right;
background-repeat: no-repeat;
或者使用複合式的background
:
background: green url(images/shadow.gif) right no-repeat;
如果使用複合風格background
同時設置獨立,只有最後一個將被使用,這是一個可能的原因,爲什麼你的顏色是不可見的:
background: green; /* will be ignored */
background: url(images/shadow.gif) right no-repeat;
無法專門限制背景圖像以僅覆蓋元素的一部分,因此您必須確保圖像小於元素或具有任何透明區域以用於背景顏色可見。
使用
background:red url(../images/samle.jpg) no-repeat left top;
使圖像的一半透明,以便通過它看到背景顏色。
否則只需添加另一個div,佔用容器div的50%,然後將其左右浮動。然後應用圖像或顏色。
這裏假設你的背景圖像和包含div一樣大。 – 2009-05-24 14:01:44
要爲圖像着色,可以使用CSS3 background
堆疊圖像和linear-gradient
。在下面的例子中,我使用了沒有實際漸變的linear-gradient
。瀏覽器將漸變視爲圖像(我認爲它實際上會生成一個位圖並覆蓋它),因此實際上是堆疊多個圖像。
background: linear-gradient(0deg, rgba(2,173,231,0.5), rgba(2,173,231,0.5)), url(images/mba-grid-5px-bg.png) repeat;
如果您有png,會生成淺藍色的圖表紙。請注意,堆疊順序可能與您的心智模型相反,第一個項目位於最上方。
由Mozilla出色的文檔,在這裏:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_multiple_backgrounds
工具用於構建梯度:
http://www.colorzilla.com/gradient-editor/
注 - 不IE11工作!我會在發現原因時發佈更新,因爲它應該這樣做。
壁虎有一個奇怪的錯誤,其中用於html
選擇會掩蓋即使body
元素實際上有更大的z-index你應該能夠看到body元素的background-image
設置background-color
身體的background-image
以及純粹基於簡單邏輯的html
background-color
。
壁虎錯誤
避免下列情況...
html {background-color: #fff;}
body {background-image: url(example.png);}
解決
body {background-color: #fff; background-image: url(example.png);}
background:url(directoryName/imageName.extention) bottom left no-repeat;
background-color: red;
下面是使用background-image
和的例子在一起:
.box {
background-image: repeating-linear-gradient(-45deg, rgba(255, 255, 255, .2), rgba(255, 255, 255, .2) 15px, transparent 15px, transparent 30px);
width: 100px;
height: 100px;
margin: 10px 0 0 10px;
display: inline-block;
}
<div class="box" style="background-color:orange"></div>
<div class="box" style="background-color:green"></div>
<div class="box" style="background-color:blue"></div>
大家好我嘗試另一種方式來結合背景圖片和背景顏色在一起:
HTML
<article><canvas id="color"></canvas></article>
CSS
article{height: 490px;background:url("Your IMAGE") no-repear center cover;opacity:1}
canvas{widht: 100%; height: 490px; opacity: 0.9;}
JAVASCRIPT
window.onload = init();
var canvas, ctx;
function init(){canvas = document.getElementeById('color'); ctx = canvas.getContext('2d');
ctx.save(); ctx.fillstyle = '#00833d'; ctx.fillRect(0,0,490,490);ctx.restore();}
請讓我知道,如果它的工作對你 感謝
什麼是你的CSS代碼? – outis 2009-05-24 12:23:20