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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。