How to handle non configured variables in Sass - css

In the following Sass file i'm creating a simple css structure where I can set up all the fonts in the first rows.
$global-font-family: 'Open Sans', sans-serif;
/*h1*/
$h1-font-family: inherit;
$h1-font-size: 4em;
$h1-letter-spacing: inherit;
$h1-line-height: inherit;
$h1-text-transform: inherit;
/*h2*/
$h2-font-family: inherit;
$h2-font-size: 3em;
$h2-letter-spacing: inherit;
$h2-line-height: inherit;
$h2-text-transform: inherit;
h3,h4,h5,ecc.
h1, h2, h3, h4, h5, h6, p {
font-family: $global-font-family;
}
h1 {
font-family: $h1-font-family;
font-size: $h1-font-size;
letter-spacing: $h1-letter-spacing;
line-height: $h1-line-height;
text-transform: $h1-text-transform;
}
h2 {
font-family: $h2-font-family;
font-size: $h2-font-size;
letter-spacing: $h2-letter-spacing;
line-height: $h2-line-height;
text-transform: $h2-text-transform;
}
h3,h4,h5 ecc.
Firstly a font-family is applied to h1,h2,h3,p. Then, you can change every single tag, but is not necessary. For example, if you set a global font-family Open Sans to all, and h1 has to be Open Sans, you leave h1 blank.
But... how I leave h1 to Open Sans without removing this
h1 { font-family: $h1-font-family }
is "inherit" the correct way? What would be the cleanest solution?
Thanks

Generally if you are creating a template, I think it would be better to write just the global rules and let the users customize whatever they need in whichever model they prefer to do it. What I mean to say is that you should leave out all the h1, h2 specific rules and let them write the selectors if they need. They can always override your CSS by appending custom rules.
But if you want to follow your model of defining the variable names that they should be using, setting the properties only if the expected variable is defined by the user then the #if directive or the if() function along with variable-exists, global-variable-exists could be useful for you like in below snippet (its a reduced sample). I think this makes it really complex but its your choice.
$global-font-family: 'Open Sans', sans-serif;
$global-font-size: 3em;
/*h1*/
$h1-font-family: 'Roboto Sans', sans-serif;
/*h2*/
$h2-font-size: 1em;
h1, h2, h3, h4, h5, h6, p {
font-family: $global-font-family;
}
h1 {
#if (global-variable-exists(h1-font-family) or variable-exists(h1-font-family)){
font-family: $h1-font-family;
}
font-size: if((global-variable-exists(h1-font-size) or variable-exists(h1-font-size)),
$h1-font-size,
$global-font-size);
}
h2 {
#if (global-variable-exists(h2-font-family) or variable-exists(h2-font-family)){
font-family: $h2-font-family;
}
font-size: if((global-variable-exists(h2-font-size) or variable-exists(h2-font-size)),
$h2-font-size,
$global-font-size);
}
variable-exists function checks if the variable is defined in the local scope whereas the global-variable-exists checks if it is defined in global scope. If it is defined then we use the value provided by the user. If else we do nothing or use the global font settings.
In the snippet, I've also demonstrated both the #if directive and the if function. They have subtle differences. The #if directive prints the property (font-family here) only if the variable exists while in the case of if function, as you can see, the property is printed irrespective of whether the variable exists or not. If the variable is present, it uses th user defined value else it uses the global setting. The choice on which one to use is upto you.
Below is the compiled CSS:
h1, h2, h3, h4, h5, h6, p {
font-family: "Open Sans", sans-serif;
}
h1 {
font-family: "Roboto Sans", sans-serif;
font-size: 3em;
}
h2 {
font-size: 1em;
}
Here since the $h2-font-family is not defined separately, it doesn't print the font-family here and so the global one defined in h1, h2, h3, h4, h5, h6, p would get used.
Note: inherit doesn't mean inherit the value from the previous matching selector. It means inherit the value from the parent. So, for example if you need body to have Arial font, global setting is for h1, h2, h3 to have Verdana then if you set $h1-font-family to inherit, it would apply Arial font to h2 instead of applying Verdana (if user didn't define anything). This might not be what you're after.
body {
font-family: Arial;
}
h1, h2, h3 {
font-family: Verdana;
}
h2 {
font-family: inherit;
}
<h1>Some Heading 1</h1>
<h2>Some Heading 2</h2>
<h3>Some Heading 3</h3>

The reason you make variables in the first place is to make changes quicker and be able to be consistent. If you want to apply your h1 font to all headings or to primary headings name the variable after that. Or simply inherit. ex:
$font-heading-primary: 'Open Sans', sans-serif;
$font-heading-secondary: 'Halfway Sans', sans-serif;
$font-family-paragraph: 'Closed Sans', sans-serif;
$font-size-lg: 3rem;
$font-size-md: 2rem;
$font-size-sm: 1rem;
h1 {
font-family: $font-heading-primary;
font-size: $font-size-lg;
}
h2 {
font-family: $font-heading-primary;
font-size: $font-size-md;
}
h3 {
font-family: $font-heading-secondary;
font-size: $font-size-sm;
}
p {
font-family: $font-paragraph;
}

There a lot of ways to about architecting your SCSS, but it's easy to get in a situation of overkill like your example. I would try to harness the power of the cascade if I were you; set the font family on the body or a base set of elements so you're not redeclaring it hundreds of times.
body,
button,
input,
select,
textarea,
address,
p,
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: $global-font-family;
}

Related

Bootstrap 4 external style sheet Font Attributes won't work

I've been struggling to create my 1st website in years. Using bootstrap in Dreamweaver.
There is a steep learning curve, made worse because it seems like bootstrap just wants to do what it wants to do.
I want to set up my styles, H1, H2, etc...
So, I added the H1 style like so:
h1 {
font-family: Impact, Haettenschweiler, "Franklin Gothic Bold", "Arial Black",
"sans-serif";
font-size: 8em;
text-align: left
}
And at first it looks like it works, but as soon as I move to do something else, or check in on my test page, it does back to whatever size it defaulted to.
Preceding it is this:
html {
font-family: sans-serif;
line-height: 1.15;
-webkit-text-size-adjust: 100%;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
article, aside, figcaption, figure, footer, header, hgroup, main, nav, section {
display: block;
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica
Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe
UI Symbol", "Noto Color Emoji";
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #0A0A0A;
text-align: left;
background-color: #fff;
}
I have piece by piece removed each of this attributes to see what my be over-riding my H1 code, but it makes no difference.
What am I missing?
Experimenting with the rest of the standard styles, h2 through h6 have the same problem. I think P is retaining size but it won't get lighter.
Mind you, the font family assigned to H1 stays. Whatever 4em, 8em, 30em, 100%, 300%, 600%.... That change will not present itself on a refresh of my test page and it will disappear from the Dreamweaver split view.
Thanks in advance.
Update: for #Isherwood
It only displays correctly within the Dreamweaver split screen interface as I change the CSS external sheet.
All this is new to me. It has been more than 8 years since I've built a site from scratch and I've only been member here for a few days.
I am quite the novice. I'm not even sure what you mean by "override styles".
I simply set h1, h2, h3, etc... in the HTML body portion around the text I want effected. I declare the font family, size, etc... in the external css as shown above.
But give me a while to see if I can figure out how to make a snippet as you requested.
Meanwhile, I do have this loaded online to see if that made any difference, in a folder on my husband's site.
https://www.walterbeckham.com/testingzone/one.html

Why is my font not rendering correct / fuzzy - is there a way to fix this?

I am using various versions of Halvetic on this website. For some reason they all display fine, except for one which is fuzzy. I have noticed that one some browsers it is better or worse - however, on my iPhone it is really crisp and perfect?
Are there any suggestions on how I can get this font to display correctly in modern browsers on a desktop (chrome/firefox etc)?
This is what I see:
URL: http://52.64.135.79/wordpress/company-overview/
Relevant CSS rules I can see are as follows. Is there something I can do to fix this?
h4, .h4 {
line-height: 21px;
}
h4, .h4 {
font-size: 16px;
}
h4, .h4 {
font-weight: 700;
}
h4, .h4 {
font-family: "Helvetica Neue LT W01_55 Roman", sans-serif !important;
margin-bottom: 5px;
padding: 5px 0 0;
letter-spacing: 0.00em;
}
body {
-webkit-font-smoothing: antialiased;
}
Ultimately the issue here was because the font itself has a separate character set for each different font weight.
The CSS was applying a font-weight of 700 to a font which was meant to be rendered at 400. The fix was to download the heavier weight version of the font, rather than allow the browser to create an artificial "bold" version.

Sass control directives to add attribute to elements with existing attribute

I'm referring to the documentation here:
sass docs
and trying to see if its possible to use an if statement to apply a letter spacing attribute to each class that uses a certain font family.
I've tried
h1 {
#if font-family == 'Open Sans Condensed' {letter-spacing: 0.1em;}
}
h1 {
font-family: 'Open Sans Condensed', sans-serif;
}
with the hope of outputting:
h1 {
font-family: 'Open Sans Condensed';
letter-spacing: 0.1em;
}
which doesn't work. i'm pretty sure that I'm approaching this problem from the wrong angle. Can anybody verify if this kind of usage is possible?
Two ways you could approach this:
1) Include the letter-spacing as part of the font-face definition.
#font-face {
font-family: 'Open Sans';
src: [urls for various formats go here];
letter-spacing: 0.1em;
}
If you're loading the font in from an external stylesheet, like with google fonts or similar, you should still be able to declare a second font-face block that just includes the font-family and letter-spacing rules.
2) Use a sass mixin. You can make it very simple or more flexible, depending on whether you want to account for multiple fonts.
Basic one-font setup:
#mixin font-styles() {
font-family: 'Open Sans Condensed', sans-serif;
letter-spacing: 0.1em;
}
h1 {
#include font-styles;
}
Or parameterized for multiple font styles:
#mixin font-styles($font: 'headings') {
#if $font == 'headings' {
font-family: 'Open Sans Condensed', sans-serif;
letter-spacing: 0.1em;
}
#elseif $font == 'text' {
font-family: 'Open Sans', sans-serif;
[related font styles go here]
}
[add more font style sets as needed]
}
h1 { #include font-styles('headings'); }
p { #include font-styles('text'); }

Font changing in the basic Casper theme causes icon disappearing

I'm really new to CSS and Ghost Blog Platform. I try to follow this instruction on how to customize the font of basic Casper theme.
However, the following line in the instruction seem not to work. With this line, icons disappear, and the font of texts behind icons also doesn't change.
[class^="icon-"]:before, [class*=" icon-"]:before
By the way, I try to change my font to Work Sans. Here is my full code.
<link href='https://fonts.googleapis.com/css?family=Work+Sans:400,700,500,600' rel='stylesheet' type='text/css'>
<style>
body,
h1, h2, h3, h4, h5, h6,
.main-nav a,
.subscribe-button,
.page-title,
.page-description,
.post-meta,
.read-next-story .post:before,
.pagination,
.site-footer,
[class^="icon-"]:before, [class*=" icon-"]:before {
font-family:"Work Sans", sans-serif; /* Replace with your own font */
}
</style>
Any suggestion is very appreciated!
I had the same problem and I found out what the problem was.
In ghost academy, it is said that we can change fonts with the code below.
body,
h1, h2, h3, h4, h5, h6,
.main-nav a,
.subscribe-button,
.page-title,
.post-meta,
.read-next-story .post:before,
.pagination,
.site-footer,
[class^="icon-"]:before, [class*=" icon-"]:before {
font-family:"Georgia", sans-serif; /* Replace with your own font */
}
But with that code, icons disappear as you said. To change the font without making icons disappeared, we can use codes like this.
<style>
body,
h1, h2, h3, h4, h5, h6,
.main-nav a,
.subscribe-button,
.page-title,
.post-meta,
.read-next-story .post:before,
.pagination,
.site-footer {
font-family:"Georgia", sans-serif; /* Replace with your own font */
}
</style>
<style>
[class^="icon-"]:before, [class*=" icon-"]:before {
font-family: "casper-icons", "Open Sans", sans-serif;
}
</style>
Hope you solve the problem.

Input's 'placeholder' font-family can't be set different than 'value' font-family (all other styles work though) [duplicate]

I am trying to override the font of all headers, input, select, text area and input placeholders on my site with the following code:
h1, h2, h3, h4, h5, h6,
button, input, select, textarea,
:-ms-input-placeholder,
::-moz-placeholder,
:-moz-placeholder,
::-webkit-input-placeholder {
font-family:some font name;
}
The problem is, for some reason it isn't working on Chrome. If I delete the :-moz and :-ms references, then chrome works fine, which leads me to believe that Chrome doesn't like pseudo-classes for some reason? I'm stumped, because I can't see why pseudo-classes that have nothing to do with Chrome would make it not work!
You need to make separate declarations for it to work in all browsers, otherwise a conflict will cause undesired results like this.
h1, h2, h3, h4, h5, h6,
button, input, select, textarea {
font-family: somefont;
}
::-webkit-input-placeholder {
font-family: somefont;
}
:-moz-placeholder {
font-family: somefont;
}
::-moz-placeholder {
font-family: somefont;
}
:-ms-input-placeholder {
font-family: somefont;
}

Resources