使用css网格的垂直和水平居中的4x4方格

时间:2018-05-03 16:11:43

标签: css css-grid

我想用div和css-grid编写2048游戏。这就是我想象的输出: enter image description here

我有外部部分,适合浏览器窗口,我只想在中左部div(称为game-container)的中间(水平和垂直)写入4x4网格

<div class = "game-container">
  <div class = "game">
    <div class = "game-cell"></div>
    <!-- 16 game cells total -->
    <div class = "game-cell"></div>
  </div>
</div>

我使用以下方法制作了一个4x4网格:

div.game {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
}

但是,我遇到的问题很少:

  • 如何制作每个game-cell方(比方说50px)
  • 如何在game-cell
  • 的中间显示数字
  • 如何使div互相接触

我可以制作其中的每一个,但不能同时制作。

此外,如何在game div的中间(如图片中)显示game-container div。

PS。如果它简化了某些东西,我不介意使用一些Bootstrap。

有关外部容器的一些信息:

html, body, div.container{
    height: 100%;
    margin: 0;
    padding: 0;
}

div.container {
  display:grid;
  grid-template-rows: 3fr 9fr 2fr;
  grid-template-columns: 3fr 5fr;
  grid-gap: 2px;
  background-color: yellow;
}

3 个答案:

答案 0 :(得分:1)

使用flexbox在网格外部实现它。

div.game {
  display: grid;
  width: 50vw;
  height: 50vh;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  width: 120px;
  height: 120px
}

.game-cell {
  width: 30px;
  height: 30px;
  border: 1px solid ;
  box-sizing: border-box;
}

.left-half{
  width: 50vw;
  height: 100vh;
  border: 1px solid red;
  display: flex;
  align-items: center;
  justify-content: center;
}

body{
  padding: 0;
  margin: 0;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class = "game-container">
  <div class="left-half">
    <div class = "game">
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
      <div class="game-cell"></div>
    </div>
  </div>
</div>
</body>
</html>

答案 1 :(得分:1)

将此添加到 .game-container css

 display: flex;
 align-items: center;
 justify-content: center;

&#13;
&#13;
.game {
  display: grid;
  width: 120px;
  height: 120px;
  border:1px solid black;
}

.game-container{
    height: 500px;
    width: 500px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid black;
}
&#13;
<div class = "game-container">

    <div class = "game">
     
    </div>

</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是使用来自社区的答案和评论的完整解决方案。

html, body, div.container{
	height: 100%;
	margin: 0;
	padding: 0;
}

div.container {
  display:grid;
  grid-template-rows: 3fr 9fr 2fr;
  grid-template-columns: 3fr 5fr;
  grid-gap: 2px;
  background-color: yellow;
}

div.container > div {
  background-color: #99c2ff;
}

div.header {
  grid-column: 1/3;
  grid-row: 1/2;
}

div.game-container {
  display: flex;
  grid-column: 1/2;
  grid-row: 2/3;
  text-align: center;
}

div.menu {
  grid-column: 2/3;
  grid-row: 2/3;
}

div.footer {
  grid-column: 1/3;
  grid-row: 3/4;
}


div.game {
  display: grid;
  grid-template-columns: repeat(4, 80px);
  grid-template-rows: repeat(4, 80px);
  margin: auto;
}

div.grid-cell {
  display: flex;
  border: 1px solid rgba(0, 0, 0, 0.8);
  font-size: 20px;
  background-color: #e6f0ff;
}

div.value-cell {
  margin: auto;
}

h1, h2, h3, h4 {
	text-align: center;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="styles.css">
    <title>2048</title>
  </head>

  <body>

    <div class="container">
      <div class="header">
        <h3>Title</h3>
      </div>
      <div class = "game-container">
          <div class = "game">
           <!-- <div class="grid-row"> -->
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
           <!-- </div> -->
           <!-- <div class="grid-row"> -->
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell">2</div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
           <!-- </div> -->
           <!-- <div class="grid-row"> -->
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell">16</div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
           <!-- </div> -->
           <!-- <div class="grid-row"> -->
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
             <div class="grid-cell"><div class="value-cell">1024</div></div>
             <div class="grid-cell"><div class="value-cell"></div></div>
           <!-- </div> -->
           </div>
       </div>
       <div class="menu">
         <p>Some info</p>
       </div>
       <div class="footer">
         <h4>Footer</h4>
       </div>
     </div>

  </body>
</html>