我有以下布局
<div id="chess">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>
有沒有可能只用css,不改變上面的html,就做出一個棋盤?這意味著沒有類或id。 兩天來我一直在尋找這樣的想法。我嘗試了第n個孩子()和一些變化,但沒有成功。
我非常好奇這是否可行。這是給某人的任務。
所以拜托,有什么主意嗎?
這是一個有趣的問題。我認為國際象棋棋盤最好表示為一個表格,而不是一系列的方格,因為屏幕閱讀器會指示數字所在的行和列。帶表:
table tr:nth-child(odd) td:nth-child(even) {
background: #000;
}
table tr:nth-child(even) td:nth-child(odd) {
background: #000;
}
http://jsfiddle.net/9kWJZ/
不必硬編碼每個:n-child()。這里有一個縮短它的方法。每個選擇器對應棋盤上的一行:
#chess div:nth-child(-2n+8),
#chess div:nth-child(8) ~ div:nth-child(-2n+15),
#chess div:nth-child(16) ~ div:nth-child(-2n+24),
#chess div:nth-child(24) ~ div:nth-child(-2n+31),
#chess div:nth-child(32) ~ div:nth-child(-2n+40),
#chess div:nth-child(40) ~ div:nth-child(-2n+47),
#chess div:nth-child(48) ~ div:nth-child(-2n+56),
#chess div:nth-child(56) ~ div:nth-child(-2n+63) {
background-color: #000;
}
jsFiddle預覽
下面的方法利用了著色圖案每16個方塊重復一次的事實(從左上到右下計數)。因此,第一個規則# chess div:n-child(16n+1)將方格1、17、33和49(換句話說,“第一列”)著色。對從3到16的所有彩色方塊重復這一過程,每個方塊代表一個單獨的列。
對于給定的標記,使用n-of-type還是n-child并不重要,但是對于額外的標記,可能會這樣,所以n-child是更明顯的選擇。
for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess div{
width:22px;height:22px;border:1px solid black;
float:left;
}
#chess div:nth-of-type(16n+16),
#chess div:nth-of-type(16n+14),
#chess div:nth-of-type(16n+12),
#chess div:nth-of-type(16n+10),
#chess div:nth-of-type(16n+7),
#chess div:nth-of-type(16n+5),
#chess div:nth-of-type(16n+3),
#chess div:nth-of-type(16n+1){
background-color:black;
}
#chess div:nth-of-type(8n+1){
clear:left;
}
<div id="chess"></div>
在純CSS中,公認的答案看起來是正確的——然而,如果你想用SCSS來簡化它,你可以做一些數學計算:
#chess {
div {
background: #fff;
/* even children on odd rows, odd children on even rows */
@each $offset in 2 4 6 8 9 11 13 15 {
&:nth-child(16n + #{$offset}) {
background: #000;
}
}
}
}
當然這是可以做到的...
body {
background-image:
-moz-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%),
-moz-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%);
background-image:
-webkit-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%),
-webkit-linear-gradient(45deg, #000 25%, transparent 25%,transparent 75%, #000 75%, #000 100%);
-moz-background-size:100px 100px;
background-size:100px 100px;
-webkit-background-size:101px 101px;
background-position:0 0, 50px 50px;
}
你不能用n-child(奇數)或n-child(偶數)來給方塊上色,因為不是所有的“奇數”或“偶數”方塊顏色都一樣。從左上角開始計算位置“1”,第一行的白色方塊將是1,3,5,7。繼續到第二行,白色方塊將是10,12,14,16。第三行將返回奇數,17、19、21和23。
因此,您可以手動為每個方塊著色,如下所示:
#chess {
/* 8 squares at 30x30px per square */
width: 240px;
height:240px;
background:#000;
}
#chess div {
width:30px;
height:30px;
float:left;
}
#chess div:nth-child(1), /* first row */
#chess div:nth-child(3),
#chess div:nth-child(5),
#chess div:nth-child(7),
#chess div:nth-child(10), /* second row */
#chess div:nth-child(12),
#chess div:nth-child(14),
#chess div:nth-child(16)
/* ... up to 64 ... */
{
background:#fff;
}
我意識到我來晚了,這個問題已經有幾個好答案了。
我只是想添加一個解決方案,我發現在處理高級:第n個子選擇器時很容易管理。它有些冗長,不如其他建議優雅,但我發現它很容易閱讀和處理。
通過鏈接第n個子偽類,您可以將選擇限制在特定范圍內。在偽代碼中,它可以表示為:
:nth-child( start of range ):nth-child( children to select ):nth-child( end of range )
這可以用來給棋盤逐行著色,如下所示:
/* Start at 1st square, color odd squares until the 8th */
#chess :nth-child(n+1):nth-child(odd):nth-child(-n+8),
/* Even squares from 9th to 16th */
#chess :nth-child(n+9):nth-child(even):nth-child(-n+16),
/* Odd squares from 17th to 24th */
#chess :nth-child(n+17):nth-child(odd):nth-child(-n+24),
/* Even squares from 25th to 32nd */
#chess :nth-child(n+25):nth-child(even):nth-child(-n+32),
/* Odd squares from 33rd to 40th */
#chess :nth-child(n+33):nth-child(odd):nth-child(-n+40),
/* Even squares from 41st to 48th */
#chess :nth-child(n+41):nth-child(even):nth-child(-n+48),
/* Odd squares from 49th to 56th */
#chess :nth-child(n+49):nth-child(odd):nth-child(-n+56),
/* Even squares from 57th to 64th */
#chess :nth-child(n+57):nth-child(even):nth-child(-n+64) {
background: #000;
}
#chess {
width: 320px;
height: 320px;
border: 1px solid #000;
}
#chess div {
float: left;
width: 40px;
height: 40px;
background: #fff;
}
#chess :nth-child(n+1):nth-child(odd):nth-child(-n+8),
#chess :nth-child(n+9):nth-child(even):nth-child(-n+16),
#chess :nth-child(n+17):nth-child(odd):nth-child(-n+24),
#chess :nth-child(n+25):nth-child(even):nth-child(-n+32),
#chess :nth-child(n+33):nth-child(odd):nth-child(-n+40),
#chess :nth-child(n+41):nth-child(even):nth-child(-n+48),
#chess :nth-child(n+49):nth-child(odd):nth-child(-n+56),
#chess :nth-child(n+57):nth-child(even):nth-child(-n+64) {
background: #000;
}
<div id="chess">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>
試試這個:
table.CHESS {
border-collapse: collapse;
}
table.CHESS td {
width: 50px;
height: 50px;
border: solid gray 1px;
}
table tr:nth-child(odd) td:nth-child(odd) {
background: #000;
}
table tr:nth-child(even) td:nth-child(even) {
background: #000;
}
完成了。樣本:http://jsfiddle.net/LFVQU/1/
<style type="text/css">
#chess{
width:800px;
height:800px;
border:1px;
border:1px solid #999;
}
#chess div{
width:100px;
height:100px;
float:left;
}
#chess div{background: #fff}
#chess div:nth-child(1), #chess div:nth-child(3), #chess div:nth-child(5), #chess div:nth-child(7),
#chess div:nth-child(10), #chess div:nth-child(12), #chess div:nth-child(14), #chess div:nth-child(16),
#chess div:nth-child(17), #chess div:nth-child(19), #chess div:nth-child(21), #chess div:nth-child(23),
#chess div:nth-child(26), #chess div:nth-child(28), #chess div:nth-child(30), #chess div:nth-child(32),
#chess div:nth-child(33), #chess div:nth-child(35), #chess div:nth-child(37), #chess div:nth-child(39),
#chess div:nth-child(42), #chess div:nth-child(44), #chess div:nth-child(46), #chess div:nth-child(48),
#chess div:nth-child(49), #chess div:nth-child(51), #chess div:nth-child(53), #chess div:nth-child(55),
#chess div:nth-child(58), #chess div:nth-child(60), #chess div:nth-child(62), #chess div:nth-child(64)
{
background-color:#000;
}
</style>
我認為使用float/clear的答案更好,這正是我想出的答案。
對于那些需要一個每個方格都有id的CSS3棋盤的人,我可以提出這個解決方案:
https://github.com/vpcom/CSS3-Chess-Board
這里有一個演示:http://vincentperrin.com/cr/css3/css3-chess-board/
它是用Sass (SCSS符號)完成的,但是你也可以使用經過處理的CSS文件。對于喜歡的人來說,這種東西也可以用玉來做。
盡情享受吧!
如果使用兩個重疊的容器是可以接受的,我認為有一個更簡單的& quot靜態& quot不使用花哨的css功能的方法:
.chess {
position: absolute;
display: grid;
grid-template-columns: repeat(4, 25%);
grid-template-rows: repeat(4, 25%);
}
#up > div {
height: 50px;
width: 50px;
margin: 0px 50px 50px 0px;
background-color: #000;
}
#down > div {
height: 50px;
width: 50px;
margin: 50px 0px 0px 50px;
background-color: #000;
}
<html>
<div id = "cont">
<div id = "up" class="chess">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>
<div id = "down" class="chess">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>
</div>
</html>
靈感來自BoltClocks小提琴
for(i=0;i<64;i++){chess.appendChild(document.createElement("div"))}
#chess {
width: 8em;
height: 8em;
margin: 0.5em;
border: 2px solid #808080;
}
#chess div {
float: left;
width: 1em;
height: 1em;
margin-left: 1em;
background-color: #000;
}
#chess div:nth-child(8n+5){
margin-left: 0;
}
#chess div:nth-child(32) ~ div{ /* we dont need those :D*/
display: none;
}
<div id="chess"></div>
https://codepen.io/yogi-dad/pen/mdzPRzp
所有的區域都有相同的尺寸,所以你可以考慮在父元素上使用漸變顏色
#chess {
/* define the size */
width: 300px;
aspect-ratio: 1;
/* define the grid */
display: grid;
grid-template-columns: repeat(8, 1fr);
grid-template-rows: repeat(8, 1fr);
/* define the coloration */
background:
repeating-conic-gradient(white 0 25%,black 0 50%)
0 0/calc(100%/4) calc(100%/4);
border: 1px solid;
}
<div id="chess">
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>
const HelloWorld = {
template: '#tmpl',
name: "HelloWorld",
methods: {
board (s) {
return Array.from({length:s**2}).map((_,i)=>(parseInt(i/s,10)+1)%2===i%s%2)
},
},
};
new Vue({
el: '#app',
components: { HelloWorld },
template: '<HelloWorld />'
})
.hello {
display: grid;
grid-template-columns: repeat(8, 20px);
}
.square {
width: 20px;
height: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script type="text/x-template" id="tmpl">
<div class="hello">
<div
class="square"
v-for="(square, idx) in board(8)"
:key="idx"
:style="{ background: square ? '#FFF' : '#000' }"
></div>
</div>
</script>
<div id="app"></div>
讓我建議你更干凈的css:
.divTableRow:nth-child(odd) .divTableCell:nth-child(even), .divTableRow:nth-child(even) .divTableCell:nth-child(odd) {
background: #999;
}
.divTableRow:nth-child(odd) .divTableCell:nth-child(even), .divTableRow:nth-child(even) .divTableCell:nth-child(odd) {
background: #999;
}
.divTable {
display: table;
width: 60%;
float: left;
}
.divTableRow {
display: table-row;
}
.divTableHeading {
background-color: #EEE;
display: table-header-group;
}
.divTableCell, .divTableHead {
display: table-cell;
padding: 3px 10px;
height: 12.5%;
width: 12.5%;
text-align: center;
}
.divTableHeading {
background-color: #EEE;
display: table-header-group;
font-weight: bold;
}
.divTableFoot {
background-color: #EEE;
display: table-footer-group;
font-weight: bold;
}
.divTableBody {
background: white;
display: table-row-group;
}
<div class="divTable">
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCell">
<div id="black-rock1" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-knight1" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-bishop1" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-queen" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-king" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-bishop2" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-knight2" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-rack2" class="draggable black">?</div>
</div>
</div>
<div class="divTableRow">
<div class="divTableCell">
<div id="black-pawn1" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-pawn2" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-pawn3" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-pawn4" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-pawn5" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-pawn6" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-pawn7" class="draggable black">?</div>
</div>
<div class="divTableCell">
<div id="black-pawn8" class="draggable black">?</div>
</div>
</div>
<div class="divTableRow">
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
</div>
<div class="divTableRow">
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
</div>
<div class="divTableRow">
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
</div>
<div class="divTableRow">
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
</div>
<div class="divTableRow">
<div class="divTableCell">
<div id="white-pawn1" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-pawn2" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-pawn3" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-pawn4" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-pawn5" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-pawn6" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-pawn7" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-pawn8" class="draggable white">?</div>
</div>
</div>
<div class="divTableRow">
<div class="divTableCell">
<div id="white-rock1" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-knight1" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-bishop1" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-queen" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-king" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-bishop2" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-knight2" class="draggable white">?</div>
</div>
<div class="divTableCell">
<div id="white-rack2" class="draggable white">?</div>
</div>
</div>
</div>
</div>