I have a lengthy CSS file (over 1K lines prepared by 3rd party)
e.g.
p {font-family: Arial;}
I want to have a build process that change all "font-family" under the p tag to
p {font-family: "Helvetica Neue", Helvetica, Arial, sans-serif}
Definitely I can modify the existing CSS file, but as it is lengthly and prepared by others (might update by them in the future, maintenance nightmare..), I am looking for some external build process that allow me to rewrite the CSS and produce a final CSS.
Is it possible with Less or SASS or other tools?
Sass and LESS are built exactly for this purpose. But you will need to rewrite your static CSS first into a dynamic style sheet, and then it would be good if the "3rd party" also would work on the dynamic stylesheet and not on the static CSS.
If this is not possible, you will have to
find/replace stuff every time you get the new file from them, or
have your additional CSS that you call after theirs that overrides their properties (e.g. sets the font-family for all p tags to your value)
So, how does the dynamic styling work: you have some variables that you define in the beginning, that get then used throughout the file, and if you want to change something, you change a variable and the whole file gets updated. But you can also use way more fancy stuff, like mixins and guards, you can read more on it at the links I pasted at the bottom of my answer. You can prevent a lot of rewriting this way.
So, this would make sense if you plan to reuse/change the file in the future.
For a very basic illustrative example - how you could use a simple variable in LESS:
#pFontFam: "Helvetica Neue", Helvetica, Arial, sans-serif;
p {
font-family: #pFontFam;
}
and the output CSS looks like this:
p {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
But you can also define a mixin, that adds the whole property and its value to a class when you call it. There are a lot of resources online that describe how to install and use this. You could start here:
Sass
LESS
As for LESS, I'd use a parametric mixin (without parameter):
You can also use parametric mixins which don’t take parameters. This
is useful if you want to hide the ruleset from the CSS output, but
want to include its properties in other rulesets:
.wrap () {
text-wrap: wrap;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
word-wrap: break-word;
}
pre { .wrap }
Which would output this CSS:
pre {
text-wrap: wrap;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
word-wrap: break-word;
}
The reason is the value you're trying to repeat at different places (or modify in one centralized place) is a string of font names that will always be used with the same CSS property: font-family. You really don't need to repeat this property over and over. It isn't some numerical value that could be used both in margins and paddings and borders...
So it'd be like:
.fontBase () {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
p {
.fontBase;
}
Related
I am trying to set Google Fonts in my React project but I am not being able to override the original font families of React.
Following some tutorials I inserted the font-family I want into index.css:
body {
/* margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Lemonada'
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale; */
font-family: 'Lemonada',
cursive;
}
I commented the original fonts for testing purposes.
However the original fonts are not being overwritten:
If I inspect the file I see this:
As you can see all the commented fonts are still there, besides there are a lot of other things that doesn't exist in index.css. Apparently it is coming from some style.css file, but I have no idea where to find this.
If I uncheck this style I get the font-family I want:
And of course, I also added the api to my index.html file.
<link href="https://fonts.googleapis.com/css2?family=Lemonada:wght#300&display=swap" rel="stylesheet">
I'd like to be able to add new font-families or at least override the original ones.
What am I doing wrong?
Make sure <link href="https://fonts.googleapis.com/css2?family=Lemonada:wght#300&display=swap" rel="stylesheet"> is loaded before the of your css file.
Also try changing the line to
font-family: 'Lemonada' !important,
I kept digging and I found two possible solutions:
1- Instead of using the index.css, I put the font-family direct inside the css file corresponding to the component I want to use the font, in this case this is the file Layout.css.
2- I found out this library and it also works and seems to be very great: https://www.npmjs.com/package/react-google-font-loader
I'm trying to get something like this to work:
body {
font-family: Verdana, Arial, sans-serif;
}
p {
font-family: Helvetica, inherit;
}
Basically if "Helvetica" is not available on the client's browser, I want the font-family to be inherited from a parent. But it seems to me that I can't use "inherit" in a font priority list.
How can I achieve something like this without having to copy paste font-family from body?
You are correct. You can use it just like you did. This is something that became available with CSS2. This question is similar and has some answers worthy of a read.
I think the real problem is that Helvetica isn't a free font. So, it just isn't available for widespread use.
Option 1) If you own the Helvetica font, make an image using that font
(for the few lines that you want that specific look for).
Option 2) (as #bjupreti suggested) is to use a substitute font that is widely
available.
Font family will automatically be inherited from the parent property. So, all you have to do is:
body {
font-family: Verdana, Arial, sans-serif;
}
p {
font-family: Helvetica;
}
This will automatically inherit the font family of body if there is no Helvetica in end users computer.
Is there any simple CSS rule for changing the whole body font-face type by detecting the browser type?
Open-sans doesn't work in IE so I'm searching for a way to change it depending on the browsers.
I tried looking at this question but still didn't understand how to accomplish this task.
You could use conditional comments in HTML, to detect the Internet Explorer lower than IE 11. http://de.wikipedia.org/wiki/Conditional_Comments
<!DOCTYPE html>
<!--[IF IE]><html class="lt-ie11" lang="de"><![endif]-->
<html lang="de">
After that, you can define two different CSS rules.
body { font-family: Open-Sans; }
html.lt-ie11 body { font-family: sans-serif; }
Another solution besides adding a conditional comment in your CSS would be to specify a fallback font family in your CSS.
Instead of doing:
body {
font-family: Open-Sans;
}
You could do:
body {
font-family: Open-Sans, Helvetica, Arial, sans-serif;
}
The browser will first try using Open Sans. If it can't find the font, it'll try using Helvetica, then Arial, then finally use the default sans-serif font if all else fails.
Hoping that it makes my pages look the same in modern browsers, I included some reset.css with the following lines
body {
*font-size: small;
font-family: arial,helvetica,sans-serif;
font: 16px/18px sans-serif;
margin: 0 auto;
}
However, the font in my Firefox 20 (for Ubuntu Canonical - 1.0) was still by a few per cent bigger than in my Chromium 25.0.1364.160 Ubuntu 10.04. Funnily, the following rule helped:
* {
font-family: arial,helvetica,sans-serif;
}
It looks like Firefox overrides the font for span, too, but I can't see it in the Web Developer Tools. It simply chooses a different font, although it shouldn't.
I've created a plunk showing it (just drop the star rule to see the change). My questions are
what's the explanation?
what's the proper way of resetting styles? Do we really need star rules just in case?
The explanation is that font: 16px/18px sans-serif sets the font family to sans-serif, which is a browser-dependent default sans serif font. It overrides the preceding font-family setting. Apparently, in your Firefox, sans-serif is mapped to a font that looks smaller than Arial, for the same font size. So the font size is the same, the letters are just smaller.
The simplest fix is to revert the order of the two declarations:
body {
font: 16px/18px sans-serif;
font-family: arial,helvetica,sans-serif;
margin: 0 auto;
}
(The *font-size hack is pointless here, since you have a rule later that sets the font size.)
Alternatively, you can combine the rules:
body {
font: 16px/18px arial,helvetica,sans-serif;
margin: 0 auto;
}
The question “what's the proper way of resetting styles?” is primarily opinion-based and also independent of the technical problem presented. Setting font properties on body isn’t really “resetting styles”, just normal styling. Technically, setting e.g. font-family on all elements has a considerable impact, and you should do that only if you really understand the impact (e.g., on form fields, on elements like code, etc.).
How can I create a custom font family that can be used inside of <span></span> tags, and only within those tags and not site wide?
My end goal is to be able to use this within a specific area of the site, and not anywhere else.
Give the span a class and assign font-family css properties to it, IE:
<span class="specialFont">Hello</span>
then in your css:
.specialFont {
font-family: verdana;
}
As per your comment, to use custom fonts you can use this, HOWEVER keep in mind this isn't supported in all browsers:
#font-face {
font-family: CustomFont;
src: URL('/fonts/myfont.otf');
}
.specialFont {
font-family: CustomFont;
}