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


CSS stroke-width用法及代碼示例


stroke-width屬性用於以SVG形狀設置邊框的寬度。此屬性隻能應用於具有形狀或文本內容元素的元素。

用法:

stroke-width:<length> | <percentage>

屬性值:


  • length:用於以測量單位設置筆劃寬度。它可以采用整數或小數百分比的值。
    值不需要具有“ px”或“ em”之類的單位標識符。不帶單位的值將基於SVG視圖框的坐標係。

    範例1:本示例設置無單位的筆劃寬度。

    <!DOCTYPE html> 
    <html> 
          
    <head> 
        <title>CSS | stroke-width</title> 
          
        <style> 
            .stroke1 { 
                stroke-width:10; 
                stroke:green; 
            } 
            .stroke2 { 
                stroke-width:30; 
                stroke:red; 
            } 
            .stroke3 { 
                stroke-width:50; 
                stroke:orange; 
            } 
        </style> 
    </head> 
      
    <body> 
        <h1 style="color:green"> 
            GeeksforGeeks 
        </h1> 
          
        <b>CSS | stroke-width</b> 
          
        <div class="container"> 
            <svg height="300px" width="400px"
                xmlns="http://www.w3.org/2000/svg"> 
                <line class="stroke1" x1="0" x2="250"
                    y1="20" y2="20" /> 
                <line class="stroke2" x1="0" x2="250"
                    y1="90" y2="90" /> 
                <line class="stroke3" x1="0" x2="250"
                y1="200" y2="200" /> 
            </svg> 
        </div> 
    </body> 
      
    </html>

    輸出:
    width-no-units

    範例2:本示例以像素為單位設置筆劃寬度。

    <!DOCTYPE html> 
    <html> 
          
    <head> 
        <title>CSS | stroke-width</title> 
          
        <style> 
            .stroke1 { 
                stroke-width:5px; 
                stroke:green; 
            } 
            .stroke2 { 
                stroke-width:10px; 
                stroke:red; 
            } 
            .stroke3 { 
                stroke-width:20px; 
                stroke:orange; 
            } 
        </style> 
    </head> 
      
    <body> 
        <h1 style="color:green"> 
            GeeksforGeeks 
        </h1> 
          
        <b>CSS | stroke-width</b> 
          
        <div class="container"> 
            <svg height="300px" width="400px"
                xmlns="http://www.w3.org/2000/svg"> 
                <circle class="stroke1" cx="60"
                    cy="50" r="25"/> 
                <circle class="stroke2" cx="60"
                    cy="150" r="25"/> 
                <circle class="stroke3" cx="60"
                    cy="250" r="25"/> 
            </svg> 
        </div> 
    </body> 
      
    </html>

    輸出:
    width-pixels

  • percentage:用於以百分比設置筆劃寬度。

    例:

    <!DOCTYPE html> 
    <html> 
          
    <head> 
        <title>CSS | stroke-width</title> 
          
        <style> 
            .stroke1 { 
                stroke-width:1%; 
                stroke:green; 
            } 
            .stroke2 { 
                stroke-width:2%; 
                stroke:red; 
            } 
            .stroke3 { 
                stroke-width:3%; 
                stroke:orange; 
            } 
        </style> 
    </head> 
      
    <body> 
        <h1 style="color:green"> 
            GeeksforGeeks 
        </h1> 
          
        <b>CSS | stroke-width</b> 
          
        <div class="container"> 
            <svg height="300px" width="400px"
                xmlns="http://www.w3.org/2000/svg"> 
                <ellipse class="stroke1" cx="100"
                    cy="50" rx="35" ry="25" /> 
                <ellipse class="stroke2" cx="100"
                    cy="150" rx="35" ry="25" /> 
                <ellipse class="stroke3" cx="100"
                    cy="250" rx="35" ry="25" /> 
            </svg> 
        </div> 
    </body> 
      
    </html>

    輸出:
    width-percentage

支持的瀏覽器:stroke-width屬性支持的瀏覽器如下所示:

  • chrome
  • 火狐瀏覽器
  • 蘋果瀏覽器
  • Opera
  • Internet Explorer 9


相關用法


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