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


CSS min-width用法及代碼示例


CSS中的min-width屬性用於定義元素的最小寬度。寬度的值不能小於min-width的值。如果元素內指定的內容較小,則min-width會保持指定的最小寬度。

用法:

min-width:length|initial|inherit;

屬性值:


  • length:此屬性用於設置min-width的長度。長度可以設置為px,cm等形式。

    用法:

    min-width:length;

    例:

    <!DOCTYPE html>  
    <html>  
        <head>  
            <title>min-width property</title>  
              
            <!-- min-width CSS property -->
            <style>  
                p {  
                    min-width:300px; 
                    display:inline-block; 
                    color:white; 
                    background-color:green;  
                }  
            </style>  
        </head>  
          
        <body>  
            <p>  
                GeeksforGeeks:A computer science portal  
            </p>  
        </body>  
    </html>                     

    輸出:

  • 百分比(%):用於以百分比設置最小寬度。

    用法:

    min-width:%;

    例:

    <!DOCTYPE html>  
    <html>  
        <head>  
            <title>min-width property</title>  
              
            <!-- min-width CSS property -->
            <style>  
                p {  
                    min-width:35%; 
                    display:inline-block; 
                    color:white; 
                    background-color:green;  
                }  
            </style>  
        </head>  
          
        <body>  
            <p>  
                GeeksforGeeks:A computer science portal  
            </p>  
        </body>  
    </html>                     

    輸出:

  • initial:它用於將min-width屬性設置為其默認值。

    用法:

    min-width:initial;

    例:

    <!DOCTYPE html>  
    <html>  
        <head>  
            <title>min-width property</title>  
              
            <!-- min-width CSS property -->
            <style>  
                p {  
                    min-width:initial; 
                    display:inline-block; 
                    color:white; 
                    background-color:green;  
                }  
            </style>  
        </head>  
          
        <body>  
            <p>  
                GeeksforGeeks:A computer science portal  
            </p>  
        </body>  
    </html>                     

    輸出:

  • inherit:此屬性是從其父級繼承的。

    用法:

    min-width:inherit;

    例:

    <!DOCTYPE html>  
    <html>  
        <head>  
            <title>min-width property</title>  
              
            <!-- min-width CSS property -->
            <style>  
                .gfg {  
                    min-width:initial; 
                    display:inline-block; 
                    color:white; 
                    background-color:green;  
                }  
                P { 
                    min-width:inherit; 
                } 
                .geeks { 
                    min-width:initial; 
                    display:inline-block; 
                    color:white; 
                    background-color:blue;  
                } 
            </style>  
        </head>  
          
        <body>  
            <div class = "gfg"> 
                <p>  
                    GeeksforGeeks:A computer science portal  
                </p>  
                <div class = "geeks">  
                    GeeksforGeeks:A computer science portal  
                </div>  
            </div> 
        </body>  
    </html>                                      

    輸出:

支持的瀏覽器:下麵列出了min-width屬性支持的瀏覽器:

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


相關用法


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