網站導覽

v2.1.0
內文

圖示

透過包含行內圖示的元件設定圖示大小。

本頁內容

將 icon 加在 <svg>、<img> 或圖示元件本身。元件一律不依元素類型為子元素套用樣式。各元件會設定 --icon-size,圖示會依該設定調整大小,因此相同的標記放在按鈕、徽章和分頁標籤中,會呈現不同的大小。

同一圖示,三種情境

<button type="button" class="btn btn-sm">  <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">    <path d="M3 8L13 8" /><path d="M8 3L8 13" />  </svg> Small</button><button type="button" class="btn">  <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">    <path d="M3 8L13 8" /><path d="M8 3L8 13" />  </svg> Medium</button><button type="button" class="btn btn-lg">  <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">    <path d="M4 10L16 10" /><path d="M10 4L10 16" />  </svg> Large</button>
import PlusIcon16 from '@master/icons-react/native/16/soft/plus'import PlusIcon20 from '@master/icons-react/native/20/soft/plus'<>  <button type="button" className="btn btn-sm"><PlusIcon16 className="icon" /> Small</button>  <button type="button" className="btn"><PlusIcon16 className="icon" /> Medium</button>  <button type="button" className="btn btn-lg"><PlusIcon20 className="icon" /> Large</button></>
<script>  import PlusIcon16 from '@master/icons-svelte/native/16/soft/plus'  import PlusIcon20 from '@master/icons-svelte/native/20/soft/plus'</script><button type="button" class="btn btn-sm"><PlusIcon16 class="icon" /> Small</button><button type="button" class="btn"><PlusIcon16 class="icon" /> Medium</button><button type="button" class="btn btn-lg"><PlusIcon20 class="icon" /> Large</button>
<script setup>import PlusIcon16 from '@master/icons-vue/native/16/soft/plus'import PlusIcon20 from '@master/icons-vue/native/20/soft/plus'</script><template>  <button type="button" class="btn btn-sm"><PlusIcon16 class="icon" /> Small</button>  <button type="button" class="btn"><PlusIcon16 class="icon" /> Medium</button>  <button type="button" class="btn btn-lg"><PlusIcon20 class="icon" /> Large</button></template>

#文字中的圖示

圖示位於元件外時,大小為 1em,位置略低於基線,以便與小寫字母對齊。

文字中的圖示

<p class="m(0) text-sm fg-text-body">  Press <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" role="img" aria-label="Download">    <path d="M8.5 9L8.4994 1.9755A0.5 0.5 0 0 0 7.5054 1.9266L7.5 2L7.5014 9.0379A0.5007 0.5007 0 0 0 7.5892 9.285A0.7044 0.7044 0 0 0 8.4108 9.285A0.4996 0.4996 0 0 0 8.5 9Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M5 6L7.8586 8.8586A0.2 0.2 0 0 0 8.1414 8.8586L11 6" /><path d="M2.5 10.5L2.5 12.5A1 1 0 0 0 3.5 13.5L12.5 13.5A1 1 0 0 0 13.5 12.5L13.5 10.5" />  </svg> to download the report.</p>
import DownloadIcon from '@master/icons-react/native/16/soft/download'<p className="m(0) text-sm fg-text-body">  Press <DownloadIcon className="icon" aria-label="Download" /> to download the report.</p>
<script>  import DownloadIcon from '@master/icons-svelte/native/16/soft/download'</script><p class="m(0) text-sm fg-text-body">  Press <DownloadIcon class="icon" aria-label="Download" /> to download the report.</p>
<script setup>import DownloadIcon from '@master/icons-vue/native/16/soft/download'</script><template>  <p class="m(0) text-sm fg-text-body">    Press <DownloadIcon class="icon" aria-label="Download" /> to download the report.  </p></template>

#Master Icons

Master UI 自己的元件與範例使用 Master Icons。每個圖示都為 16、20、24px 各畫一次,並以 currentColor 繪製。icon 接受任何 SVG、圖片或圖示元件,所以其他圖示集的用法相同:Master Icons 是預設,不是必要條件。

npm install @master/icons-react --save-exact

請安裝對應框架的套件:@master/icons-react、-vue、-svelte、-preact 或 -astro;只需要 SVG 檔案與純 JavaScript 時安裝 @master/icons。

依元件給圖示的尺寸選擇圖稿,只匯入那一張圖稿,並加上 icon:

元件設定的尺寸圖稿圖稿線寬
17px 以下161
18 到 22px201.25
超過 22px241.5
import SearchIcon from '@master/icons-react/native/16/soft/search'<button type="button" className="btn btn-square" aria-label="Search">  <SearchIcon className="icon" /></button>

在 HTML 中,從圖示集複製 SVG 並加上 icon。名稱與其他圖示集不同,寫之前請先查詢:

node node_modules/@master/icons/skills/master-icons/scripts/search.mjs "arrow left"

#描邊

圖示會保留圖稿自己的線寬。Master Icons 每個尺寸的線寬都是網格的十六分之一,所以圖示的線寬在 16px 時是 1px,並隨元件設定的尺寸等比增加。要改變 Master Icons 的粗細,請在固定入口(例如 @master/icons-react/16/soft)選擇 strokeWidth:鏤空與間隙會依該線寬重新繪製。

描邊

<svg class="icon fg-text-strong" style="--icon-size: 1.5rem" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">  <circle cx="12" cy="12" r="9" /><path d="M12 6.5L12 11.8892A0.2 0.2 0 0 0 12.094 12.0588L16 14.5" /></svg><svg class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 1.25" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">  <circle cx="12" cy="12" r="9" /><path d="M12 6.5L12 11.8892A0.2 0.2 0 0 0 12.094 12.0588L16 14.5" /></svg><svg class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 2" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">  <circle cx="12" cy="12" r="9" /><path d="M12 6.5L12 11.8892A0.2 0.2 0 0 0 12.094 12.0588L16 14.5" /></svg>
import ClockIcon from '@master/icons-react/native/24/soft/clock'<>  <ClockIcon className="icon fg-text-strong" style={{ '--icon-size': '1.5rem' } as React.CSSProperties} />  <ClockIcon className="icon fg-text-strong" style={{ '--icon-size': '1.5rem', '--icon-stroke': '1.25' } as React.CSSProperties} />  <ClockIcon className="icon fg-text-strong" style={{ '--icon-size': '1.5rem', '--icon-stroke': '2' } as React.CSSProperties} /></>
<script>  import ClockIcon from '@master/icons-svelte/native/24/soft/clock'</script><ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem" /><ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 1.25" /><ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 2" />
<script setup>import ClockIcon from '@master/icons-vue/native/24/soft/clock'</script><template>  <ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem" />  <ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 1.25" />  <ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 2" /></template>

--icon-stroke 會取代圖稿的線寬,以圖稿 viewBox 的單位計算;填色圖示不受影響。可用它把一組單純的線條圖示調整到介面的粗細。設定在容器上時,裡面的圖示都會跟著改變。

#無障礙

  • 使用 aria-hidden="true" 隱藏裝飾性圖示。
  • 只有圖示的控制項應在本身設定無障礙名稱,例如將 aria-label 加在方形按鈕上。
  • 在句子中代替一個詞的圖示,例如上方的下載箭頭,就是那個詞:請為它加上 role="img",並以 aria-label 說明它代表什麼,而不是它長什麼樣子。

#自訂屬性

屬性預設值說明
--icon-size圖示的寬度與高度,由容納圖示的元件設定,沒有這類元件時為 1em
--icon-stroke取代圖稿的線寬,以圖稿 viewBox 的單位計算。未設定時圖示保留自己的線寬。填色圖示不受影響
--icon-color圖示的顏色。外層元件可設定此值來淡化圖示或為圖示上色。沒有外層元件時,圖示沿用文字顏色

#類別

類別說明
icon行內圖示的大小由 --icon-size 設定,在元件外為 1em,位置略低於基線

#原始程式碼

@master/ui/components/icon.css
/* * Icon: sizes an inline icon by the --icon-size its component provides. * Put .icon on the <svg>, <img> or icon component itself. Components never reach into * their children by element type; they set --icon-size and --icon-color instead. * * The drawing keeps its own stroke. An optically sized set such as Master Icons draws each size * at its own width (1 on a 16 grid, 1.25 on 20, 1.5 on 24: a sixteenth of the grid at every size), * so a single width written here would be wrong for two of the three. --icon-stroke is the * exception a set asks for by name: when it is set, it replaces the drawing's width. */@layer components.ui {  .icon {    flex: none;    width: var(--icon-size, 1em);    height: var(--icon-size, 1em);    /* Icons carry internal whitespace; a control pulls them toward its edge by --icon-offset. */    margin-inline: var(--icon-offset, 0);    /*     * Without --icon-stroke the declaration rolls back to the stroke-width the SVG carries, and to     * the initial 1 where it carries none. With it, the value replaces the drawing's; fill icons are unaffected.     */    stroke-width: var(--icon-stroke, revert-layer);    /* A component may quiet its icons next to a strong label; by default an icon takes the text color. */    color: var(--icon-color, inherit);    vertical-align: -.125em;  }}