.search-bar svg { width: 25px; height: 25px; color: red; } .search-button svg { width: 25px; height: 25px; color: red; }
<div class="search-bar"> <form> <a class="search-button" type="submit"> <svg class="svg-research" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"> <rect width="256" height="256" fill="none"/> <circle cx="116" cy="116" r="84" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="8"/> <line x1="175.4" y1="175.4" x2="224" y2="224" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="8"/> </svg> </a> </form> </div>
有人能告訴我如何使用CSS控制svg的顏色嗎?我嘗試使用不同的類和ID來(lái)做這個(gè),但似乎沒(méi)有任何一個(gè)對(duì)顏色產(chǎn)生影響。我能夠改變大小和位置,但無(wú)法改變顏色。我希望能夠使用一個(gè)單獨(dú)的ID或類來(lái)做到這一點(diǎn),而不是分別改變圓圈和線條的顏色。
<div class="search-bar"> <form> <a class="search-button" type="submit"> <svg class="svg-research" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"> <rect width="256" height="256" fill="none"/> <circle cx="116" cy="116" r="84" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="8"/> <line x1="175.4" y1="175.4" x2="224" y2="224" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="8"/> </svg> </a> </form> </div>
給共享屬性(例如線條和圓的描邊)賦予currentColor
的值,然后您可以通過(guò)svg的color
屬性(或任何祖先或繼承的屬性)來(lái)控制它:
.search-button svg { width: 25px; height: 25px; color: red; } .search-button svg line,.search-button svg circle{ stroke: currentColor; }
<div class="search-bar"> <form> <a class="search-button" type="submit"> <svg class="svg-research" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"> <rect width="256" height="256" fill="none"/> <circle cx="116" cy="116" r="84" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="8"/> <line x1="175.4" y1="175.4" x2="224" y2="224" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="8"/> </svg> </a> </form> </div>
(這是Font Awesome和其他庫(kù)在幕后所做的。如果您給height
和width
賦予em
單位的值,您也可以通過(guò)后續(xù)的font-size
來(lái)控制它,這也是繼承的)