css - 為何flex的align-items: center無法垂直居中?
問題描述
參看語法規則完成練習,我只能水平居中,無法垂直居中,是少設置了什么嗎?我想實現的是在位置高度的情況下實現居中,這樣可以用到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%;}
相關文章:
1. index.php錯誤,求指點2. 微信公眾號在線生成二維碼帶參數怎么搞?3. 微信開放平臺 - android 微信支付后點完成按鈕,后回調打開第三方頁面,屏幕閃動,求解決方法4. html5 - H5 video 問題5. javascript - umeditor設置字體,字號失效,不過字體顏色,加粗等功能正常6. css - 移動端h5播放器問題求解決,急急7. PHP單例模式8. javascript - 微信報redirect_uri參數錯誤9. android - 類似微信朋友圈或者QQ空間說說那種點擊圖片放大,并且有放大縮小手勢,左右滑動圖片手勢效果10. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?
