多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# CSS margin-top 属性 ## 实例 设置 p 元素的上外边距: ``` p { margin-top:2cm; } ``` (在页面底部可以找到更多实例) ## 浏览器支持 | IE | Firefox | Chrome | Safari | Opera | | --- | --- | --- | --- | --- | 所有主流浏览器都支持 margin-top 属性。 注释:任何的版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit"。 ## 定义和用法 margin-top 属性设置元素的上外边距。 注释:允许使用负值。 | 默认值: | 0 | | --- | --- | | 继承性: | no | | --- | --- | | 版本: | CSS1 | | --- | --- | | JavaScript 语法: | _object_.style.marginTop="10px" | | --- | --- | ## 可能的值 | 值 | 描述 | | --- | --- | | auto | 浏览器设置的上外边距。 | | _length_ | 定义固定的上外边距。默认值是 0。 | | _%_ | 定义基于父对象总高度的百分比上外边距。 | | inherit | 规定应该从父元素继承上外边距。 | ## TIY 实例 [设置文本的上外边距 1](/tiy/t.asp?f=csse_margin-top) 本例演示如何使用厘米值来设置文本的顶边距。 ``` <html> <head> <style type="text/css"> p.topmargin {margin-top: 5cm} </style> </head> <body> <p>这个段落没有指定外边距。</p> <p class="topmargin">这个段落带有指定的上外边距。</p> </body> </html> ``` [设置文本的上外边距 2](/tiy/t.asp?f=csse_margin-top_percent) 本例演示如何使用百分比值来设置文本的顶边距。 ``` <html> <head> <style type="text/css"> p.topmargin { margin-top: 25% } </style> </head> <body> <p>This is a paragraph with no margin specified</p> <p class="topmargin">This is a paragraph with a specified top margin</p> </body> </html> ``` ## 相关页面 CSS 教程:[CSS 外边距](/css/css_margin.asp "CSS 外边距") HTML DOM 参考手册:[marginTop 属性](/jsref/prop_style_margintop.asp "HTML DOM marginTop 属性")