代码之家  ›  专栏  ›  技术社区  ›  Jonas

为什么我必须显示:块项目,以便它们在flexbox中彼此下方对齐?

  •  1
  • Jonas  · 技术社区  · 4 年前

    下面的CSS代码应该使表单的每一项都位于一列中,比如,一列在另一列的下面,但除非我添加代码,否则它不会工作:

    input {
        display:block;
    }
    

    #flex 规则块?

        * {
            box-sizing: border-box;
            margin: 0;
            padding:0
        }
        
        #flex {
            display: flex;
            flex-direction: column;
            flex-wrap: wrap;
            justify-content: center;
            align-items: center;
            align-content: center;
            justify-items: center;
            height: 90vh;
        }
    <div id='flex'>
          <form id='something'>
                    <input type='email' />
                    <input type='password' />
                    <button>Entrar</button>
                </form>
                </div>
    2 回复  |  直到 4 年前
        1
  •  2
  •   Johannes    4 年前

    你的 #flex #something ,因此flex设置几乎没有效果,它们肯定不会影响孙子辈 input button ,但只有直接的孩子( )。将您的设置应用于 #某物 -这将产生你所要求的效果,因为它会直接影响你的孩子 :

    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0
    }
    
    #something {
      display: flex;
      flex-direction: column;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;
      align-content: center;
      justify-items: center;
      height: 90vh;
    }
    <div id='flex'>
      <form id='something'>
        <input type='email' />
        <input type='password' />
        <button>Entrar</button>
      </form>
    </div>
        2
  •  2
  •   Alohci    4 年前

    display:contents

    * {
        box-sizing: border-box;
        margin: 0;
        padding:0
    }
    
    #something {
        display:contents;
    }
        
    #flex {
        display: flex;
        flex-direction: column;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        align-content: center;
        justify-items: center;
        height: 90vh;
    }
    <div id='flex'>
      <form id='something'>
          <input type='email' />
          <input type='password' />
          <button>Entrar</button>
      </form>
    </div>