用html和css写一个九宫格
时间: 2024-05-25 13:18:12 浏览: 7
<!DOCTYPE html>
<html>
<head>
<title>九宫格</title>
<style type="text/css">
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
grid-gap: 10px;
}
.item {
background-color: #ccc;
padding: 50px;
text-align: center;
font-size: 24px;
}
</style>
</head>
<body>
<div class="grid">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>
</body>
</html>
相关问题
用html和css做一个简单的静态九宫格相册
好的,我可以为您提供代码示例。首先,我们需要一个 HTML 结构来创建九宫格布局,然后使用 CSS 样式来设计它。
HTML 代码:
```
<div class="grid-container">
<div class="grid-item"><img src="image1.jpg"></div>
<div class="grid-item"><img src="image2.jpg"></div>
<div class="grid-item"><img src="image3.jpg"></div>
<div class="grid-item"><img src="image4.jpg"></div>
<div class="grid-item"><img src="image5.jpg"></div>
<div class="grid-item"><img src="image6.jpg"></div>
<div class="grid-item"><img src="image7.jpg"></div>
<div class="grid-item"><img src="image8.jpg"></div>
<div class="grid-item"><img src="image9.jpg"></div>
</div>
```
CSS 代码:
```
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
}
.grid-item {
border: 1px solid #ddd;
padding: 10px;
text-align: center;
}
.grid-item img {
max-width: 100%;
height: auto;
}
```
上面的代码将会创建一个带有 3 列的九宫格相册,每个单元格都有一个边框和一些内边距。 图片将根据其原始大小进行缩放,以适应其容器。
您可以更改 HTML 中的图片路径来替换为自己的图片。其他样式,如背景颜色、边框颜色、内边距等,也可以根据需要进行自定义。
用HTML5写一个九宫格拼图游戏
好的,以下是一个简单的九宫格拼图游戏的 HTML5 代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>九宫格拼图游戏</title>
<style>
/* 设置游戏区域样式 */
#game-board {
display: flex;
flex-wrap: wrap;
width: 450px;
height: 450px;
background-color: #eee;
border: 5px solid #333;
}
/* 设置拼图块样式 */
.puzzle-piece {
width: 150px;
height: 150px;
background-size: 450px 450px;
background-repeat: no-repeat;
border: 5px solid #333;
}
/* 设置空白块样式 */
.empty-piece {
border: none;
background-color: #eee;
}
</style>
</head>
<body>
<h1>九宫格拼图游戏</h1>
<div id="game-board">
<!-- 生成拼图块 -->
<div class="puzzle-piece" id="piece-1"></div>
<div class="puzzle-piece" id="piece-2"></div>
<div class="puzzle-piece" id="piece-3"></div>
<div class="puzzle-piece" id="piece-4"></div>
<div class="puzzle-piece" id="piece-5"></div>
<div class="puzzle-piece" id="piece-6"></div>
<div class="puzzle-piece" id="piece-7"></div>
<div class="puzzle-piece" id="piece-8"></div>
<div class="empty-piece" id="empty"></div>
</div>
<script>
// 获取拼图块和空白块元素
const pieces = document.querySelectorAll('.puzzle-piece');
const emptyPiece = document.querySelector('.empty-piece');
// 随机排序拼图块位置
const pieceOrder = shuffleArray([1, 2, 3, 4, 5, 6, 7, 8]);
for (let i = 0; i < pieces.length; i++) {
pieces[i].style.order = pieceOrder[i];
pieces[i].style.backgroundImage = `url('img/piece${pieceOrder[i]}.jpg')`;
}
// 为拼图块添加点击事件
for (let i = 0; i < pieces.length; i++) {
pieces[i].addEventListener('click', function() {
// 判断拼图块是否可以移动
if (canMove(this)) {
// 移动拼图块
movePiece(this);
// 判断是否游戏结束
if (isGameOver()) {
alert('恭喜你完成了游戏!');
}
}
});
}
// 判断拼图块是否可以移动
function canMove(piece) {
const pieceOrder = parseInt(piece.style.order);
const emptyOrder = parseInt(emptyPiece.style.order);
// 判断是否在同一行或同一列
if (pieceOrder === emptyOrder - 3 ||
pieceOrder === emptyOrder - 1 ||
pieceOrder === emptyOrder + 1 ||
pieceOrder === emptyOrder + 3) {
return true;
} else {
return false;
}
}
// 移动拼图块
function movePiece(piece) {
const pieceOrder = parseInt(piece.style.order);
piece.style.order = emptyPiece.style.order;
emptyPiece.style.order = pieceOrder;
}
// 判断游戏是否结束
function isGameOver() {
for (let i = 0; i < pieces.length; i++) {
if (parseInt(pieces[i].style.order) !== i + 1) {
return false;
}
}
return true;
}
// 洗牌算法,随机排序数组
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
</script>
</body>
</html>
```
上面的代码生成了一个 3x3 的九宫格拼图游戏,拼图块和空白块使用 CSS 的 `order` 属性来控制位置,点击拼图块时判断是否可以移动,移动时交换位置,判断游戏是否结束时,判断拼图块的顺序是否正确。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)