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. python - 求一个在def中可以实现调用本def满足特定条件continue效果的方法(标题说不太清楚,请见题内描述)2. $fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题3. USE关键字4. javascript - mysql插入数据时怎样避免与库中的数据重复?5. MySQL 这句 创建表结构语句的错误在哪?6. 导入phpmyadmin的时候报错了7. MySQL能否给某个字段的值设置有效期?8. MYSQL新建用户设置可以远程访问的问题9. mysql数据库做关联一般用id还是用户名10. MySQL 如何 SELECT 除去某一列的所有列?
![$fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题](http://www.haobala.com/attached/image/news/202205/093622cb60.png)