我已成功使用CSS僞元件:before
和重複的:after
爲「蓋」部分以模擬模式「棋盤」紅色和白色背景。不幸的是,這需要您使用除transparent
以外的顏色作爲紅色的「間隙」,以便後面的瓷磚可以「覆蓋」紅色棋盤部分。
下JsFiddle使用transform:scale(10)
更好地顯示模式,林不知道,如果你打算把內容在這樣的背景元素裏面,但我想表明的是,僞元素坐在後面的任何內部的內容,但下面的代碼只是包含了相關的CSS
.background {
height:100px;
width:100px;
position:relative;
background-image:linear-gradient(45deg, red 25%, transparent 25%, transparent 75%, red 75%, red),
linear-gradient(45deg, red 25%, white 25%, white 75%, red 75%, red);
background-size: 2px 2px;
background-position:0 0, 1px 1px;
}
.background:after,
.background:before{
content:"";
/* position the psuedo elements entirely over the background */
top:0;
left:0;
position:absolute;
height:100%;
width:100%;
/* create "cover" gradients */
background-image:linear-gradient(45deg, white 25%, transparent 25%);
background-size:4px 4px;
background-position:0 0;
background-repeat:repeat;
/* set a negative z-index so content is above it */
z-index:-1;
}
.background:before{
background-position:2px 2px;
}
UPDATE
從服用的base64 PNG生成代碼並對設計進行硬編碼,我們最終得到了JS,它將輸出您需要製作html電子郵件背景圖片的圖片代碼。您只需根據需要更改color1
和color2
變量。
JSFIDDLE
JS
//create a new canvas element to hold the sized down pattern
var output = document.getElementById('base64-code');
var patcanvas = document.createElement('canvas');
var patcontext = patcanvas.getContext('2d');
var color1 = [255,0,0,1]; // red
var color2 = [255,255,255,1]; // white
var matrix = [
[color2, color1, color2, color1],
[color1, color2, color2, color2],
[color2, color1, color2, color1],
[color2, color2, color1, color2]
];
/*
the matrix variable represents the width, then height of the pattern, so we're sort of drawing it on its side and mirrored
.#.#
#...
.#.#
..#.
will result in
.#..
#.#.
...#
#.#.
*/
function drawPattern() {
//set width and height, which also clears the canvas
patcanvas.width = 4;
patcanvas.height = 4;
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] != 0) {
tileColor = matrix[i][j];
patcontext.fillStyle = "rgba(" + tileColor[0] + ", " + tileColor[1] + ", " + tileColor[2] + ", " + tileColor[3] + ")";
patcontext.fillRect(i, j, 1, 1);
}
}
}
//get the preview canvas and clear it as well
var pcanvas = document.getElementById("preview-canvas");
pcanvas.width = pcanvas.width;
var pcontext = pcanvas.getContext("2d");
//create a pattern from the pattern canvas and fill the preview canvas with it
var pattern = pcontext.createPattern(patcanvas, "repeat");
pcontext.rect(0, 0, pcanvas.width, pcanvas.height);
pcontext.fillStyle = pattern;
pcontext.fill();
//also update the code
var dataURL = patcanvas.toDataURL("image/png");
output.innerHTML = dataURL;
};
drawPattern();
你可以使用一個工具,如http://www.patternify.com/產生重複的base64 png格式,而不是使用一個梯度 – haxxxton