当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。