CSS margin-top属性,设置上外边距
margin-top属性
HTML的网页设计中,上外边距该怎么设置呢?CSS中的margin-top属性可以为HTML文档中的元素设置上外边距,允许负值,默认情况下为0。
margin-top属性的值可以是如下几种:
值 | 描述 |
---|---|
百分比 | 单位为%,长度是相对于元素宽度的百分比,自适应的页面比较常用 |
length | 长度值,单位为px、pt、cm等 |
auto | 让浏览器自动计算外边距,可以用于让元素水平居中 |
inherit | 从父元素继承外边距属性 |
margin-top属性实例代码,及在线编辑器
<div class='x1'>
<div class='y1'></div>
</div>
<style>
.x1{width:100%;height:100%;border:1px solid YellowGreen;}
.y1{background-color:skyblue;width:50%;height:20%;margin-top:15%;}
</style>