Adding Styling to svelte-routing Link tag in svelte js
Asked Answered
O

3

10

I wanted to add Styling to the <Link> tag in svelte routing but I couldn't. I have tried to add a class in which there is some styling but it didn't work.

<Link to='/' class='link'></Link>

the class contains:

.link {
   text-decoration: none;
}

Does anyone have a solution for this?

Olenta answered 26/7, 2020 at 9:0 Comment(0)
S
10

The <Link></Link> component represents a html <a></a> tag.

You can use the global svelte-css option:

<style>
    .link > :global(a) {
        text-decoration: none;
    }
    
    :global(a) {
        ...
    }
</style>

See also global-REPL: https://svelte.dev/repl/be432b377c7549e8b60ed10452065f52?version=3.8.1

Another way is to modify the Link.svelte component in the svelte-routing package itself. This can be done inside your node_modules folder or you can fork the repository (https://github.com/EmilTholin/svelte-routing) and do the changes.

Subtile answered 26/7, 2020 at 10:41 Comment(3)
Thanks! Although it's a bit late to say it.Olenta
When trying your suggestion, I can't seem to get it work. Setting any styling inside the :global(a){} doesn't get applied and vs code says that it is unused. What am I missing here?Silicon
@Silicon if you add the :global(a) to the parent's style it should work. For example: .link-wrapper > :global(a) {text-decoration: none;} <div class="link-wrapper"> <Link to='/' class='link'></Link> </div>Prosthesis
T
5

You can use this option:

import { link } from 'svelte-routing';
...
<a href='/' class='link' use:link></a>

this gives you the same behavior and lets you add styles

source: https://github.com/EmilTholin/svelte-routing#link-1

Tearful answered 24/1, 2021 at 16:23 Comment(3)
Unfortunately it does not give the same behavior. It behaves like an <a> tag by refreshing the page when changing the URL.Olenta
be sure that you hare using link and not LinkTearful
this solution is correct. Thanks manSubsidize
C
0

There are some open pull requests about the issue you mentioned:

Maybe try following up on those issues

Cyrie answered 27/7, 2020 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.