Custom CSS properties in #font-face - css

The body should display a handwriting font but will only do so if the comment line is uncommented.
It seems I cannot use custom properties in #font-face? Tested on FF and Chrome.
What's going on here?
:root {
--backgroundColor: cornflowerblue;
--textColor: white;
--fontName: 'Indie Flower';
}
#font-face {
font-family: var(--fontName);
/* font-family: 'Indie Flower'; */
src: url(https://fonts.gstatic.com/s/indieflower/v11/m8JVjfNVeKWVnh3QMuKkFcZVaUuH.woff2) format('woff2');
}
body {
background-color: var(--backgroundColor);
color: var(--textColor);
font-family: var(--fontName);
}
<p>Custom CSS properties don't work inside #font-face rule?</p>

Your original suspicion that CSS properties don't work inside #font-face is accurate. The accepted answer is incorrect and only appears to work because of a mistake (see below).
I checked in Firefox, Chrome, and Safari, and none of them accept a #font-face in which a descriptor includes a var() function. Firefox developer edition was particularly helpful, since it let me see that the #font-face rule isn't being parsed at all with font-family: var(--fontName);
I also tried using a custom property for the src descriptor, which also failed.
As far as I can tell, the CSS #font-face specification says nothing about whether custom properties should work or not. No browser has chosen to make them work. This makes considerable sense, as a single font family's source file and properties are not likely to be variable.
If you have to dynamically construct #font-face rules client-side, the best method is using the FontFace API in a script. If absolutely necessary you could even read from your CSS properties with getComputedStyle(document.body).getPropertyValue("--fontName").
The accepted answer only seems to work because it redefines the font family name in #font-face, setting it to a string with the literal value of 'var(--fontName)'. It also sets the body font-family: 'var(--fontName)'. The two strings match, so the font loads.
CSS functions like var() are NEVER evaluated inside of a string. Everything between the opening and closing quotes is evaluated literally, as this diagram from the CSS syntax spec shows. So #disinfor's code isn't referencing the --fontName: "Indie Flower"; set on :root at all.
As a demonstration, see what happens when we unquote the font-family on body, so it actually evaluates the var(). The "Indie Flower" font doesn't exist in the document, so it doesn't load, but a font named "var(--fontName)" does exist, and will load:
:root {
--backgroundColor: cornflowerblue;
--textColor: white;
--fontName: "Indie Flower";
}
#font-face {
font-family: 'var(--fontName)';
src: url(https://fonts.gstatic.com/s/indieflower/v11/m8JVjfNVeKWVnh3QMuKkFcZVaUuH.woff2) format('woff2');
}
body {
background-color: var(--backgroundColor);
color: var(--textColor);
font-family: var(--fontName);
}
.use-the-string {
font-family: 'var(--fontName)';
}
<p>This tries to use "Indie Flower", but it doesn't exist.</p>
<p class="use-the-string">This uses the font named "var(--fontName)"</p>

The problem is how the font-name is actually output for the browser to read. Remove the quote marks for the root var, and add them around the call to the variable.
:root {
--backgroundColor: cornflowerblue;
--textColor: white;
--fontName: Indie Flower;
}
#font-face {
font-family: 'var(--fontName)';
src: url(https://fonts.gstatic.com/s/indieflower/v11/m8JVjfNVeKWVnh3QMuKkFcZVaUuH.woff2) format('woff2');
}
body {
background-color: var(--backgroundColor);
color: var(--textColor);
font-family: 'var(--fontName)';
}
<p>Custom CSS properties don't work inside #font-face rule?</p>
Edit
Here's the version with a fallback.
:root {
--backgroundColor: cornflowerblue;
--textColor: white;
--fontName: "Indie Flower";
--fallBack: sans-serif;
--fullFont: 'var(--fontName)', var(--fallBack);
}
#font-face {
font-family: 'var(--fontName)';
src: url(https://fonts.gstatic.com/s/indieflower/v11/m8JVjfNVeKWVnh3QMuKkFcZVaUuH.woff2) format('woff2');
}
body {
background-color: var(--backgroundColor);
color: var(--textColor);
font-family: 'var(--fontName)', var(--fallBack);
}
p {
font-family: var(--fullFont);
}
<p>Custom CSS properties don't work inside #font-face rule?</p>

Related

how to use CSS variable which has multiple property

I am currently working on a react project which uses DSM invision for the UI design, basicly, DSM provides a css file '_style-params' which contains style variables.
--color-primary: #00a2ff;
--color-secondary: #6c757d;
--color-success: #28a745;
--color-danger: #dc3545;
for this kind is easy to understand and use, i can just import the css file into my main css file and write:
background-color:var(--color-primary);
However, when it comes to font, I have some question:
below is from _style-params.css:
/* button fonts/default/5-warning font style */
--font-button-fonts-default-5-warning-font-size: 16px;
--font-button-fonts-default-5-warning-line-height: 14px;
--font-button-fonts-default-5-warning-text-align: center;
--font-button-fonts-default-5-warning-color: #f0ad4e;
--font-button-fonts-default-5-warning-letter-spacing: 0.2px;
--font-button-fonts-default-5-warning-font-style: normal;
--font-button-fonts-default-5-warning-font-weight: 400;
--font-button-fonts-default-5-warning-font-family: Roboto;
--font-button-fonts-default-5-warning: {
font-size: var(--font-button-fonts-default-5-warning-font-size);
line-height: var(--font-button-fonts-default-5-warning-line-height);
text-align: var(--font-button-fonts-default-5-warning-text-align);
color: var(--font-button-fonts-default-5-warning-color);
letter-spacing: var(--font-button-fonts-default-5-warning-letter-spacing);
font-style: var(--font-button-fonts-default-5-warning-font-style);
font-weight: var(--font-button-fonts-default-5-warning-font-weight);
font-family: var(--font-button-fonts-default-5-warning-font-family);
};
Am I able to just use following varible (which is a object)
var(--font-button-fonts-default-5-warning)
I dunno which property to use this variable, i tried following:
font:var(--font-button-fonts-default-5-warning)
which is not working (obviously).
So, am I able to use this object CSS varible?
or I have to use the individual variables?
There is no object concept in CSS variables. You need to use them individually but you can also combine them in the same variable that you can use later relying on the shothand notation of properties.
Example:
:root {
/* button fonts/default/5-warning font style */
--font-button-fonts-default-5-warning-font-size: 30px;
--font-button-fonts-default-5-warning-line-height: 14px;
--font-button-fonts-default-5-warning-text-align: center;
--font-button-fonts-default-5-warning-color: #f0ad4e;
--font-button-fonts-default-5-warning-letter-spacing: 0.2px;
--font-button-fonts-default-5-warning-font-style: normal;
--font-button-fonts-default-5-warning-font-weight: 400;
--font-button-fonts-default-5-warning-font-family: "Roboto";
--font-button-fonts-default-5-warning:
var(--font-button-fonts-default-5-warning-font-style)
var(--font-button-fonts-default-5-warning-font-weight)
var(--font-button-fonts-default-5-warning-font-size)/
var(--font-button-fonts-default-5-warning-line-height)
var(--font-button-fonts-default-5-warning-font-family);
}
.warning {
font:var(--font-button-fonts-default-5-warning);
color: var(--font-button-fonts-default-5-warning-color);
letter-spacing: var(--font-button-fonts-default-5-warning-letter-spacing);
text-align:var(--font-button-fonts-default-5-warning-text-align);
}
<p>text here</p>
<p class="warning">text here</p>
We can use almost all the values inside font but not color, letter-spacing and text-align that you need use them individually.

Applying custom font in css is not working?

I am trying to use a custom font for the headings on my site but I cannot get it to work. It is just displaying as default Times New Roman or something no matter what I try. Does anyone have any advice about what I can do to get this font to show?
<style>
#font-face {
font-family:"volcano";
src: url("/wp-content/themes/wp-bootstrap-4/assets/fonts/Volcano-King.ttf") format ("truetype"),
url("/wp-content/themes/wp-bootstrap-4/assets/fonts/Volcano-King.woff") format ("woff");
}
h1 {
font-family: "volcano";
}
</style>
Looks like there's a missing :
Try this
<style>
#font-face {
font-family:"volcano";
src: url("/wp-content/themes/wp-bootstrap-4/assets/fonts/Volcano-King.ttf" format ("truetype"),
url("/wp-content/themes/wp-bootstrap-4/assets/fonts/Volcano-King.woff" format ("woff");
}
h1 {
font-family: "volcano";
}
</style>
h1 {
font-family: "volcano";
}
add a colon between property-name and property-value

How to set a global font - CSS

I want to set a global font with CSS in one of my projects.
What I have tried is the following:
#font-face {
font-family: HoftypeLight;
src: Valid URL;
}
* {
font-family: HoftypeLight;
}
but it seems not to work.
I know that I properly get the font because I have tried to add to one of my texts this class:
.test {
font-family: HoftypeLight;
}
and it works.
Hello I think use like this
body {
font-family: Algerian;
}
font same for in all project within the body tag.

How to use Font Awesome classes on arbitrary tags?

I want, very strongly, to avoid littering my markup with non-sematic <i> tags and am attempting to use the icon-* CSS classes on other elements, such has headers, the primary use case.
I have the following markup:
<div class="box-header">
<h2><i class="icon-list-ul"></i><span class="break"></span>Unordered List</h2>
</div>
<div class="box-header">
<h2 class="icon-list-ul">Unordered List</h2>
</div>
The first is the original markup, the second is my desired markup. Rendered, they look like:
I'll worry about the divider later. Note, however, the difference in 'boldness' of the text. I notice that Font Awesome by default applies its font (and some other properties) to anything with a CSS class matching icon-*, and my first attempt to correct the problem revolved around changing this css:
[class^="icon-"],
[class*=" icon-"] {
font-family: FontAwesome;
font-weight: normal;
font-style: normal;
text-decoration: inherit;
-webkit-font-smoothing: antialiased;
*margin-right: .3em;
}
Into the following to ensure the the font selection is only applied to the :before icon content:
[class^="icon-"]:before,
[class*=" icon-"]:before {
font-family: FontAwesome;
font-weight: normal;
font-style: normal;
text-decoration: inherit;
-webkit-font-smoothing: antialiased;
*margin-right: .3em;
}
Now I have a different problem, as can be shown in the following screenshot:
While the font weight is back to the normal (thin) value, the alignment of the text has shifted down substantially, both for the standard markup and my desired markup cases. What's going on here, and how can I use the icon-* classes on arbitrary elements without those elements own formatting getting out-of-wack?
Thanks for any assistance you can offer!
As this has lingered for a long time, I'll post a self-answer pointing out the methodology I utilize these days, which involves a SASS/SCSS mix-in.
Basically, follow the methodology of FontAwesome itself, with the exception that instead of populating a bajillion generic CSS classes for use with non-semantic <i> (italic) tags, populate the SCSS variables for each character, and dynamically apply via #include to the specific elements you wish to have icons applied to.
From the gist example, to automatically apply social media icons to links onto those platforms unless otherwise disabled, you can:
a:not(.no-icon) {
&[href^="https://twitter"],
&[href^="https://www.twitter"] { #include icon($icon-twitter-square, false, true) { margin: 0; }; }
&[href^="https://facebook.com"],
&[href^="https://www.facebook.com"] { #include icon($icon-facebook-square, false, true) { margin: 0; }; }
&[href^="https://linkedin.com"],
&[href^="https://www.linkedin.com"] { #include icon($icon-linkedin-square, false, true) { margin: 0; }; }
&[href^="https://youtube.com"],
&[href^="https://www.youtube.com"] { #include icon($icon-youtube-square, false, true) { margin: 0; }; }
}
Have login / log out links?
&[href$="/account/authenticate"] { #include icon($icon-sign-in, false, true) { margin: 0; }; }
Want icons defined in the HTML markup? Okay!
[data-icon] {
padding-left: 3.5em;
position: relative;
vertical-align: middle;
#include icon(attr(data-icon)) {
margin-left: -3.5em;
width: 3.5em;
text-align: center;
}
}
The difference in boldness comes down to alternate font-smoothing being applied. Forcing -webkit-font-smoothing: antialiased and -moz-osx-font-smoothing: grayscale help to mitigate.
your content inside of <h2 class="icon-list-ul"> will be affected by the awesomefont setting in [class*=" icon-"] ....
If you want to use different font styles for h2-tags and before-elements,
just keep [class*=" icon-"]:before and delete [class*=" icon-"]

Is it standard compliant to write font-size: 62.5%/1.2em in CSS?

I noticed that some stylesheets have something like this:
body { font-size: 62.5%/1.2em; }
I got a warning "unexpected token /" when I wrote this in NetBeans. And if I changed the EM value, say,
body { font-size: 62.5%/1em; }
the computed font-size remained 16px.
My question is, Is it standard compliant to write something like that? And how to computed the actual font-size?
In CSS2, the font-size property does not allow a value of the form x/y.
What you're using is the font short hand property, which allows x/y as a short-hand of font-size: x; line-height: y;. So either use
body { font: 62.5%/1.2em sans-serif; }
/* ^^^^^^^^^^ the font-family is needed. */
or
body {
font-size: 62.5%;
line-height: 1.2em;
}

Resources