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. java固定键值转换,使用枚举实现字典?2. javascript - 移动端开发 H5 页面在 iOS手机上无法实现 长按复制文本 求解决3. 如何解决tp6在zend中无代码提示4. java - HTTPS双向认证基础上有无必要再进行加签验签?5. vim - win10无法打开markdown编辑器6. html - 如何使用用户输入的数据去运行一个数学公式,最后怎么返回。7. python - flask学习,user_syy添加报role is invalid keyword for User.8. 这是什么情况???9. css3 - less或者scss 颜色计算的知识应该怎么学?或者在哪里学?10. javascript - 有没有类似高铁管家的时间选择插件
