You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使container内第三个div居中?flex布局设置无效问题

问题:如何将container中的newGame div居中

我尝试将container容器里的newGame div居中,容器应包含homeBox、awayBox和newGame三个div。前两个div通过flexbox已处于正确位置,但第三个div位置异常。我给newGame类设置了display: flex和align-items: center,但并未生效。

第三个div(按钮)位于容器右侧

原始CSS代码

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  font-family: "Karla", sans-serif;
  font-size: 16px;
  background-color: hsl(204, 43%, 93%);
}

section {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100%;
  padding: 100px 30px;
}

.container {
  display: flex;
  justify-content: space-around;
  width: 100%;
  height: 100%;
  max-width: 650px;
  max-height: 385px;
  background: #1b244a;
  border-radius: 20px;
  position: relative;
}

.container>div {
  padding: 30px;
}

h3 {
  text-align: center;
  color: white;
  font-size: 40px;
  margin-bottom: 25px;
}

.scoreBox {
  background: black;
  width: 100%;
  min-width: 155px;
  min-height: 120px;
  border-radius: 15px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.scoreBox h1 {
  color: red;
  font-size: 6.8em;
}

.scoreBtn {
  display: flex;
  justify-content: space-around;
}

.scoreBtn>* {
  padding: 10px 10px;
}

.scoreBtn button {
  background-color: transparent;
  width: 45px;
  height: 45px;
  border-color: #9aabd8;
  border-radius: 5px;
  color: white;
  font-family: 'Karla', sans-serif;
  cursor: pointer;
}

.scoreBtn button:hover {
  background-color: #408c99;
}

.newGame {
  display: flex;
  align-items: center;
}

.newGame button {
  box-shadow: 0px 10px 14px -7px #276873;
  background: linear-gradient(to bottom, #599bb3 5%, #408c99 100%);
  background-color: #599bb3;
  border-radius: 8px;
  display: inline-block;
  cursor: pointer;
  color: #ffffff;
  font-family: Arial;
  font-size: 12px;
  font-weight: bold;
  padding: 13px 5px;
  text-decoration: none;
  text-shadow: 0px 1px 0px #3d768a;
  font-family: 'Karla', sans-serif;
  font-size: 1em;
  text-transform: uppercase;
}

.newGame button:hover {
  background: linear-gradient(to bottom, #408c99 5%, #599bb3 100%);
  background-color: #408c99;
}

.newGame button:active {
  position: relative;
  top: 1px;
}

原始HTML代码

<section>
  <div class="container">
    <div class="homeBox">
      <h3>HOME</h3>
      <div class="scoreBox">
        <h1 id="scoreHome">0</h1>
      </div>

      <div class="scoreBtn">
        <button>+1</button>
        <button>+2</button>
        <button>+3</button>
      </div>


    </div>
    <div class="awayBox">
      <div class="awayBox">
        <h3>AWAY</h3>
        <div class="scoreBox">
          <h1 id="scoreAway">0</h1>
        </div>

        <div class="scoreBtn">
          <button>+1</button>
          <button>+2</button>
          <button>+3</button>
        </div>


      </div>
      <div class="newGame">
        <button>New Game</button>
      </div>
    </div>
</section>

解决方案

1. 修正HTML结构错误

你的HTML存在两个核心问题:

  • awayBox嵌套了一层多余的同名div,导致布局层级混乱;
  • newGame被放在awayBox内部,并非container的直接子元素,因此无法被container的flex布局控制。

修正后的HTML:

<section>
  <div class="container">
    <div class="homeBox">
      <h3>HOME</h3>
      <div class="scoreBox">
        <h1 id="scoreHome">0</h1>
      </div>

      <div class="scoreBtn">
        <button>+1</button>
        <button>+2</button>
        <button>+3</button>
      </div>
    </div>
    <div class="awayBox">
      <h3>AWAY</h3>
      <div class="scoreBox">
        <h1 id="scoreAway">0</h1>
      </div>

      <div class="scoreBtn">
        <button>+1</button>
        <button>+2</button>
        <button>+3</button>
      </div>
    </div>
    <!-- 将newGame移至container下作为直接子元素 -->
    <div class="newGame">
      <button>New Game</button>
    </div>
  </div>
</section>

2. 调整CSS布局

现在newGame已成为container的直接子元素,需要调整flex属性让它实现居中:

修改container的CSS:

.container {
  display: flex;
  flex-wrap: wrap; /* 允许子元素换行 */
  justify-content: space-around;
  align-items: center; /* 垂直方向居中对齐 */
  width: 100%;
  max-width: 650px;
  background: #1b244a;
  border-radius: 20px;
  padding: 30px; /* 给容器统一加内边距,替代子元素的padding */
}

/* 移除原有的container子元素padding,避免重复 */
/* .container>div { padding: 30px; } */

修改newGame的CSS:

.newGame {
  display: flex;
  justify-content: center; /* 水平居中按钮 */
  width: 100%; /* 占满整行,确保在容器居中 */
  margin-top: 20px; /* 与上方得分区域拉开间距 */
}

效果说明

调整后,homeBox和awayBox会在容器第一行左右分布,newGame会单独占一行并水平居中,按钮也会在newGame容器内居中显示。


内容的提问来源于stack exchange,提问作者oceanotrash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 12:47:30