Hello, I want to replace the site title with a logo image, but I’d like to give users the option to choose between the logo image and the text title. What’s the best way to implement this feature?
This is the default behavior:
<header>
<h1 class="site-title><a href="/">My New Hugo Website</a></h1>
</header>
When the option to display a logo image is enabled, I want the site title to be hidden from view but still accessible for text browsers for SEO purposes, like so:
<header>
<h1 class="site-title visuallyhidden"><a href="/">My New Hugo Website</a></h1>
<a href="/">
<img class="logo-img" src="/images/logo.png" alt="">
</a>
</header>
Then use this CSS:
.visuallyhidden {
position: absolute;
padding: 0;
border: 0;
width: 1px;
height: 1px;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
}
I’m just unsure how to configure this in the config.toml
file to allow users to enable or disable the feature. Should I include something like this under params
?
logo = "site-logo.jpg"
Is this a good approach? Also how can I check this in the template? should I use a with
conditional? I mean do something if the value of logo defined, but something else if logo is empty or not defined at all.
logo = "site-logo"
I tried this in my header.html partial and it seems to work:
{{- $logo := .Site.Params.logo }}
<header>
{{- if $logo }}
<h1 class="site-title visuallyhidden"><a href="{{ .Site.BaseURL }}">{{ .Site.Title }}</a></h1>
<a href="{{ .Site.BaseURL }}">
<img src="{{ .Site.BaseURL }}/images/{{ $logo }}" alt="{{ site.Title }}">
</a>
{{- else }}
<h1 class="site-title">
<a href="{{ .Site.BaseURL }}">{{ .Site.Title }}</a>
</h1>
{{- end }}
</header>
Would it be better to create a new partial for the “logo” something like partials/logo.html
and import it as a cached partial in the partials/header.html
? Right now I’m putting everything inside partials/header.html
. Maybe there will be a navigation bar as well in the header.
I was thinking about putting this in the header.html
partial :
{{ partials.IncludeCached "logo.html" . }}
where the logo should be, regardless if it’s an image or text.
I would be grateful for any suggestion. Thank you.