用 CSS 統一控制 SVG 樣式

這篇文章將帶你了解 SVG 圖片與 CSS 的強大結合,學習如何透過 CSS 來操控 SVG 圖形的樣式,賦予你的網頁設計更多彈性和創意。

STEP 1: 修改 SVG

開啟要使用的 svg 檔案,將內部的 fill (填充色彩), stroke (邊線色彩), stroke-width (邊線寬度) 屬性刪除,並在最外層的 svg 標籤加入 id=”main”

<svg version="1.1" id="main"
	xmlns="http://www.w3.org/2000/svg"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	viewBox="0 0 512 512" xml:space="preserve" fill="#ffffff" stroke="#000000" stroke-width="20px">
	<g>
		<path class="st0" d="M461.814,197.514c-2.999-11.335-14.624-18.093-25.958-15.094c-1.866,0.553-13.477,3.649-26.042,14.341 c-6.234,5.349-12.633,12.751-17.361,22.454c-4.748,9.69-7.685,21.577-7.657,35.033c0.013,16.345,4.133,34.895,13.442,56.257 c6.282,14.403,9.144,29.697,9.144,44.846c0.062,25.627-8.438,50.756-21.121,68.283c-6.296,8.777-13.546,15.606-20.816,20.022 c-2.986,1.81-5.943,3.131-8.888,4.181l0.989-5.854c-0.055-17.03-4.05-34.84-13.021-50.528 c-28.356-49.643-66.223-134.741-66.223-134.741l-1.527-4.879c29.47-7.796,58.579-23.408,73.148-54.985 c38.931-84.344-41.08-142.73-41.08-142.73s-25.958-56.222-38.924-54.06c-12.978,2.164-41.094,38.931-41.094,38.931h-23.788h-23.788 c0,0-28.108-36.767-41.08-38.931c-12.979-2.163-38.924,54.06-38.924,54.06s-80.018,58.386-41.087,142.73 c13.822,29.953,40.741,45.572,68.634,53.748l-2.951,9.662c0,0-31.908,81.552-60.279,131.195C37.198,441.092,58.478,512,97.477,512 c29.47,0,79.14,0,101.692,0c7.292,0,11.763,0,11.763,0c22.544,0,72.222,0,101.691,0c12.654,0,23.38-7.547,31.204-19.324 c15.826-0.013,30.81-4.872,43.707-12.758c19.455-11.915,34.708-30.32,45.434-51.896c10.685-21.618,16.856-46.636,16.878-72.672 c0-20.484-3.885-41.619-12.682-61.813c-7.561-17.34-9.918-30.216-9.904-39.29c0.028-7.526,1.5-12.544,3.359-16.414 c1.417-2.889,3.124-5.17,4.983-7.091c2.771-2.868,5.964-4.879,8.349-6.054c1.182-0.595,2.135-0.968,2.674-1.162l0.449-0.152 l-0.007-0.028C458.179,220.189,464.779,208.724,461.814,197.514z">
		</path>
	</g>
</svg>

(SVG 檔案預覽圖)

修改後

<svg version="1.1" id="main"
	xmlns="http://www.w3.org/2000/svg"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	viewBox="0 0 512 512" xml:space="preserve">
	<g>
		<path class="st0" d="M461.814,197.514c-2.999-11.335-14.624-18.093-25.958-15.094c-1.866,0.553-13.477,3.649-26.042,14.341 c-6.234,5.349-12.633,12.751-17.361,22.454c-4.748,9.69-7.685,21.577-7.657,35.033c0.013,16.345,4.133,34.895,13.442,56.257 c6.282,14.403,9.144,29.697,9.144,44.846c0.062,25.627-8.438,50.756-21.121,68.283c-6.296,8.777-13.546,15.606-20.816,20.022 c-2.986,1.81-5.943,3.131-8.888,4.181l0.989-5.854c-0.055-17.03-4.05-34.84-13.021-50.528 c-28.356-49.643-66.223-134.741-66.223-134.741l-1.527-4.879c29.47-7.796,58.579-23.408,73.148-54.985 c38.931-84.344-41.08-142.73-41.08-142.73s-25.958-56.222-38.924-54.06c-12.978,2.164-41.094,38.931-41.094,38.931h-23.788h-23.788 c0,0-28.108-36.767-41.08-38.931c-12.979-2.163-38.924,54.06-38.924,54.06s-80.018,58.386-41.087,142.73 c13.822,29.953,40.741,45.572,68.634,53.748l-2.951,9.662c0,0-31.908,81.552-60.279,131.195C37.198,441.092,58.478,512,97.477,512 c29.47,0,79.14,0,101.692,0c7.292,0,11.763,0,11.763,0c22.544,0,72.222,0,101.691,0c12.654,0,23.38-7.547,31.204-19.324 c15.826-0.013,30.81-4.872,43.707-12.758c19.455-11.915,34.708-30.32,45.434-51.896c10.685-21.618,16.856-46.636,16.878-72.672 c0-20.484-3.885-41.619-12.682-61.813c-7.561-17.34-9.918-30.216-9.904-39.29c0.028-7.526,1.5-12.544,3.359-16.414 c1.417-2.889,3.124-5.17,4.983-7.091c2.771-2.868,5.964-4.879,8.349-6.054c1.182-0.595,2.135-0.968,2.674-1.162l0.449-0.152 l-0.007-0.028C458.179,220.189,464.779,208.724,461.814,197.514z">
		</path>
	</g>
</svg>

我們會得到沒有填充、沒有線條的 svg 圖

STEP 2: 使用 use 標籤在 HTML 中引入

在 HTML 中,我們使用 use 標籤,引入 SVG,在 href 屬性中,我們還要透過 #main 去指定到剛剛所設定的 id=”main”

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <svg style="width: 100px">
      <!-- 透過 use 中的 href 屬性找到 svg 檔案,並加入 #main -->
      <use href="./cat-sit.svg#main"></use>
    </svg>
  </body>
</html>

我們就可以在畫面上看到這個 svg 圖片

STEP 3: 加入 SVG 樣式

再來我們就可以自行設定 SVG 的樣式,使用的屬性跟在 SVG 移除的屬性相同

  • fill (填充色彩)
  • stroke (邊線色彩)
  • stroke-width (邊線寬度)
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <style>
      .icon {
        fill: gray;
        transition: 0.3s;
      }

      .icon:hover {
        fill: skyblue;
        stroke: black;
        stroke-width: 10px;
      }
    </style>
  </head>

  <body>
    <svg class="icon" style="width: 100px">
      <use href="./cat-sit.svg#main"></use>
    </svg>
  </body>
</html>

結果如下:

如此一來,如果我們有多個 SVG 的小圖標,我們就可以透過 CSS 來控制這些圖標的樣式,達到統一色彩,也可以在不同的情境下,將改變圖標的樣式!