37

How would I do something like this:

<style>
Nested {
    color: blue;
}
</style>

<Nested />

i.e. How do I apply a style to a component from its parent?

Jacques Amsel
  • 1,061
  • 2
  • 14
  • 30

7 Answers7

25

You need to pass props to the parent component with export let, then tie those props to class or style in the child component.

You can either put a style tag on the element in the child you want to style dynamically and use a variable you export for the parent to determine the value of a style directly, then assign the color on the tag like this:

<!-- in parent component -->

<script>
import Nested from './Nested.svelte';
</script>

<Nested color="green"/>
<!-- in Nested.svelte -->

<script>
export let color;
</script>

<p style="color: {color}">
    Yes this will work
</p>

Upside here is flexibility if you only have one or two styles to adjust, downside is that you won't be able to adjust multiple CSS properties from a single prop.

or

You can still use the :global selector but just add a specific ref to the element being styled in the child like so:

<!-- in parent component -->

<script>
import Nested from './Nested.svelte';
</script>

<Nested ref="green"/>

<style>
:global([ref=green]) {
    background: green;
    color: white;
    padding: 5px;
    border-radius: .5rem;
}
</style>
<!-- in Nested.svelte -->

<script>
export let ref;
</script>

<p {ref}>
    Yes this will work also
</p>

This ensures global only affects the exact ref element inside the child it's intended for and not any other classes or native elements. You can see it in action at this REPL link

JHeth
  • 7,067
  • 2
  • 23
  • 34
  • 3
    The `ref` above is just an attribute, so that global CSS will leak out and affect any component with an attribute of `ref=green`. – SystemParadox Apr 04 '22 at 13:18
14

The only way I can think of is with an additional div element.

App.svelte

<script>
    import Nested from './Nested.svelte'    
</script>

<style>
    div :global(.style-in-parent) {
        color: green;
    }
</style>

<div>
    <Nested />  
</div>

Nested.svelte

<div class="style-in-parent">
    Colored based on parent style
</div>

Multiple Nested elements

You could even allow the class name to be dynamic and allow for different colors if you use multiple Nested components. Here's a link to a working example.

Mike Nikles
  • 1,450
  • 11
  • 16
  • 1
    As I see you use :global so it's not targeting only inside the Nested Component, but all class `style-in-parent` inside the div. I tried it [here](https://svelte.dev/repl/fa67efdea32448e2895c1e5e7fee9458?version=3.6.7). – Zenocode Jul 11 '19 at 13:05
  • 2
    Ah right, good catch. I think the next step is to understand your use case better and why you try to style from the parent component. – Mike Nikles Jul 11 '19 at 13:47
  • In order to avoid leaking in the same component, you should add a class known to be unique, once again in the same component (svelte will add a hash preventing it from leaking outside. Here's an variation of @MikeNikles example: https://svelte.dev/repl/09b33283d787480cb30cab35d5b9d0a0?version=3.6.7 – opensas May 18 '22 at 00:01
11

You could use inline styles and $$props...

<!-- in parent component -->

<script>
import Nested from './Nested.svelte';
</script>

<Nested style="background: green; color: white; padding: 10px; text-align: center; font-weight: bold" />
<!-- in Nested.svelte -->

<script>
    let stylish=$$props.style
</script>

<div style={stylish}>
    Hello World
</div>

REPL

rodgco
  • 159
  • 2
  • 5
  • 5
    One-liner for Nested.svelte: `
    `
    – Georgy K Oct 19 '20 at 07:59
  • This seems to be just what OP asked for, are there any downsides to doing it this way? Looks very straight-forward! – Martin Gunnarsson Jan 28 '21 at 10:06
  • @MartinGunnarsson what if you are using library components? Then you have to go to the source code to add props into divs – cikatomo Jan 09 '22 at 15:50
  • @cikatomo what will be a good solution in your opinion for headless library components like https://www.npmjs.com/package/@bojalelabs/headless-svelte-ui to enable us to add props to divs? – ayooluwa alfonso Feb 11 '22 at 10:05
  • @ayooluwaalfonso sorry, I really don't know – cikatomo Feb 12 '22 at 18:22
  • I found using $$props in a component caused it to get updated more frequently. It was better to have custom props and svelte would more accurately determine if a component updated or not. – tsmigiel Jan 16 '23 at 20:25
8

using :global(*) is the simplest solution.

No need to specify a class in the child if you want to style all immediate children for example

In the parent component:

<style>
  div > :global(*) {
    color: blue;
  }
<style>

<div>
  <Nested />
<div>

Nested will be blue.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Johannes
  • 316
  • 3
  • 4
3

I take a look and found nothing relevant (maybe here), so here is an alternative by adding <div> around your custom component.

<style>
.Nested {
    color: blue;
}
</style>
<div class="Nested">
   <Nested />
</div>

Maybe you will found something but this one works.

Zenocode
  • 656
  • 7
  • 19
  • This approach sets `color: blue` on the entire `Nested` component. I provided an answer below that allows you to targed certain elements within `Nested`. – Mike Nikles Jul 11 '19 at 12:53
  • @MikeNikles nice one, I just discover svelte 20 minute ago, I didn't know about this. – Zenocode Jul 11 '19 at 12:59
  • You made a nice discovery :)! I've used it for a while but it also took me quite a bit to figure out how this all works with styling nested components. – Mike Nikles Jul 11 '19 at 13:04
  • Is the `.` inside the style tag needed? – lolelo Jun 09 '20 at 17:41
  • Yes because you not anymore targeting the `Nested` component but div class above, so in CSS you need to use the dot to trigger it. – Zenocode Jun 09 '20 at 20:56
1

The way I do it is like this:

<style lang="stylus">
  section
    // section styles

    :global(img)
    // image styles
</style>

This generates css selectors like section.svelte-15ht3eh img that only affects the children img tag of the section tag.

No classes or tricks involved there.

pgalle
  • 216
  • 3
  • 13
1

Update for 2023, you can wrap your style tags in svelte:head which will put them in the head, removing them from the scope context.

<svelte:head>
  <style>
    div { color: red };
  </style>
</svelte:head>

This snippet will make all div elements have the color "red". As such, I'd recommend using this sparingly (I'd probably only use it for scripts/content in a legacy way). Only because debugging "why is everything red" will be impossible for projects with significant routes.

The, iirc, "documented" way is through CSS variables.

<!-- Nested.svelte -->
<div>
  <slot />
</div>

<style>
  div { color: var(--nested-color, inherit); }
</style>

===

<!-- Parent.svelte -->
<div>
  <Nested>
    Applesauce
  </Nested>
</div>

<style>
  div {
    --nested-color: red;
  }
</style>
JonShipman
  • 530
  • 4
  • 8