Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling Vue Slot

Is there any way to style a slot in a Vue component?

<slot style="position: absolute"></slot>

and

<slot class="slot"></slot>

do not work.

like image 973
AryanJ-NYC Avatar asked Nov 15 '17 21:11

AryanJ-NYC


3 Answers

Wrap the slot in a <div> and style the <div> instead:

<div style="...">
  <slot></slot>
</div>

If you really need to style the slot element, you can use CSS selectors like this:

<div class="wrapper">
  <slot></slot>
</div>
.wrapper > * {
  color: red;
}
like image 181
Decade Moon Avatar answered Dec 02 '22 18:12

Decade Moon


I found such a way out.

    computed: {
        isAppBoxSlotActive() {
            return Boolean(this.$slots['app-box']);
        }
    },

    <div v-if="isAppBoxActive"
         :class="$style['app-box']">
        <slot name="app-box">...</slot>
    </div>
like image 32
Ilya Degtyarenko Avatar answered Dec 02 '22 18:12

Ilya Degtyarenko


You can pass a class from the parent like so:

In the component template:

<slot name="quoteText"></slot>

And when passing to the slot:

<p slot="quoteText" class="mb-md-100">Text</p>
like image 29
Roy Barber Avatar answered Dec 02 '22 19:12

Roy Barber