當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


CSS line-height用法及代碼示例


CSS中的line-height屬性用於設置用於行的空間量,例如文本中的空間。不允許使用負值。

用法:

line-height:normal|number|length|percentage|initial|inherit;

屬性值


  • 正常:此模式代表法線高度。這是默認值。
    line-height:normal;
    

    例:

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>CSS line-height Property</title> 
            <style> 
                .geek { 
                  line-height:normal; 
                  background:green; 
                  color:white; 
                } 
            </style> 
        </head> 
        <body style = "text-align:center;"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                CSS line-height Property 
            </h2> 
              
            <div class="geek"> 
                A computer science portal for geeks.<br> 
                This div has line-height:normal; 
            </div> 
        </body> 
    </html>

    輸出:
    lineheight

  • 數:該值是一個無單位數乘以當前的font-size來設置行高。在大多數情況下,這是設置line-height並避免由於繼承而導致意外結果的首選方法。
    line-height:number;
    

    例:

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>CSS line-height Property</title> 
            <style> 
                .geek { 
                  line-height:2.5; 
                  background:green; 
                  color:white; 
                } 
            </style> 
        </head> 
        <body style = "text-align:center;"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                CSS line-height Property 
            </h2> 
              
            <div class="geek"> 
                A computer science portal for geeks.<br> 
                This div has line-height:2.5; 
            </div> 
        </body> 
    </html>

    輸出:
    lineheight

  • 長度:在此模式下,將指定固定的行高。
    line-height:length;
    

    例:

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>CSS line-height Property</title> 
            <style> 
                .geek { 
                  line-height:2em; 
                  background:green; 
                  color:white; 
                } 
            </style> 
        </head> 
        <body style = "text-align:center;"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                CSS line-height Property 
            </h2> 
              
            <div class="geek"> 
                A computer science portal for geeks.<br> 
                This div has line-height:2em; 
            </div> 
        </body> 
    </html>

    輸出:
    lineheight

  • 百分比:此模式用於以當前字體大小的百分比設置行高。
    line-height:percentage;
    

    例:

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>CSS line-height Property</title> 
            <style> 
                .geek { 
                  line-height:150%; 
                  background:green; 
                  color:white; 
                } 
            </style> 
        </head> 
        <body style = "text-align:center;"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                CSS line-height Property 
            </h2> 
              
            <div class="geek"> 
                A computer science portal for geeks.<br> 
                This div has line-height:150%; 
            </div> 
        </body> 
    </html>

    輸出:
    lineheight

  • 初始:此模式用於將此屬性設置為其默認值。
    用法:
    line-height:initial;
    

支持的瀏覽器:下麵列出了line-height屬性支持的瀏覽器:

  • 穀歌瀏覽器1.0
  • Internet Explorer 4.0
  • Firefox 1.0
  • Opera 7.0
  • 蘋果Safari 1.0


相關用法


注:本文由純淨天空篩選整理自Vishal Chaudhary 2大神的英文原創作品 CSS | line-height Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。