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. html5 - 只用CSS如何实现input框的宽度随框里输入的内容长短自动适应?2. PHP类中的$this3. boot2docker无法启动4. html - 急求,能否用flex或者高端点的CSS来布局这个图呢?不想用浮动或定位了5. javascript - 初学angularJS+express,路由路径中/转换成%2F,导致路径失效,求原因?6. javascript - 关于jsp中嵌入js代码的问题7. javascript - 百度坐标,返回目标附近有哪些坐标8. html5 - 在微信上扫二维码看到有趣的h5页面,怎样才可以看到它的代码呢?9. Python操作sqlserver是异步的吗?10. javascript - 前端如何实现这种美图秀秀效果

网公网安备