CSS: How to align vertically a “label” and “input” inside a “div”?

后端 未结 7 1356
刺人心
刺人心 2020-11-29 17:59

Consider the following code:

HTML:

7条回答
  •  误落风尘
    2020-11-29 18:14

    a more modern approach would be to use css flex-box.

    div {
      height: 50px;
      background: grey;
      display: flex;
      align-items: center
    }

    a more complex example... if you have multible elements in the flex flow, you can use align-self to align single elements differently to the specified align...

    div {
      display: flex;
      align-items: center
    }
    
    * {
      margin: 10px
    }
    
    label {
      align-self: flex-start
    }

    its also super easy to center horizontally and vertically:

    div {
      position:absolute;
      top:0;left:0;right:0;bottom:0;
      background: grey;
      display: flex;
      align-items: center;
      justify-content:center
    }

提交回复
热议问题