This article mainly introduces how to use pure CSS to implement a chess set, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Please download all the source code of the daily front-end practical series from github:
https://github.com/comehope/front-end-daily-challenges
Define dom, a total of 8 lists, each list contains 8 elements:
<p class="chess"> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> </p>
Centered display:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: darkslategray; }
Define the background of the container Color and size (size determined by font size):
.chess { background-color: burlywood; font-size: 32px; }
Draw a grid-shaped chessboard:
ul { display: table; margin: 0; padding: 0; } li { display: table-cell; width: 1.5em; height: 1.5em; }
Set the color of the grid:
ul:nth-child(odd) li:nth-child(even), ul:nth-child(even) li:nth-child(odd) { background-color: rgba(0, 0, 0, 0.6); }
Place the chess pieces on the board :
<p class="chess"> <ul> <li>♜</li> <li>♞</li> <li>♝</li> <li>♛</li> <li>♚</li> <li>♝</li> <li>♞</li> <li>♜</li> </ul> <ul> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li></li><li></li><li></li><li></li> <li></li><li></li><li></li><li></li> </ul> <ul> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> <li>♟</li> </ul> <ul> <li>♜</li> <li>♞</li> <li>♝</li> <li>♛</li> <li>♚</li> <li>♝</li> <li>♞</li> <li>♜</li> </ul> </p>
Set the color of the chess pieces:
ul:nth-child(-n+2) { color: black; } ul:nth-child(n+7) { color: white; }
Finally, add a little three-dimensional effect to the chessboard:
.chess { border: 0.2em solid tan; box-shadow: 0 0.3em 2em 0.4em rgba(0, 0, 0, 0.3); }
You’re done!
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use pure CSS to achieve the effect of a color card
How to use pure CSS to achieve the effect of a cartoon parrot
The above is the detailed content of How to implement a chess set using pure CSS. For more information, please follow other related articles on the PHP Chinese website!