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


HTML Style borderWidth用法及代码示例


HTML DOM中的borderWidth属性用于设置或返回border元素的宽度。

用法:

  • 用于设置宽度的边框。
    object.style.borderWidth = value
  • 它返回border width属性。
    object.style.borderWidth

返回值:它以给定的样式返回选定的边框元素。


下面列出了border-width属性的六个不同值:

  • 厚:它将边框宽度设置为粗。

    用法:

    document.getElementById("id_name").style.borderWidth = "thick";

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            HTML | DOM Style borderWidth Property 
        </title> 
        <style> 
            div { 
                border:1px solid green; 
                padding:10px; 
                text-align:center; 
            } 
        </style> 
    </head> 
      
    <body> 
      
        <div id="d1"> 
            <p>Welocme to GeeksforGeeks.!</p> 
            <p> 
              This tutorial is on  
              HTML | DOM Style borderWidth Property. 
            </p> 
        </div> 
        <br> 
        <input type="button" onclick="myFunction()" 
                             value="Click Me.!" /> 
      
        <script> 
            function myFunction() { 
                document.getElementById("d1").style.borderWidth  
                                                     = "thick"; 
            } 
        </script> 
      
    </body> 
      
    </html>

    输出:
    之前单击按钮:

    单击按钮后:

    长度:用于将border-width的每一侧设置为给定长度。长度可以设置为px,em等。

    用法:

    document.getElementById("id_name").style.borderWidth =
    top_px right_px bottom_px left_px;

    例:本示例设置边框的每一侧的宽度。

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            HTML | DOM Style borderWidth Property 
        </title> 
        <style> 
            div { 
                border-style:solid; 
                border:1px solid green; 
                padding:10px; 
                text-align:center; 
            } 
        </style> 
    </head> 
      
    <body> 
      
        <div id="d1"> 
            <p>Welocme to GeeksforGeeks.!</p> 
            <p> 
              This tutorial is on  
              HTML | DOM Style borderWidth Property.  
            </p> 
        </div> 
        <br> 
        <input type="button" onclick="mainFunction()"
                             value="Click Me.!" /> 
        <script> 
            function mainFunction() { 
                document.getElementById("d1").style.borderWidth 
                                          = "1px 7px 15px 25px"; 
            } 
        </script> 
      
    </body> 
      
    </html>

    输出:
    之前单击按钮:

    单击按钮后:

    稀薄:用于将边框宽度设置为稀薄。

    用法:


    document.getElementById("id_name").style.borderWidth = "thin";

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            HTML | DOM Style borderWidth Property 
        </title> 
        <style> 
            div { 
                border:8px solid green; 
                padding:10px; 
                text-align:center; 
            } 
        </style> 
    </head> 
      
    <body> 
      
        <div id="d1"> 
            <p>Welocme to GeeksforGeeks.!</p> 
            <p> 
              This tutorial is on  
              HTML | DOM Style borderWidth Property. 
            </p> 
        </div> 
        <br> 
        <input type="button" onclick="myFunction()" 
                             value="Click Me.!" /> 
      
        <script> 
            function myFunction() { 
                document.getElementById("d1").style.borderWidth  
                                                      = "thin"; 
            } 
        </script> 
      
    </body> 
      
    </html>

    输出:
    之前单击按钮:

    单击按钮后:

    medium:用于将边框更改为medium。它是border-width的默认值。

    用法:

    document.getElementById("id_name").style.borderWidth = "medium";

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            HTML | DOM Style borderWidth Property 
        </title> 
        <style> 
            div { 
                border:30px solid green; 
                padding:10px; 
                text-align:center; 
            } 
        </style> 
    </head> 
      
    <body> 
      
        <div id="d1"> 
            <p>Welocme to GeeksforGeeks.!</p> 
            <p> 
              This tutorial is on  
              HTML | DOM Style borderWidth Property. 
            </p> 
        </div> 
        <br> 
        <input type="button" onclick="myFunction()" 
                             value="Click Me.!" /> 
      
        <script> 
            function myFunction() { 
                document.getElementById("d1").style.borderWidth 
                                                    = "medium"; 
            } 
        </script> 
      
    </body> 
      
    </html>

    输出:
    之前单击按钮:

    单击按钮后:

    继承:它指定从其父元素继承的border-width属性。

    用法:

    document.getElementById("id_name").style.borderWidth = "inherit";

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            HTML | DOM Style borderWidth Property 
        </title> 
        <style> 
            span { 
                color:blue; 
                border:3px solid black; 
                padding:3px; 
            } 
              
            .d1 span { 
                color:inherit; 
            } 
              
            div { 
                padding:5px; 
                display:block; 
                margin:6px; 
            } 
              
            .c2 { 
                width:50%; 
                height:40%; 
                padding:20px; 
                border:2px solid green; 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="c2"> 
            <div> 
                This is first  
                <span>span element</span>. 
            </div> 
      
            <div class="d1" style="color:green"> 
                This is second  
              <span id="gfg">span element</span>. 
            </div> 
      
            <div style="color:red"

    输出:
    之前单击按钮:

    单击按钮后:

    支持的浏览器:下面列出了DOM borderWidth属性支持的浏览器:

    • 谷歌浏览器
    • IE浏览器
    • Firefox
    • Safari
    • Opera


相关用法


注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 HTML | DOM Style borderWidth Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。