HTML5绘制国际象棋,如何用纯CSS实现一副国际象棋

这篇文章主要介绍了关于如何用纯CSS实现一副国际象棋 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

每日前端实战系列的全部源代码请从 github 下载:

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,一共 8 个列表,每个列表包含 8 个元素:

居中显示:body {

margin: 0;

height: 100vh;

display: flex;

align-items: center;

justify-content: center;

background-color: darkslategray;

}

定义容器的背景色和尺寸(由字号决定尺寸):.chess {

background-color: burlywood;

font-size: 32px;

}

画出网格状棋盘:ul {

display: table;

margin: 0;

padding: 0;

}

li {

display: table-cell;

width: 1.5em;

height: 1.5em;

}

设置网格交错的颜色:ul:nth-child(odd) li:nth-child(even),

ul:nth-child(even) li:nth-child(odd) {

background-color: rgba(0, 0, 0, 0.6);

}

在棋盘上安放棋子:

    ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
    ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
    ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
    ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜

设置棋子的颜色:ul:nth-child(-n+2) {

color: black;

}

ul:nth-child(n+7) {

color: white;

}

最后,为棋盘增加一点立体效果:.chess {

border: 0.2em solid tan;

box-shadow: 0 0.3em 2em 0.4em rgba(0, 0, 0, 0.3);

}

大功告成!

相关推荐:

这篇文章主要介绍了关于如何用纯CSS实现一副国际象棋 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 每日前端实战系列的全部源代码请从 github 下载: https://github.com/comehope/front-end-daily-challenges 代码解读 定义 dom,一共 8 个列表,每个列表包含 8 个元素: 居中显示:body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: darkslategray; } 定义容器的背景色和尺寸(由字号决定尺寸):.chess { background-color: burlywood; font-size: 32px; } 画出网格状棋盘:ul { display: table; margin: 0; padding: 0; } li { display: table-cell; width: 1.5em; height: 1.5em; } 设置网格交错的颜色:ul:nth-child(odd) li:nth-child(even), ul:nth-child(even) li:nth-child(odd) { background-color: rgba(0, 0, 0, 0.6); } 在棋盘上安放棋子: ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 设置棋子的颜色:ul:nth-child(-n+2) { color: black; } ul:nth-child(n+7) { color: white; } 最后,为棋盘增加一点立体效果:.chess { border: 0.2em solid tan; box-shadow: 0 0.3em 2em 0.4em rgba(0, 0, 0, 0.3); } 大功告成! 相关推荐:
经验分享 程序员 微信小程序 职场和发展