如何在 div 盒子内居中按钮?

huangapple go评论71阅读模式
英文:

CSS, how to center the button inside the div box?

问题

你想要将放在名为 newGame 的 div 类中的“New Game”按钮居中。

英文:

this is my first time posting I'm fairly new in front-end web development. I'm having a hard time positioning some of my elements, especially this one every time I change something it doesn't meet my desired position for my button I just need to center it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  margin: 0;
}

.container {
  display: flex;
  justify-content: space-around;
  background: #1B244A;
  width: 575px;
  height: 385px;
}

h3 {
  text-align: center;
  color: white;
  font-size: 40px;
  position: relative;
  top: 25px;
}

.scoreBox {
  background: black;
  width: 155px;
  height: 120px;
  border-radius: 5px;
  text-align: center;
}

.scoreBox h1 {
  font-size: 90px;
  color: red;
  padding: 10px 0;
  font-family: &#39;cursed timer&#39;, sans-serif;
}

.scoreBtn {
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
}

.scoreBtn button {
  background-color: transparent;
  width: 45px;
  height: 45px;
  border-color: #9AABD8;
  border-radius: 5px;
  color: white;
}

.newGame {}

.newGame button {}

<!-- language: lang-html -->

&lt;html&gt;

&lt;head&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;index.css&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;homeTitle&quot;&gt;

      &lt;h3&gt;HOME&lt;/h3&gt;
      &lt;div class=&quot;scoreBox&quot;&gt;
        &lt;h1 id=&quot;scoreHome&quot;&gt;0&lt;/h1&gt;
      &lt;/div&gt;

      &lt;div class=&quot;scoreBtn&quot;&gt;
        &lt;button onclick=&quot;plusOneHome()&quot;&gt;+1&lt;/button&gt;
        &lt;button onclick=&quot;plusTwoHome()&quot;&gt;+2&lt;/button&gt;
        &lt;button onclick=&quot;plusThreeHome()&quot;&gt;+3&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;guestTitle&quot;&gt;
      &lt;h3&gt;GUEST&lt;/h3&gt;
      &lt;div class=&quot;scoreBox&quot;&gt;
        &lt;h1 id=&quot;scoreAway&quot;&gt;0&lt;/h1&gt;
      &lt;/div&gt;

      &lt;div class=&quot;scoreBtn&quot;&gt;
        &lt;button onclick=&quot;plusOneAway()&quot;&gt;+1&lt;/button&gt;
        &lt;button onclick=&quot;plusTwoAway()&quot;&gt;+2&lt;/button&gt;
        &lt;button onclick=&quot;plusThreeAway()&quot;&gt;+3&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;newGame&quot;&gt;
      &lt;button&gt;New Game&lt;/button&gt;
    &lt;/div&gt;

  &lt;/div&gt;
  &lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

I'm trying to figure out how to center the new game button that I put in a div class called newGame it always stays in the right corner.

答案1

得分: 0

使用flex属性**align-items: center;**来在水平方向的flex-direction中垂直居中任何div元素

body {
  margin: 0;
}

.container {
  display: flex;
  justify-content: space-around;
  background: #1b244a;
  width: 575px;
  height: 385px;
}

h3 {
  text-align: center;
  color: white;
  font-size: 40px;
  position: relative;
  top: 25px;
}

.scoreBox {
  background: black;
  width: 155px;
  height: 120px;
  border-radius: 5px;
  text-align: center;
}

.scoreBox h1 {
  font-size: 90px;
  color: red;
  padding: 10px 0;
  font-family: "cursed timer", sans-serif;
}

.scoreBtn {
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
}

.scoreBtn button {
  background-color: transparent;
  width: 45px;
  height: 45px;
  border-color: #9aabd8;
  border-radius: 5px;
  color: white;
}

.newGame {
  display: flex;
  align-items: center;
}
<html>
  <head>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div class="container">
      <div class="homeTitle">
        <h3>HOME</h3>
        <div class="scoreBox">
          <h1 id="scoreHome">0</h1>
        </div>

        <div class="scoreBtn">
          <button onclick="plusOneHome()">+1</button>
          <button onclick="plusTwoHome()">+2</button>
          <button onclick="plusThreeHome()">+3</button>
        </div>
      </div>

      <div class="guestTitle">
        <h3>GUEST</h3>
        <div class="scoreBox">
          <h1 id="scoreAway">0</h1>
        </div>

        <div class="scoreBtn">
          <button onclick="plusOneAway()">+1</button>
          <button onclick="plusTwoAway()">+2</button>
          <button onclick="plusThreeAway()">+3</button>
        </div>
      </div>

      <div class="newGame">
        <button>New Game</button>
      </div>
    </div>
    <script src="index.js"></script>
  </body>
</html>

注:代码部分已排版,但不包含代码翻译。

英文:

Use flex property align-items: center; to center any div element vertically when flex-direction in horizontal

body {
  margin: 0;
}

.container {
  display: flex;
  justify-content: space-around;
  background: #1b244a;
  width: 575px;
  height: 385px;
}
h3 {
  text-align: center;
  color: white;
  font-size: 40px;
  position: relative;
  top: 25px;
}

.scoreBox {
  background: black;
  width: 155px;
  height: 120px;
  border-radius: 5px;
  text-align: center;
}

.scoreBox h1 {
  font-size: 90px;
  color: red;
  padding: 10px 0;
  font-family: &quot;cursed timer&quot;, sans-serif;
}

.scoreBtn {
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
}

.scoreBtn button {
  background-color: transparent;
  width: 45px;
  height: 45px;
  border-color: #9aabd8;
  border-radius: 5px;
  color: white;
}
.newGame {
  display: flex;
  align-items: center;
}

.newGame button {
}

<!-- language: lang-html -->

&lt;html&gt;
  &lt;head&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;./style.css&quot; /&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;homeTitle&quot;&gt;
        &lt;h3&gt;HOME&lt;/h3&gt;
        &lt;div class=&quot;scoreBox&quot;&gt;
          &lt;h1 id=&quot;scoreHome&quot;&gt;0&lt;/h1&gt;
        &lt;/div&gt;

        &lt;div class=&quot;scoreBtn&quot;&gt;
          &lt;button onclick=&quot;plusOneHome()&quot;&gt;+1&lt;/button&gt;
          &lt;button onclick=&quot;plusTwoHome()&quot;&gt;+2&lt;/button&gt;
          &lt;button onclick=&quot;plusThreeHome()&quot;&gt;+3&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;

      &lt;div class=&quot;guestTitle&quot;&gt;
        &lt;h3&gt;GUEST&lt;/h3&gt;
        &lt;div class=&quot;scoreBox&quot;&gt;
          &lt;h1 id=&quot;scoreAway&quot;&gt;0&lt;/h1&gt;
        &lt;/div&gt;

        &lt;div class=&quot;scoreBtn&quot;&gt;
          &lt;button onclick=&quot;plusOneAway()&quot;&gt;+1&lt;/button&gt;
          &lt;button onclick=&quot;plusTwoAway()&quot;&gt;+2&lt;/button&gt;
          &lt;button onclick=&quot;plusThreeAway()&quot;&gt;+3&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;

      &lt;div class=&quot;newGame&quot;&gt;
        &lt;button&gt;New Game&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案2

得分: 0

尝试这个,看看是否适用于你,你可以随时更改数值以匹配按钮的位置,以满足你的需求。

.newGame{
  position: absolute;
  left: 15%;
  top: 40%;
  -webkit-transform: translate(-50%, -50%);
}

我猜这是你期望的效果

英文:

Try this and see if it works for you, and you can always change the values to match the position of button according to your need.

.newGame{
  position: absolute;
  left: 15%;
  top: 40%;
  -webkit-transform: translate(-50%, -50%);
}

I guess this is what you are expecting

huangapple
  • 本文由 发表于 2023年1月6日 13:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定