Utilizing Vue 3 slot styles in a child component

Issue with Scoped Styling and Child Component: I am facing a problem while trying to style the contents of a <slot> within a child component using scoped css. Despite applying styles, they do not reflect as expected.

The scenario involves two components:

<!-- Parent.vue -->
<template>
  <h1>{{ msg }} from Parent</h1>
  <Child>
    <h1>{{ msg }} from Child</h1>
  </Child>
</template>
...
<style scoped>
h1 {
  color: green;
}
</style>

<!-- Child.vue -->
<template>
  <slot></slot>
</template>
...
<style scoped>
h1 {
  color: red;
}
</style>

Although I intend for the second <h1> tag to appear red, it ends up being green due to the rendered component having attributes such as:

<h1 data-v-452d6c4c data-v-2dcc19c8-s>Hello from Child</h1>

<style>
h1[data-v-452d6c4c] {
  color: green;
}
h1[data-v-2dcc19c8] {
  color: red;
}
</style>

The suffix "-s" in the attribute data-v-2dcc19c8-s seems to be causing this issue.

This leads me to explore other solutions, possibly involving classes or alternative approaches. However, my limited experience with <slot> has sparked curiosity in understanding these intricacies better. The presence of the "-s" hints at a framework-related aspect that I may have overlooked. Can anyone provide insights on this?

For reference, here's a sample demonstrating the issue:

https://codesandbox.io/s/condescending-brown-ilfwn

Answer №1

Discover the power of using the revolutionary :slotted selector in Vue 3:

Child.vue

<template>
  <slot></slot>
</template>

<script>
export default {
  name: "Child",
};
</script>

<style scoped>
:slotted(h1) {
  color: red !important;
}
</style>

Experience the exciting change in Vue 3 where child scoped styles no longer impact slotted content by default.

For your specific scenario, ensure to include the !important modifier as the parent has also defined a style for h1, which would otherwise take precedence.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to drag items into empty Vuetify v-data-table using SortableJS functionality

I'm looking to implement a drag-and-drop functionality between two v-data-tables in Vuetify using SortableJS. Check out this example with 3 tables: https://codepen.io/arno-van-oordt/pen/MdqrgK Everything works smoothly if both tables have at least on ...

Challenges with the dropdown menu navigation bar

I am struggling with my dropdown menu and sign up/sign in buttons as they are not aligning properly. I have tried various coding methods but haven't been able to fix the issue. Can someone provide me with suggestions on how to rectify this problem? c ...

What are some ways to adjust red and green blocks using CSS?

One question that arises is how to create a version of a webpage where only the yellow block can slide up, while the red and green blocks remain fixed. Currently, the green block is treated with the following CSS: position:sticky; right:0px; top:100px; ...

"Applying CSS styles to HTML elements through PHP code

Hey there, I need help with some CSS styling for my PHP code. The code below is used to display a name: while ($db_field = mysql_fetch_assoc($result)) { print $db_field['ship_name'] . "<BR>"; I really want to jazz it up with some ...

"Resolving Cross-Origin Resource Sharing (CORS) problems with Axios on AWS

Currently, I have my fastapi host running on AWS Elastic Beanstalk. My goal is to create a Vue application that can interact with it seamlessly. However, when attempting the following code snippet: const res = await axios.post(baseUrl, { ...

What is the best way to showcase a message using if-else conditions in Vue.js?

<div class="dimension-section no-border-radius margin-top-2"> <div class="dimension-size"></div> <div> <ul> <li v-on:click="display = !display" @click="getPartingCharges('5-10')" :class="{ active: is ...

Add data to a size guide

My coding project involved creating a sizing chart using HTML, CSS, and JavaScript. The chart allows users to select their preferred units of measurement (metric or imperial). I used JavaScript to dynamically update the values in the chart based on the sel ...

Identifying the relationship between child and parent components in Vue.js

I am new to Vue.js and I am practicing some simple exercises on communication between Vue components. However, I am struggling with understanding who is a child component and who is a parent component. For example, consider the following code snippet: HTM ...

Make sure to verify if all values are contained within an array by utilizing JavaScript or TypeScript

These are the two arrays I'm working with. My goal is to ensure that every value in ValuesToBeCheckArr is present in ActualArr. If any values are missing from ActualArr, the function should return 0 or false. Additionally, there is an operator variabl ...

Dynamically Loading CSS files in a JQuery plugin using a Conditional Test

I'm trying to figure out the optimal way to dynamically load certain files based on specific conditions. Currently, I am loading three CSS files and two javascript files like this: <link href="core.min.css" rel="stylesheet" type="text/css"> & ...

What is the best way to assign a single class from an array to every list item in a particular sequence?

html <ul class="logoUl"> <li class="orange"></li> <li class="blue"></li> <li class="green"></li> <li class="pink"></li> </ul> SCRIPT if (selectedCategory == 'currentAll&apo ...

Activate auto-closing tags feature in .vue files within VS Code

I recently discovered the html.autoClosingTags option in VS code, which allows me to type '' and have it automatically insert those tags with the cursor inside. However, I noticed that this feature only works in .html files. Currently, I am worki ...

Styling Angular Material Forms: Customizing Input Backgrounds and Button Heights

I'm currently working on a simple email input form with a submit button. Here are the adjustments I want to make: Set the background of the email input to white Ensure that the submit button matches the height of the input field However, my attempts ...

Building a Responsive Grid Layout with HTML and CSS: Layering one div on top of another

I'm struggling to find a solution to my problem. I attempted to set my div as absolute but it messed up the entire flexible grid layout. All I need is to place the div above the background div. Thank you in advance! CSS #mannequin { box-sizing: ...

Issue with importing coverflow styles in vuejs using Swiperjs

I am currently working on creating a VueJS slider with the coverflow effect using swiperjs. I was inspired by this example: Coverflow Example However, I seem to be experiencing difficulty in applying the effect to my slider. It appears that I may have mis ...

Ways to remove a row from a div element in JavaScript without relying on tables

Hey everyone, I'm looking for a way to delete a row within a div when a delete button is pressed using JavaScript. The catch is that I need to accomplish this without using a table, only with div elements. Can anyone provide a solution for me? func ...

Is it possible for a div to contain more than one class in Twitter Bootstrap?

Is it possible for a div element to have multiple classes assigned to it? I am currently working with Twitter Bootstrap and I would like to utilize two predefined classes. Specifically, I want to apply the active class to a dropdown-toggle within a navig ...

Vuejs transitions cause fixed elements to move erratically on screen

When navigating between routes in Vue and using transitions, it appears that fixed or absolute elements only move to their positions after the transition has completed. This results in a noticeable jump after the animation is complete. Could this be a bug? ...

Adding a gradient to enhance an SVG chart

Hey there! I'm currently experimenting with Plotly to create some awesome charts, and I've been trying to figure out how to give my area-charts a gradient instead of the usual fill with opacity. This is how I typically build my graph: Plotly.ne ...

In Chrome, the height of 100% is not functioning properly within the <div> element

I've been struggling with a problem that I've searched the internet for solutions to, but haven't been able to resolve. The issue revolves around an iframe containing a page that loads specific CSS rules. html, body { position: relative; he ...