Material Symbols 中有切换样式的方法吗?

huangapple go评论63阅读模式
英文:

Is there a way to switch style in Material Symbols?

问题

当我使用Material Icons时,我可以通过替换类名来切换样式(在material-iconsmaterial-icons-outlined之间)。现在我正在尝试使用Material Symbols。假设我使用星形图标并希望使图标填充。

public/index.html

<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,1,0" />
<span class="material-symbols-outlined">
star
</span>

对于Material Symbols,填充和轮廓之间的类名将相同,我想知道是否可以在程序中切换样式。

英文:

When I use Material Icons, I can switch the style by replacing the class name. (between material-icons and material-icons-outlined)

Now I am trying to use Material Symbols. Let's say I use star icon and want to make the icon filled.

public/index.html

<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,1,0" />
<span class="material-symbols-outlined">
star
</span>

For Material Symbols, the class name will be the same between filled and outlined and I am wondering if I can switch the style programaitically.

答案1

得分: 1

你可以使用CSS的font-variation-settings属性来为每个元素设置符号样式。查看这里的CodePen示例:使用Google Fonts的可变字体

我使用了在Google:Material Symbols指南中找到的示例代码,并对其进行了修改,以创建一个小演示,展示如何使用CSS的自定义属性来设置符号样式。

  • 定义一个CSS的自定义属性--variation
  • 使用JavaScript修改自定义属性以保存所需的font-variation-settings
  • 在CSS中使用当前的自定义属性值来设置所需的符号样式:
    1. unset:将其设置为.material-symbols-outlined的Google默认设置
    2. 细样式font-variation-settings: 'FILL' 0, 'wght' 100, 'GRAD' 0, 'opsz' 48
    3. 填充样式font-variation-settings: 'FILL' 1, 'wght' 700, 'GRAD' 0, 'opsz' 48

片段 给符号字体文件加载时间...

<!-- 开始片段: js hide: false console: false babel: false -->

<!-- 语言: lang-html -->
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" rel="stylesheet" />

<style>
:root      { --variation: unset } /* 将其设置为默认的Google设置 */
.icons     { font-variation-settings: var(--variation) } /* 使用当前设置 */
.icons > * { font-size: 3.5rem }
</style>

<fieldset>
    <legend>&nbsp;图标样式&nbsp;</legend>
    <label for="outline">
        <input id="outline" class="radio" type="radio" name="group" checked
               oninput="document.documentElement.style.setProperty('--variation', 'unset');">
        outlined
    </label>
    <label for="thin">
        <input id="thin" class="radio" type="radio" name="group"
               oninput="document.documentElement.style.setProperty('--variation', 'FILL 0, wght 100, GRAD 0, opsz 48');">
        thin
    </label>
    <label for="filled">
        <input id="filled" class="radio" type="radio" name="group"
               oninput="document.documentElement.style.setProperty('--variation', 'FILL 1, wght 700, GRAD 0, opsz 48');">
        filled
    </label>

    <br><br>

    <div class="icons">
        <span class="material-symbols-outlined">search</span>
        <span class="material-symbols-outlined">home</span>
        <span class="material-symbols-outlined">settings</span>
        <span class="material-symbols-outlined">favorite</span>
    </div>
</fieldset>

<!-- 结束片段 -->
英文:

You can use the CSS font-variation-settings property to set the symbol style per element. Check the codepen example here: Variable font with Google Fonts.

I used example code found in Google: Material Symbols guide and modified it to create a little demo showcasing how to set the symbol style using a CSS custom property.

  • define a CSS custom property --variation
  • modify the custom property with Javascript to hold required font-variation-settings
  • use the current custom property value in CSS to set required symbol style:
    1. unset: sets it to the Google default setting for .material-symbols-outlined
    2. thin style: font-variation-settings: &#39;FILL&#39; 0, &#39;wght&#39; 100, &#39;GRAD&#39; 0, &#39;opsz&#39; 48
    3. filled style: font-variation-settings: &#39;FILL&#39; 1, &#39;wght&#39; 700, &#39;GRAD&#39; 0, &#39;opsz&#39; 48

snippet give the symbol font file time to load...

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;link href=&quot;https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;style&gt;
:root      { --variation: unset } /* Sets it to default Google settings */
.icons     { font-variation-settings: var(--variation) } /* Use current settings */
.icons &gt; * { font-size: 3.5rem }
&lt;/style&gt;

&lt;fieldset&gt;
    &lt;legend&gt;&amp;nbsp;Icon Style&amp;nbsp;&lt;/legend&gt;
    &lt;label for=&quot;outline&quot;&gt;
        &lt;input id=&quot;outline&quot; class=&quot;radio&quot; type=&quot;radio&quot; name=&quot;group&quot; checked
               oninput=&quot;document.documentElement.style.setProperty(&#39;--variation&#39;, `unset`);&quot;&gt;
        outlined
    &lt;/label&gt;
    &lt;label for=&quot;thin&quot;&gt;
        &lt;input id=&quot;thin&quot; class=&quot;radio&quot; type=&quot;radio&quot; name=&quot;group&quot;
               oninput=&quot;document.documentElement.style.setProperty(&#39;--variation&#39;, `&#39;FILL&#39; 0, &#39;wght&#39; 100, &#39;GRAD&#39; 0, &#39;opsz&#39; 48`);&quot;&gt;
        thin
    &lt;/label&gt;
    &lt;label for=&quot;filled&quot;&gt;
        &lt;input id=&quot;filled&quot; class=&quot;radio&quot; type=&quot;radio&quot; name=&quot;group&quot;
               oninput=&quot;document.documentElement.style.setProperty(&#39;--variation&#39;, `&#39;FILL&#39; 1, &#39;wght&#39; 700, &#39;GRAD&#39; 0, &#39;opsz&#39; 48`);&quot;&gt;
        filled
    &lt;/label&gt;

    &lt;br&gt;&lt;br&gt;

    &lt;div class=&quot;icons&quot;&gt;
        &lt;span class=&quot;material-symbols-outlined&quot;&gt;search&lt;/span&gt;
        &lt;span class=&quot;material-symbols-outlined&quot;&gt;home&lt;/span&gt;
        &lt;span class=&quot;material-symbols-outlined&quot;&gt;settings&lt;/span&gt;
        &lt;span class=&quot;material-symbols-outlined&quot;&gt;favorite&lt;/span&gt;
    &lt;/div&gt;
&lt;/fieldset&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 07:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定