为什么我的flexbox CSS不能正常工作?

ie3xauqp  于 2023-02-10  发布在  其他
关注(0)|答案(2)|浏览(138)

为什么display:flex在我的CSS代码中不起作用?我想把这部分HTML代码放在页面的中心。

body {
  margin: 0;
  display: flex auto 0 1;
  justify-content: center;
  height: 100vh;
}
<form class="form" id="form"></form>
<h4 class="score" id="score">score :0</h4>
<h1 id="Question">What is 1 multiply by 1?</h1>
<input type="text" class="input" id="input" placeholder="Enter your answer" autofocus autocomplete="off">
<button type="submit" class="btn">submit</button>
8cdiaqws

8cdiaqws1#

flex auto 0 1display的无效属性值。浏览器的文档检查器将向您显示此信息。您需要将displayflex属性中的这些值分隔开。
我猜想您也需要flex-direction: column,但我将把它留给您。

body {
  margin: 0;
  display: flex;
  flex: auto 0 1;
  justify-content: center;
  height: 100vh;
}
<form class="form" id="form"></form>
<h4 class="score" id="score">score :0</h4>
<h1 id="Question">What is 1 multiply by 1?</h1>
<input type="text" class="input" id="input" placeholder="Enter your answer" autofocus autocomplete="off">
<button type="submit" class="btn">submit</button>
3mpgtkmj

3mpgtkmj2#

关键是把它 Package 在一个容器中,使它成为内联块,然后使用flex将它居中:

body {
  margin: 0;
  display: flex ;
  justify-content:center;
  align-items:center;
  height: 100vh;

}
.container{
  display:inline-block;
}
<div class="container">
    <form class="form" id="form"></form>
   <h4 class="score" id="score">score :0</h4>
  <h1 id="Question">What is 1 multiply by 1?</h1>
  <input type="text" class="input" id="input" 
   placeholder="Enter your answer" autofocus        autocomplete="off">
   <button type="submit" class="btn">submit</button>
</div>

相关问题