标签(空格分隔): margin居中css
以下图的代码查看:html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>margin塌陷</title> <style type="text/css"> *{ padding:0; margin:0; } .a{ width:780px; height:50px; background-color: red; margin:0px auto; } </style> </head> <body> <div class="a"></div> <div class="b"></div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>margin塌陷</title> <style type="text/css"> *{ padding:0; margin:0; } div{ width:780px; height:50px; background-color: red; margin:0px auto; text-align: center; } /*水平居中必须有width,必需要明确width,文字水平居中使用text-align:center 2.只有标准流下的盒子才能使用margin:0auto 3.当一个盒子浮动了,固定定位了,绝对定位了,margin:0auto不能用了, 4.margin是居中盒子,不是居中文本*/ </style> </head> <body> <div>文字</div> <div class="a"></div> </body> </html>
- 总结: 水平居中必须有width,必需要明确width,文字水平居中使用text-align:center 2.只有标准流下的盒子才能使用margin:0auto 3.当一个盒子浮动了,固定定位了,绝对定位了,margin:0auto不能用了, 4.margin是居中盒子,不是居中文本