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


HTML Style minWidth用法及代碼示例


DOM樣式minWidth屬性用於設置或返回元素的最小寬度。它適用於block-level元素或僅適用於具有絕對或固定位置的元素。

用法:

  • 返回minWidth屬性:
    object.style.minWidth
  • 設置minWidth屬性:
    object.style.minWidth = "length|%|initial|inherit"

屬性:


  • length:它以長度單位設置最小寬度。
  • %:它以父級的百分比設置最小寬度。
  • initial:它將屬性設置為默認值。
  • inherit:它從父元素繼承屬性值。

返回值:它返回一個字符串,代表所選元素的最小寬度。

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style minWidth Property 
    </title> 
    <style> 
        h1 { 
            color:green; 
        } 
          
        #gfgg { 
            width:60%; 
            background-color:lightgreen; 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
  
        <h1>GeeksforGeeks</h1> 
        <button onclick="min()"> 
          Press 
      </button> 
  
        <h4>Click on the 'Press' button to 
          set the minWidth of the div element.</h4> 
  
        <div id="gfgg"> 
            <p>DOM Style minWidth Property:It is 
              used to set the minWidth of an element.</p> 
            <p>Here, the DIV element is used to  
              showcase the min.width property.</p> 
            <p>Clicking on the press button  
              will execute the property.</p> 
        </div> 
  
        <script> 
            function min() { 
                document.getElementById( 
                  "gfgg").style.minWidth = "500px"; 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

  • 在單擊按鈕之前:
  • 單擊按鈕後:

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style minWidth Property 
    </title> 
    <style> 
        h1 { 
            color:green; 
        } 
          
        #gfgg { 
            width:60%; 
            background-color:lightgreen; 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
        <h1>GeeksforGeeks</h1> 
        <button onclick="min()">Press</button> 
        <h4>Click on the 'Press' button to see the 
embedded box displaying the minWidth of the div element.</h4> 
  
        <div style="background:lightgreen; 
                    min-width:200px;"  
             id="gfgg"> 
            
            <p>DOM Style minWidth Property:It is used  
              to set the minWidth of an element.</p> 
            
            <p>Here, the DIV element is used to  
              showcase the min.width property.</p> 
            
            <p>Clicking on the press button 
              will execute the property.</p> 
        </div> 
  
        <script> 
            function min() { 
                alert(document.getElementById( 
                  "gfgg").style.minWidth); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

  • 在單擊按鈕之前:
  • 單擊按鈕後:

瀏覽器支持:HTML DOM樣式minWidth屬性支持的瀏覽器如下:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • Opera
  • Safari


相關用法


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