css - 為何flex的align-items: center無法垂直居中?
問題描述
參看語法規(guī)則完成練習(xí),我只能水平居中,無法垂直居中,是少設(shè)置了什么嗎?我想實現(xiàn)的是在位置高度的情況下實現(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 一個高度,這樣可以垂直居中了
html,body,.box1{ display: flex; justify-content: center; align-items: center; height:100%;}
相關(guān)文章:
1. win10 Apache24+PHP8.0,Apache不能正常加載php.ini。2. python - (2006, ’MySQL server has gone away’)3. python - PyCharm里的一個文件不小心忽略了wx包4. http - python requests上傳文件問題5. javascript - 關(guān)于微信掃一掃的技術(shù)問題6. python - 使用eclipse運行django代碼,修改了views.py這個文件,但是瀏覽器顯示的還是原有沒修改的結(jié)果,怎么處理?7. 小白學(xué)python的問題 關(guān)于%d和%s的區(qū)別8. django - pycharm 如何配置 python3 的開發(fā)環(huán)境?9. html - Python2 BeautifulSoup 提取網(wǎng)頁中的表格數(shù)據(jù)及連接10. android - VideoView與百度Map沖突
