CSS margin-bottom属性实例讲解

发布时间:2025-12-09 11:58:58 浏览次数:1

CSS margin-bottom属性用于指定要在元素底部使用的边距量。可以按长度或百分比设置边距。

用法:

margin-bottom:<length> | <percentage> | auto

属性值:

  • Length:此值指定具有固定值的边距长度。该值可以为正,负或零。

    例:



    <!DOCTYPE html> <html> <head>     <title>CSS margin-bottom</title>     <style>         p{             background-color:lightgreen;         }     </style> </head> <body>     <h1 style="color:green">GeeksforGeeks</h1>           <!-- margin-bottom for below p          is set to 50px -->    <p style="margin-bottom:50px">Line One</p>           <!-- margin-bottom for below p          is set to 0px -->    <p style="margin-bottom:0px">Line Two</p>           <p>Line Three</p> </body> </html>                    

    输出:

  • Percentage:此值指定相对于包含元素的宽度的百分比裕量。

    例:

    <!DOCTYPE html> <html> <head>     <title>CSS margin-bottom</title>     <style>         h1 {             color:green;         }                   .larger {             width:300px;             background-color:white;         }                   .smaller {             width:100px;             background-color:white;         }                   p{             background-color:lightgreen;         }     </style> </head> <body>     <h1>GeeksforGeeks</h1>           <!-- margin-bottom is set to 10% with width of          containing box set to 300px -->    <p >         <p style="margin-bottom:10%";>Line One</p>         <p>Line Two</p>     </p>           <!-- margin-bottom is set to 10% with width of          containing box set to 100px -->    <p >         <p style="margin-bottom:10%;">Line One</p>         <p>Line Two</p>     </p> </body> </html>                    

    输出:

  • auto:如果此属性的值设置为“auto”,则浏览器将自动为边距大小计算一个合适的值。

支持的浏览器:下面列出了margin-bottom属性支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • Firefox
  • Opera
  • Safari
margin-bottom
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477