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. docker镜像push报错2. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?3. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?4. 如何解决docker宿主机无法访问容器中的服务?5. docker api 开发的端口怎么获取?6. android glide asbitmap 在baseadpter中的问题7. 老师,flex-shrink: 1; 按视频操作,不会自动缩放8. python - 字符串里的u’u00a1’怎么打印成中文9. python - 怎样在linux下开发flask web应用时查看代码出错(traceback)的地方。10. PHP中的$this代表当前的类还是方法?

网公网安备