css single or multiple line vertical align

后端 未结 8 2002
北荒
北荒 2020-11-28 21:04

I have a title that can have one or more lines.

How can I align the text vertically? If it was always one line I could just set the line-height to the container heig

8条回答
  •  暖寄归人
    2020-11-28 21:27

    Instead of using vertical-align: center and display: table-cell, you might want to take a look at the newer method called CSS display flex which is MUCH simpler

    .box {
        width: 200px;
        height: 50px; 
        padding: 10px;
        margin-bottom: 20px;
        background-color: red;
        
        /* Only add these 2 lines */
        display: flex;
        align-items: center;
      }
    Lorem ipsum dolor
    Lorem ipsum dolor sit amet ipsum dolor

提交回复
热议问题