Skip to content

Commit

Permalink
feat: add custom css properties to change fonts (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Oct 6, 2024
1 parent 7ecb9a2 commit 9c27c66
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 14 deletions.
29 changes: 29 additions & 0 deletions exampleSite/content/posts/features/theming/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,33 @@ If you want to add more Favicon formats you have to [overwrite](https://gohugo.i
<meta name="theme-color" content="#efefef">
```

## Fonts

To use a custom font, it needs to be specified first. While there are many ways to do this, we recommend to use `@font-face` as it supports local as well as remote fonts. If you want to serve the fonts from your own server, you have to place them in the `static/fonts` folder of your project.

The font registration is done in the `custom.css` file. There are also a few custom CSS properties available to simplify the usage of custom fonts.

**Example:**

<!-- prettier-ignore -->
```css
@font-face {
font-family: "DancingScript";
src:
url("fonts/DancingScript.woff2") format("woff2"),
url("fonts/DancingScript.woff") format("woff");
font-weight: normal;
font-style: normal;
font-display: swap;
}

:root {
--code-max-height: 60rem;

--header-font-family: "DancingScript";
--body-font-family: "DancingScript";
--code-font-family: "DancingScript";
}
```

Happy customizing!
16 changes: 16 additions & 0 deletions exampleSite/static/custom.css.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

:root {
--code-max-height: 60rem;


--header-font-family: "DancingScript";
--body-font-family: "DancingScript";
--code-font-family: "DancingScript";
}

/* Light mode theming */
Expand Down Expand Up @@ -156,3 +161,14 @@
--footer-link-color-visited: #ffcc5c;
}
}


@font-face {
font-family: "DancingScript";
src:
url("fonts/DancingScript.woff2") format("woff2"),
url("fonts/DancingScript.woff") format("woff");
font-weight: normal;
font-style: normal;
font-display: swap;
}
10 changes: 8 additions & 2 deletions src/sass/_base.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
:root,
:root[color-theme="light"] {
:root {
--code-max-height: none;

--header-font-family: #{meta.inspect($header-font-family)};
--body-font-family: #{meta.inspect($body-font-family)};
--code-font-family: #{meta.inspect($code-font-family)};
}

:root,
:root[color-theme="light"] {
@include color_theme_light;
@include code_theme_light;
}
Expand Down
3 changes: 3 additions & 0 deletions src/sass/_defaults.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ $link-color-visited: rgba(119, 73, 191, 1) !default;
$link-color-footer: rgba(246, 107, 14, 1) !default;

$body-background: white !default;
$body-font-family: "Liberation Sans", sans-serif !default;
$body-font-color: $gray-800 !default;
$body-font-weight: normal !default;
$body-min-width: 20rem !default;

$code-font-family: "Liberation Mono", monospace !default;
$code-font-color: rgba(95, 95, 95, 1) !default;
$code-font-color-dark: rgba(185, 185, 185, 1) !default;

Expand All @@ -67,6 +69,7 @@ $link-color-visited-dark: rgba(186, 142, 240) !default;
$code-background: lighten($gray-200, 4) !default;
$code-background-dark: darken($body-background-dark, 3) !default;

$header-font-family: "Metropolis", sans-serif !default;
$header-height: 3.5rem !default;
$menu-width: 16rem !default;

Expand Down
28 changes: 18 additions & 10 deletions src/sass/_fonts.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@font-face {
font-family: "Liberation Sans";
src: url("fonts/LiberationSans-Bold.woff2") format("woff2"),
src:
url("fonts/LiberationSans-Bold.woff2") format("woff2"),
url("fonts/LiberationSans-Bold.woff") format("woff");
font-weight: bold;
font-style: normal;
Expand All @@ -9,7 +10,8 @@

@font-face {
font-family: "Liberation Sans";
src: url("fonts/LiberationSans-BoldItalic.woff2") format("woff2"),
src:
url("fonts/LiberationSans-BoldItalic.woff2") format("woff2"),
url("fonts/LiberationSans-BoldItalic.woff") format("woff");
font-weight: bold;
font-style: italic;
Expand All @@ -18,7 +20,8 @@

@font-face {
font-family: "Liberation Sans";
src: url("fonts/LiberationSans-Italic.woff2") format("woff2"),
src:
url("fonts/LiberationSans-Italic.woff2") format("woff2"),
url("fonts/LiberationSans-Italic.woff") format("woff");
font-weight: normal;
font-style: italic;
Expand All @@ -27,7 +30,8 @@

@font-face {
font-family: "Liberation Sans";
src: url("fonts/LiberationSans.woff2") format("woff2"),
src:
url("fonts/LiberationSans.woff2") format("woff2"),
url("fonts/LiberationSans.woff") format("woff");
font-weight: normal;
font-style: normal;
Expand All @@ -36,7 +40,8 @@

@font-face {
font-family: "Liberation Mono";
src: url("fonts/LiberationMono.woff2") format("woff2"),
src:
url("fonts/LiberationMono.woff2") format("woff2"),
url("fonts/LiberationMono.woff") format("woff");
font-weight: normal;
font-style: normal;
Expand All @@ -45,30 +50,33 @@

@font-face {
font-family: "Metropolis";
src: url("fonts/Metropolis.woff2") format("woff2"), url("fonts/Metropolis.woff") format("woff");
src:
url("fonts/Metropolis.woff2") format("woff2"),
url("fonts/Metropolis.woff") format("woff");
font-weight: normal;
font-style: normal;
font-display: swap;
}

@font-face {
font-family: "GeekblogIcons";
src: url("fonts/GeekblogIcons.woff2") format("woff2"),
src:
url("fonts/GeekblogIcons.woff2") format("woff2"),
url("fonts/GeekblogIcons.woff") format("woff");
font-weight: normal;
font-style: normal;
font-display: swap;
}

body {
font-family: "Liberation Sans", sans-serif;
font-family: var(--body-font-family);
}

code,
.gblog-error__title {
font-family: "Liberation Mono", monospace;
font-family: var(--code-font-family);
}

.gblog-header {
font-family: "Metropolis", sans-serif;
font-family: var(--header-font-family);
}
4 changes: 2 additions & 2 deletions src/sass/_shortcodes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@

// {{< mermaid >}}
.gblog-mermaid {
font-family: "Liberation Sans", sans-serif;
font-family: var(--body-font-family);

// Fix height of mermaid SVG elements (see https://github.com/mermaid-js/mermaid/issues/2481)
> svg {
Expand All @@ -258,7 +258,7 @@
&__default {
padding: 0;
margin: 0;
font-family: "Liberation Mono", monospace;
font-family: var(--code-font-family);
}

&__meta {
Expand Down
1 change: 1 addition & 0 deletions src/sass/main.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@use "sass:map";
@use "sass:meta";

@import "_defaults";
@import "_color_mode";
Expand Down

0 comments on commit 9c27c66

Please sign in to comment.