这篇文章给大家分享的是有关CSS中margin属性的用法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
代码入下:HTML文件和CSS文件。
MarginCollapsing01.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <link rel="stylesheet" type="text/css" href="MarginCollapsing01.css" /> </head> <body> <div class="BaseCanvas"> <div class="MarginFrame1">亿速云margin属性的偏移量</div> <div class="MarginFrame2">亿速云</div> </div> </body> </html>
MarginCollapsing01.css
.BaseCanvas { margin-left: 32px; margin-right: 32px; margin-top: 32px; width: calc100%-64px); height: 320px; background-color: #E0E0E0; border: solid 1px #202020; } .MarginFrame1 { width: 80%; background-color: #fff2a9; border: solid 1px #ff6a00; margin-top: 40px; margin-bottom: 40px; margin-left: 12px; margin-right: 8px; } .MarginFrame2 { width: 80%; background-color: #9effb5; margin-top: 40px; margin-bottom: 40px; margin-left: 12px; margin-right: 8px; }
MarginFrame1 在底部设置40个像素的边距,MarginFrame2设置顶部的40个像素的边距。上框和下框之间的距离等于外框和上框之间的距离,从而不获取具有每个边缘的80像素的边缘,并且可以确认只有40像素的距离。
更改margin大小
编辑CSS文件并将MarginFrame 1下方的边距更改为60像素。
MarginCollapsing01.css
.BaseCanvas { margin-left: 32px; margin-right: 32px; margin-top: 32px; width: calc100%-64px); height: 320px; background-color: #E0E0E0; border: solid 1px #202020; } .MarginFrame1 { width: 80%; background-color: #fff2a9; border: solid 1px #ff6a00; margin-top: 40px; margin-bottom: 60px; margin-left: 12px; margin-right: 8px; } .MarginFrame2 { width: 80%; background-color: #9effb5; margin-top: 40px; margin-bottom: 40px; margin-left: 12px; margin-right: 8px; }
设置float的值
代码如下:
MarginCollapsing02.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <link rel="stylesheet" type="text/css" href="MarginCollapsing02.css" /> </head> <body> <div class="BaseCanvas"> <div class="MarginFrame1">框架1</div> <div class="MarginFrame2">框架2</div> <div class="MarginFrame3">框架3</div> <div class="MarginFrame4">框架4</div> </div> </body> </html>
MarginCollapsing02.css
.BaseCanvas { margin-left: 32px; margin-right: 32px; margin-top: 32px; width: calc100%-64px); height: 320px; background-color: #E0E0E0; border: solid 1px #202020; } .MarginFrame1 { width: 160px; float: left; background-color: #fff2a9; border: solid 1px #ff6a00; margin-top: 32px; margin-bottom: 48px; margin-left: 24px; margin-right: 24px; } .MarginFrame2 { width: 160px; float: left; background-color: #fff2a9; border: solid 1px #ff6a00; margin-top: 32px; margin-bottom: 48px; margin-left: 24px; margin-right: 24px; } .MarginFrame3 { width: 160px; float: left; background-color: #fff2a9; border: solid 1px #ff6a00; margin-top: 32px; margin-bottom: 48px; margin-left: 24px; margin-right: 24px; } .MarginFrame4 { width: 160px; float: left; background-color: #fff2a9; border: solid 1px #ff6a00; margin-top: 32px; margin-bottom: 48px; margin-left: 24px; margin-right: 24px; }
效果如下: