准备
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .in{ border: 1px solid black; height: 100px; width: 100px; } </style> </head> <body> <div> <div class="in" style="background-color: red;"> </div> <div class="in" style="background-color: green;"></div> <div class="in" style="background-color: yellow;"></div> </div> </body> </html>
只有一个值的时候是把四周都设为同样的外边距
... <div> <div class="in" style="background-color: red;"> </div> <!--设置margin样式属性--> <div class="in" style="background-color: green;margin: 20px;"></div> <div class="in" style="background-color: yellow;"> </div> </div> ...
两个值对应的第一个值是上下外边距,第二个是左右外边距
<div> <div class="in" style="background-color: red;"> </div> <!--设置margin样式属性上下为40px,左右为20px--> <div class="in" style="background-color: green;margin: 40px 20px;"></div> <div class="in" style="background-color: yellow;"> </div> </div>
三个值对应的第一个值是上外边距,第二个是左右外边距,第三个是下外边距
<div> <div class="in" style="background-color: red;"> </div> <!--设置margin样式属性上外边距为40px,左右外边距为10px,下外边距为60px--> <div class="in" style="background-color: green;margin: 40px 10px 60px;"></div> <div class="in" style="background-color: yellow;"> </div> </div>
四个值对应的为顺时针方向,依次为上右下左外边距
<div> <div class="in" style="background-color: red;"> </div> <!--设置margin样式属性,上右下左外边距依次为10px,20px,40px,60px--> <div class="in" style="background-color: green;margin: 10px 20px 40px 60px;"></div> <div class="in" style="background-color: yellow;"> </div> </div>
设置上外边距
<div> <div class="in" style="background-color: red;"> </div> <div class="in" style="background-color: green;margin-top: 10px;"></div> <div class="in" style="background-color: yellow;"> </div> </div>
设置右外边距
<div> <div class="in" style="background-color: red;"> </div> <div class="in" style="background-color: green;margin-right: 10px;"></div> <div class="in" style="background-color: yellow;"> </div> </div>
设置下外边距
<div> <div class="in" style="background-color: red;"> </div> <div class="in" style="background-color: green;margin-bottom: 10px;"></div> <div class="in" style="background-color: yellow;"> </div> </div>
设置左外边距
<div> <div class="in" style="background-color: red;"> </div> <div class="in" style="background-color: green;margin-left: 10px;"></div> <div class="in" style="background-color: yellow;"> </div> </div>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #out{ border: 1px solid black; height: 300px; width: 300px; } .in{ height: 100px; width: 100px; } </style> </head> <body> <div id="out"> <div class="in" style="background-color: red;"> </div> <div class="in" style="background-color: green;"></div> <div class="in" style="background-color: yellow;"></div> </div> </body> </html>
结果
只有一个值的时候是把四周都设为同样的外边距
有两个值的时候是第一个值为上下内边距,第二个值为左右内边距
三个值为第一个值为上内边距,第二个值为左右内边距,第三个值为下内边距
四个值的时候,为顺时针顺序指定,上右下左。
#out{ border: 1px solid black; height: 300px; width: 300px; padding: 10px 20px 30px 40px; }
结果
使用浏览器的F12查看
指定上内边距
指定右内边距
指定下内边距
指定左内边距
#out{ border: 1px solid black; height: 300px; width: 300px; padding-left: 30px; }