【问题标题】:Is there anyway to apply letter and word spacing to a "font" using css?无论如何使用css将字母和单词间距应用于“字体”?
【发布时间】:2022-01-03 04:49:52
【问题描述】:

我有一个自定义字体,代码如下 -

@font-face {
font-family: 'classylight';
url : 'some path';
font-weight:normal;
    }

我想为网站上各处的这种字体专门设置一些值,例如字母间距、字间距和其他样式。我想减少不必要的过程,并寻找attribute=style属性。

我试过了-

body [style="font-family:classylight"] {
letter-spacing:50px;
word-spacing:-20px; 
}

它不工作。任何人都可以帮忙吗?我只想为此使用 css。如果css没有可能,请参考javascript、jquery。

PS - 我不是在寻找直接将样式添加到正文部分的答案,例如

p {
font-family: classylight;
letter-spacing:50px;
word-spacing:-20px; 
    } 

【问题讨论】:

  • 你真的应该阅读the docs
  • 但我从来没有使用过这样的代码。
  • '.className { @font-face { font-family: MyHelvetica; src: local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf);字体粗细:粗体; } }'
  • 没有人使用过这样的代码,body [font-family='classylight'] 从 body 中选择 font-family 属性设置为 classylight 的元素,很可能选择器没有找到任何元素。

标签: javascript html jquery css


【解决方案1】:

独特的文本颜色和大小,您不能使用attribute=style 属性更改字母间距和字间距。您应该在创建时通过更改笔宽来更改它们,或者您应该在 css 的主体中更改它们。

【讨论】:

    【解决方案2】:

    对所有字母间距、单词间距等使用 remem。 而对于字体粗细,是因为初始声明覆盖了你自己的字体粗细。

    【讨论】:

    • 如何改变单位使css工作?很抱歉,您回答这个问题只是为了回答。
    • 因为 rem 是基于具有默认值的根字体大小。 Em 是基于父字体大小的。这两个是更好的选择,尤其是对于字母间距等,因为您有自定义字体声明。
    • 这些单位不适用于字体大小!
    【解决方案3】:

    我猜你可能会觉得这很有用.. 这是最常用的 css 文本属性

    <h1 style="
    
    text-align: center; 
    font-size: 25px;  
    color:#FF0000;      
    font-family: Arial Black,Arial,Helvetica,Verdana,Trebuchet MS,Comic Sans MS,sans-serif;    
    font-style: normal;   
    font-weight: 1000;
    background-color: #D3D3D3; 
    line-height: 3;
    
    ">TEST 123</h1>

    还有关于您的问题“font-weight:bold”不起作用,也许它被其他值覆盖,例如

    ,这使得文本已经很大......

    【讨论】:

    • 感谢妈妈的努力。但我正在尝试专门将样式应用于字体
    【解决方案4】:

    所以你不能真正在字体声明中使用letter-spacing。但是,您可以做的是创建一个 class,它具有您想要的字母间距,然后将该类添加到 HTML 元素中。像这样:

    @font-face {
       font-family: 'Roboto';
       url : 'https://fonts.googleapis.com/css2?family=Roboto:wght@400&display=swap';
       font-weight: normal;
    }
    
    @font-face {
       font-family: 'Open Sans';
       url : 'https://fonts.googleapis.com/css2?family=Open+Sans&display=swap';
       font-weight: normal;
    }
            
    .roboto-norm{
       font-family: 'Roboto';
       font-weight: normal;
    }
            
    .roboto-norm-ltr-spc{
       font-family: 'Roboto';
       font-weight: normal;
       letter-spacing: 0.25em !important;
    }
            
    .opensans-norm{
       font-family: 'Open Sans';
       font-weight: normal;
    }
            
    .opensans-norm-ltr-spc{
       font-family: 'Open Sans';
       font-weight: normal;
       letter-spacing: 0.25em !important;
    }
    <label class="roboto-norm">Normal roboto letter spacing</label>
    <br><br>
    <label class="roboto-norm-ltr-spc">Custom roboto letter spacing</label>
    
    <br><br>
    <br><br>
    
    <label class="opensans-norm">Normal open sans letter spacing</label>
    <br><br>
    <label class="opensans-norm-ltr-spc">Custom open sans letter spacing</label>

    至于font-weight: normal 部分,您使用@font-face 规则所做的是声明字体。它基本上是一种变量,然后在设置 HTML 元素的样式时会引用它。字体的重量是其中的一部分。

    下面的 sn-p 应该更清楚。我所做的是我导入了两种样式的Roboto 字体,第一种是常规粗细,又名400,另一种是粗体,又名700

    @font-face {
       font-family: 'Roboto';
       url : 'https://fonts.googleapis.com/css2?family=Roboto:wght@400&display=swap';
       font-weight: normal;
    }
    @font-face {
       font-family: 'Roboto';
       url : 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap';
       font-weight: bold;
    }
        
    *{
       font-family: 'Roboto';
    }
        
    #normal{
       font-weight: normal;
    }
    #bold{
       font-weight: bold;
    }
    <label id="normal">normal font</label>
    <br><br>
    <label id="bold">bold font</label>

    【讨论】:

    • 是的。得到第二部分的答案。但是,这不是我要寻找的第一部分。我希望正文中具有特定字体系列的所有内容都具有指定的字母和单词间距。
    • 答案的第一部分为您提供了解决方案。通过添加特定的字体系列和不添加什么来创建一个满足您要求的类,然后使用它。你想要的无法完成,因为没有选择器可以用来选择某个字体系列的所有元素。
    • 没有兄弟。我说的是可能的。我犯了一个错误,想知道我在哪里犯了那个错误。 CSS Attribute 属性专门用于此目的。我使用不同类型的字体,每种字体都需要不同的样式。所以,我特别想采用这种方法。
    • 在您编辑答案后,我明白您想说什么。我在发布这个问题之前就这样做了。它完美地工作。我想问的是 - 在 css 中是否有任何方法,通过使用属性,直接声明我正在尝试的字体样式,以便减少冗余。
    • 你不能,因为'font-family'没有选择器。您可以通过 JS/jQuery,但这需要循环遍历所有元素,这比使用类的 CSS 替代方案要昂贵得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 2013-04-21
    • 2013-12-03
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多