首页 技术 正文
技术 2022年11月8日
0 收藏 645 点赞 1,217 浏览 1899 个字

margin

外边距,表示边框到最近盒子的距离。

对于左右两边

 <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>流浪者</title>     <style>         *{             padding: 0px;             margin: 0px;         }         .box1{             width: 200px;             height: 200px;             background-color: red;             display: inline-block;             margin-right: 20px;         }         .box2{             width: 200px;             height: 200px;             background-color: green;             display: inline-block;             margin-left: 20px;         }     </style> </head> <body> <span class="box1"></span> <span class="box2"></span> </body> </html>

07-简单认识margin

对于上下盒子

 <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>流浪者</title>     <style>         *{             padding: 0px;             margin: 0px;         }         .box1{             width: 200px;             height: 200px;             background-color: red;             display: block;             margin-bottom: 20px;         }         .box2{             width: 200px;             height: 200px;             background-color: green;             display: block;             margin-top: 60px;         }     </style> </head> <body> <span class="box1"></span> <span class="box2"></span> </body> </html>

07-简单认识margin

这种现象叫做margin塌陷问题,当给两个标准文档流下的盒子设置垂直方向上的margin时,以较大的为准。

当我们给两个标准流下的兄弟盒子设置浮动之后,它们脱离了标准文档流,就不会出现margin塌陷的问题。

 <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>流浪者</title>     <style>        *{             padding: 0;             margin: 0;         }         .father{             width: 400px;             overflow: hidden;             border: 1px solid gray;         }         .box1{             width: 300px;             height: 200px;             background-color: red;             margin-bottom: 20px;             float: left;         }         .box2{             width: 400px;             height: 300px;             background-color: green;             margin-top: 50px;             float: left;         }     </style> </head> <body> <div class="father">     <div class="box1"></div>     <div class="box2"></div> </div> </body> </html>

07-简单认识margin

margin:0  auto;

设置标准文档流下的盒子居中。

如果给盒子设置了浮动,则margin:0 auto失效。

 <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>流浪者</title>     <style>        *{             padding: 0;             margin: 0;         }         .box1{             width: 200px;             height: 200px;             background-color: red;             margin: 0 auto;         }         .box2{             width: 400px;             height: 300px;             background-color: green;         }     </style> </head> <body> <div class="box1"></div> <div class="box2"></div> </body> </html>

07-简单认识margin

使用margin:0 auto;注意点:

1.使用margin: 0 auto;水平居中盒子必须有width,要有明确width,文字水平居中使用text-align: center;

2.只有标准流下的盒子 才能使用margin:0 auto;

当一个盒子浮动了,固定定位,绝对定位(后面会讲),margin:0 auto; 不能用了

3.margin:0 auto;居中盒子。而不是居中文本,文字水平居中使用text-align: center;

作者:流浪者

日期:2019-08-31

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,956
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,480
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,327
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,110
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,741
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,776