css - 為何flex的align-items: center無法垂直居中?
問題描述
參看語法規(guī)則完成練習(xí),我只能水平居中,無法垂直居中,是少設(shè)置了什么嗎?我想實(shí)現(xiàn)的是在位置高度的情況下實(shí)現(xiàn)居中,這樣可以用到flex嗎?
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title> <style>.box1{ width: 400px; height: 200px; background: #ccc; position: relative;}.leftCircle{ position: absolute; left: 0; top: 0; background: #fc0; width: 50px; height:50px; -webkit-border-radius:0 0 50px 0; -moz-border-radius:0 0 50px 0; border-radius:0 0 50px 0;}.rightCircle{ position: absolute; right: 0; bottom: 0; background: #fc0; width: 50px; height:50px; -webkit-border-radius:50px 0 0 0; -moz-border-radius:50px 0 0 0; border-radius:50px 0 0 0;}body{ display: flex; justify-content: center; align-items: center;} </style></head><body><p class='box1'> <p class='leftCircle'></p> <p class='rightCircle'></p></p></body></html>
問題解答
回答1:給body 一個(gè)高度,這樣可以垂直居中了
html,body,.box1{ display: flex; justify-content: center; align-items: center; height:100%;}
相關(guān)文章:
1. 鏈接圖片時(shí),鏈接不成功2. mysql - redis和mongodb怎么結(jié)合3. error_log 指定不能生存錯(cuò)誤日志的地址4. javascript - 正則匹配大于0,不大于16,不超過兩位小數(shù)5. php安裝lpsolve 擴(kuò)展求助6. 老師,樣式?jīng)]有變呀?7. python - type函數(shù)問題8. pbootcms程序的詳情頁模板想要實(shí)現(xiàn)多版塊展現(xiàn),說不清楚,我截圖在里面9. html5和Flash對(duì)抗是什么情況?10. button按鈕點(diǎn)擊了沒有任何反應(yīng)
