代码之家  ›  专栏  ›  技术社区  ›  Ujjawal Bhandari

<div>屏幕中心不是元素的文本

  •  0
  • Ujjawal Bhandari  · 技术社区  · 3 年前

    我希望div的元素位于屏幕的中心,但不希望其内部的文本。例如 Sample size (n) 应该在下面的代码中左对齐。

    .center-screen {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      text-align: center;
    }
    <div class='center-screen'>
    <label class='input2' for='pop'><b>Population proportion (p)</b></label>
    <input class='input2' type='number' step='0.01' id='pop' value='0.43'/>
    <label class='input2' for='samplesize'><b>Sample size (n)</b></label>
    <input class='input2' type='number' id='samplesize' value='40'/>
    <input class='input2' type='button' value='Calculate' />
    </div>
    1 回复  |  直到 3 年前
        1
  •  1
  •   Maik Lowrey    3 年前

    可以用div flex容器包装。然后只对齐子容器。然后从子容器中移除文本对齐。

    .container {
      display: flex;
      justify-content: center;
    }
    .center-screen {
      display: flex;
      flex-direction: column;
      justify-content: center;
      
    }
    <div class="container">
      <div class='center-screen'>
        <label class='input2' for='pop'><b>Population proportion (p)</b></label>
        <input class='input2' type='number' step='0.01' id='pop' value='0.43'/>
        <label class='input2' for='samplesize'><b>Sample size (n)</b></label>
        <input class='input2' type='number' id='samplesize' value='40'/>
        <input class='input2' type='button' value='Calculate' />
      </div>
    </div>