首页 > 编程知识 正文

div水平居中显示三种方式,如何使div居中显示

时间:2023-05-04 14:10:40 阅读:225908 作者:2216

使一个div居中显示的三种方法
1.

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>div居中</title> <style> /*第一种方xdddc/ div{ width: 200px; height: 200px; background-color: orchid; position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; } </style></head><body><div></div></body></html> <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>div居中</title> <style> /*第二种方xdddc/ div{ width: 200px; height: 200px; position: absolute; background-color: orchid; top: 50%; left: 50%; margin-top: -100px; margin-left: -100px; } </style></head><body><div></div></body></html>

使div2在div1里面居中显示

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>div居中</title> <style> /*第三种方xdddc/ #div1 { width: 700px; height: 600px; background-color: orange; display: flex; align-items: center; justify-content: center; } #div2 { width: 200px; height: 200px; background-color: orchid; } </style></head><body><div id="div1"> <div id="div2"></div></div></body></html>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。