[提高总结三]css的居中对齐

    xiaoxiao2021-03-25  176

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>右侧导航菜单练习</title> <link rel="stylesheet" href="css/main.css"> <script src="js/main.js"></script> </head> <body> <button>按钮</button> <div class="testCenter"> <div class="testCenter2"> <div class="testCenter3"> <div class="testCenter4"> </div> </div> </div> </div> </body> </html> /* 1: block = inline-block + width:100% 2: inline-block = inline + 换行 3: inline情况下,height和width都不起作用 4: padding的垂直效果 = display不能为inline */ .testCenter { position: absolute; width: 500px; height: 500px; background-color: red; border: solid 1px yellow; left: 0; right: 0; top: 0; bottom: 0; margin: auto; } /*推荐用法*/ .testCenter2 { position: absolute; width: 400px; height: 400px; background-color: green; left: 50%; top: 50%; margin: -200px 0 0 -200px; } .testCenter3 { position: absolute; width: 300px; height: 300px; background-color: royalblue; left: 50%; top: 50%; transform: translate(-50%, -50%); /*设置这三个属性,子元素就能居中了,ie11以下不支持*/ justify-content: center; align-items: center; display: flex; } .testCenter4 { width: 200px; height: 200px; background-color: yellow; }

    下一篇要看阮老师的flex布局,弹性布局。

    http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

    转载请注明原文地址: https://ju.6miu.com/read-662.html

    最新回复(0)