CSS to vertically align text to middle of div

前端 未结 6 2091
清酒与你
清酒与你 2021-02-13 14:11

What is the correct way to force text in a div to vertically align to the middle? I have found a couple \'tricks\', but I need something that works with a single line and multip

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 14:11

    If your multi-line elements need to wrap for responsive reasons, then your best bet is with Flexbox. Due to the flakiness of Firefox's 2009 Flexbox implementation, it has to be handled slightly differently than you would do it for modern Flexbox implementations.

    http://codepen.io/cimmanon/pen/mxuFa

    • One lorem ipsum dolor sit amet
    • Two
    • Three
    • Four
    • Five
    li { text-align: center; vertical-align: middle; /* fallback for non-Flexbox browsers */ display: inline-block; /* Flexbox browsers */ display: -webkit-inline-box; display: -moz-inline-box; display: -ms-inline-flexbox; display: -webkit-inline-flex; display: inline-flex; /* vertical centering for legacy, horizontal centering for modern */ -webkit-box-pack: center; -moz-box-pack: center; -ms-flex-pack: center; -webkit-justify-content: center; justify-content: center; /* modern Flexbox only */ -ms-flex-align: center; -webkit-align-items: center; align-items: center; /* legacy Flexbox only */ -webkit-box-orient: vertical; -moz-box-orient: vertical; }

提交回复
热议问题