css 在容器div内居中放置第三个div [重复]

cld4siwp  于 2023-03-05  发布在  其他
关注(0)|答案(5)|浏览(138)
    • 此问题在此处已有答案**:

Flexbox: center horizontally and vertically(14个答案)
(133个答案)
3天前关闭。
我试图找出这个特定的div块的中心,容器有3个div框,分别是homeBox、awayBox和newGame。前两个框的位置很好,因为我使用了flexbox,但问题是第三个div块的位置不对,我试图为newGame类编写一个display flex,将其设置为align-item: center,但它不起作用。

* {
  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;
}
<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>
ktecyv1j

ktecyv1j1#

尝试添加
边距:自动;
到. newGame按钮

ca1c2owp

ca1c2owp2#

请记住.newgame位于.awayBox内,因此,要使.newgame按钮相对于该框居中,只需移除

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

并将其更改为:

.newGame {
  text-align:center;
}

如果要将其相对于上面两个div居中,请将html移出awayBox

a14dhokn

a14dhokn3#

你有一个div没有关闭。awayBox被重复使用了!
用网格怎么样?网格实际上是建立二维结构。

* {
  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: grid;
  grid-template-rows: 1fr 1fr;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  max-width: 650px;
  max-height: 385px;
  background: #1b244a;
  border-radius: 20px;
  padding: 20px;
  position: relative;
}

.container>div {
  align-self: center;
  justify-self: center;
}

.homeBox {
  grid-area: 1 / 1 / 2 / 2;
}

.awayBox {
  grid-area: 1 / 2 / 2 / 3;
}

.newGame {
  grid-area: 2 / 1 / 3 / 3;
}

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 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;
}
<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>
    <div class="newGame">
      <button>New Game</button>
    </div>
  </div>
</section>
ma8fv8wu

ma8fv8wu4#

我将引入rows,类为container-row
这样,您就可以更改每行的格式,使按钮在另一行的下方居中

* {
  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;
  flex-direction: column;
}

.container>div {
  padding: 30px;
}

.container-row {
  display: flex;
  justify-content: center;
}

.space-around {
  justify-content: space-around;
}

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;
}
<section>
    <div class="container">
        <div class="container-row space-around">
            <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>
        </div>
        <div class="container-row">
            <div class="newGame">
                <button>New Game</button>
            </div>
        </div>    
    </div>
</section>
1cklez4t

1cklez4t5#

我通过使用网格解决了这个问题,我还发现你的HTML代码中有一些问题,所以这里是代码。
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="newGame">
            <button>New Game</button>
          </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>
    </section>

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: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px , 1fr));
  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 {
  text-align: center;
  margin: auto;
}

.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;
}

相关问题