亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在flexbox中的圖像上居中文本

在flexbox中的圖像上居中文本

慕絲7291255 2019-08-13 17:10:45
在flexbox中的圖像上居中文本如何將文本居中對齊<img>優選使用FlexBox?body {   margin: 0px;}.height-100vh {   height: 100vh;}.center-aligned {   display: box;   display: flex;   box-align: center;   align-items: center;   box-pack: center;   justify-content: center;}.background-image {   position: relative;}.text {   position: absolute;}<section class="height-100vh center-aligned">   <img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" />   <div class="text">SOME TEXT</div></section>
查看完整描述

3 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

要將文本居中于圖像上,您不需要flexbox。只需使用CSS定位屬性。

.height-100vh {
    height: 100vh;
    position: relative;               /* establish nearest positioned ancestor for
                                         absolute positioning */}.text {
    position: absolute;  
    left: 50%;                        /* horizontal alignment */
    top: 50%;                         /* vertical alignment */
    transform: translate(-50%, -50%); /* precise centering; see link below */}

修改了Codepen

上面的代碼將文本在圖像上垂直和水平居中:



查看完整回答
反對 回復 2019-08-13
  • 3 回答
  • 0 關注
  • 801 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號