html - 垂直和水平居中 flexbox

标签 html css flexbox center

<分区>

我尝试将 div 骰子包装器和 Button Horizo​​ntaly 和 Verticaly 居中。问题是,如果我给骰子包装器 100% 高度,则可以很好地处理骰子,但按钮现在位于页。当我将按钮 div 放入骰子 div 中时,按钮位于正确的位置,我无法进入骰子下方。

    html,
    body {
      height: 100%;
    }
    
    .container {
      height: 100%;
    }
    
    #dice-wrapper {
      display: flex;
      justify-content: center;
      align-items: center;
      height: auto;
      border: cornflowerblue solid 1px;
    }
    
    .die img {
      max-width: 7rem;
    }
    
    .btn {
      display: flex;
      border: cornflowerblue solid 1px;
    }
     <body>
        <div class="container">
          <div id="dice-wrapper">
            <div class="die">
              <input type="checkbox" id="dice-1" name="dice-1" value="dice-1" />
              <img src="img/dice-5.png" alt="Dice" class="dice1" id="dice-1" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-2" name="dice-2" value="dice-2" />
              <img src="img/dice-5.png" alt="Dice" class="dice2" id="dice-2" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-3" name="dice-3" value="dice-3" />
              <img src="img/dice-5.png" alt="Dice" class="dice3" id="dice-3" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-4" name="dice-4" value="dice-4" />
              <img src="img/dice-5.png" alt="Dice" class="dice4" id="dice-4" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-5" name="dice-5" value="dice-5" />
              <img src="img/dice-5.png" alt="Dice" class="dice5" id="dice-5" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-6" name="dice-6" value="dice-6" />
              <img src="img/dice-5.png" alt="Dice" class="dice6" id="dice-6" />
            </div>
          </div>
          <div class="btn">
            <button class="btn_roll">roll</button>
          </div>
        </div>
        <script type="text/javascript" src="js/app.js"></script>
      </body>
    
  

最佳答案

试试这个答案。 在这里,我用具有 .dies 类的父 div 包装了 .die div。并将 .dies 容器和 .btn 容器放入 #dice-wrapper 容器中。

    html,
    body {
      height: 100%;
    }
    
    .container {
      height: 100%;
    }
    
    #dice-wrapper {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100%;
      border: cornflowerblue solid 1px;
    }

    .dies {
      display: flex;
      flex-direction: row;
    }
    
    .die img {
      max-width: 7rem;
    }
    
    .btn {
      display: flex;
      flex-direction: row;
      border: cornflowerblue solid 1px;
    }
     <body>
        <div class="container">
          <div id="dice-wrapper">
          <div class="dies">
            <div class="die">
              <input type="checkbox" id="dice-1" name="dice-1" value="dice-1" />
              <img src="img/dice-5.png" alt="Dice" class="dice1" id="dice-1" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-2" name="dice-2" value="dice-2" />
              <img src="img/dice-5.png" alt="Dice" class="dice2" id="dice-2" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-3" name="dice-3" value="dice-3" />
              <img src="img/dice-5.png" alt="Dice" class="dice3" id="dice-3" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-4" name="dice-4" value="dice-4" />
              <img src="img/dice-5.png" alt="Dice" class="dice4" id="dice-4" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-5" name="dice-5" value="dice-5" />
              <img src="img/dice-5.png" alt="Dice" class="dice5" id="dice-5" />
            </div>
            <div class="die">
              <input type="checkbox" id="dice-6" name="dice-6" value="dice-6" />
              <img src="img/dice-5.png" alt="Dice" class="dice6" id="dice-6" />
            </div>
            </div>
          <div class="btn">
            <button class="btn_roll">roll</button>
          </div>
          </div>
          
        </div>
        <script type="text/javascript" src="js/app.js"></script>
      </body>
    
  

关于html - 垂直和水平居中 flexbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58746009/

上一篇:html - 当将鼠标悬停在一个菜单项上到另一个菜单项时,它会与前一个菜单项重叠吗?请测试菜单功能?

下一篇:css - 为类添加CSS

相关文章:

javascript - 为什么我会在 HTML 和 CSS 中使用类而不是属性?

css - 为什么 justify-content : space-evenly not valid in Atom?

html - Div 在 Google chrome 中不显示(在 firefox 中显示)

jquery - 苹果网络应用程序状态栏检测纵向或横向不工作

html - 水平居中 flex 容器

html - Div 在另一个 div 下消失

HTML & CSS - 将 <link> 标签放在 <head> 之外

css - 脱离父 div

html - 在 CSS 中使图像适合 flexbox 容器

html - 在html和css中制作全高div