Effortlessly implement CSS styling in a scoped shadow element with Vue3

I am facing an issue with applying styles to an anchor element in my code. Below is a snippet of the code:

<template>
    <custom-button>
     #shadow-root (open)
     <a href="#"></a>
     <other-custom></other-custom>
    </custom-button>
</template>
<style scoped lang="scss">
  custom-button{
    :deep(a) {
     outline:1px red solid;
     }
  }
</style>

Despite trying various methods like scoped and slotted, I have not been able to get the style applied to the anchor. Any suggestions on how to solve this issue would be greatly appreciated. Thank you for your help.

Here is a new example:

test.vue

<template>
  <custom-button>
  </custom-button>
</template>

<style scoped lang="scss">
.custom-button {
  :deep(a) {
    outline: 3px red solid;
  }
}
</style>

custom-button.vue

<template>
   <a href="#">Link</a>
</template>
<script>
export default {
  name: 'custom-button',
};
</script>

Unfortunately, this solution did not work when using Tolbxela demo environment.

Answer №1

UPDATE

Unfortunately, the solution does not work for the second example. The scoped CSS seems to only apply at the App level, which is causing the issue.

Instead of complicating things, why not just add the styles directly into the custom-button.vue file?

<template>
   <a href="#">Link</a>
</template>
<script>
export default {
  name: 'custom-button',
};
</script>
<style scoped>
a {
  outline: 3px red solid;
}
</style>

This approach works perfectly fine. You can view the updated Codesandbox here

Alternatively, you could try something like this:

<template>
 <div class="wrapper">
  <custom-button></custom-button>
 </div> 
</template>

<style scoped lang="scss">
 .wrapper > a {
    outline: 3px red solid;
 }
</style>

There should be an additional curly bracket in the style section initially.

<style scoped lang="scss">
  .custom-button {
    :deep(a) {
      outline:3px red solid;
    }
  }
</style>

If you remove all custom elements, the code will function as expected.

<template>
    <button class="custom-button">
     #shadow-root (open)
     <a href="#">Link</a>
   </button>
</template>

https://i.stack.imgur.com/4iEqf.png

You can find the corresponding Codesandbox link here

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

Persisted state in Vuex fails to retain data after the page is refreshed

I recently added persisted state to my Vue application using the command npm install --save vuex-persistedstate. After that, I configured my Vuex store.js file in the following way: import Vue from 'vue' import Vuex from 'vuex' import ...

Error while setting up Vuejs3: During npm installation, the argument for "path" must be a string or an instance of Buffer or URL

Attempting to build a vuejs3 docker image, encountering an error during npm install: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab2bba8f7acbbb6b3bebbaeb5a89aeff4ebf4ef">[email protected]</a&g ...

populating all available space in layouts using bootstrap columns

I am attempting to create a grid layout using bootstrap, but I am facing an issue with positioning one of the columns. I have tried adjusting the placement of the divs and using offsets/pulls, but so far I have not been successful. To explain what I am a ...

Sliding Image Menu using jQuery

I am struggling with creating a menu using jquery mouseenter / mouseout effects. My goal is to have a small icon displayed that expands to the left and reveals the menu link when a user hovers over it. The issue I am facing is that the effect only works w ...

What are the reasons for not utilizing JSS to load Roboto font in material-ui library?

Why does Material-UI rely on the "typeface-roboto" CSS module to load the Roboto font, even though they typically use JSS for styling components? Does this mix of JSS and CSS contradict their usual approach? ...

Spacing for Bootstrap Navbar List Items

I've searched through the CSS and I'm unable to identify what is causing the width between the top level links on my navbar. I need them to be slightly smaller so that when it adjusts for a screen below 900px, it doesn't convert into a 2-row ...

Reset the queue for the slideDown animation using jQuery

I am experiencing an issue with my code where multiple animation effects are running simultaneously. Specifically, when I hover over navbarli1 and then move to another li element with the same navbarli1 class, the slide-down effect is not working as expect ...

I am experiencing difficulty with aligning my input boxes to the center in my React-Native

Just a heads up, this is my first time diving into the world of React Native, so bear with me as I try to figure things out. Please be kind if my question seems silly... Here's a snapshot of what I currently have: https://i.stack.imgur.com/VWGFm.png ...

Utilize the ::before selector to insert white spaces

I attempted this solution, but unfortunately it did not produce the desired outcome: .stackoverflow::before { font-family: 'FontAwesome'; content: "\f16c "; } <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-aw ...

Challenges with line height in IE when adjusting font size in textarea

I'm facing an issue with adjusting the font size in a textarea using JavaScript. While it works perfectly in Firefox, there are some problems in IE. Specifically, the line-height does not change accordingly. This results in gaps between lines when the ...

Issues arise when attempting to load a vue module within a micro-frontend setup

Still fairly new to VUE, I'm excited to implement the best practices I've picked up from other frontend technologies, particularly the micro-frontend approach. While I have successfully been able to dynamically load my module, I've hit a roa ...

Is it possible to use CSS transitions to animate an absolute positioned element?

I am having trouble getting a CSS transition to work for animating an element's position change. I have tried using the all properties in the transition declaration like in this example: http://jsfiddle.net/yFy5n/3/ However, my goal is to only apply ...

Move your cursor over X or Y to alter the color of Y exclusively

I am currently designing a navigation bar that includes icons followed by the title of their respective pages (for example, an icon of a home followed by the text 'Home'). I would like to change the color of only(!) the icon from black (default) ...

What could be causing my CSS relative positioning to malfunction?

I am struggling to adjust the positioning of my divs using relative and absolute properties. Any advice on how to resolve this issue would be greatly appreciated. Thank you. Example Code: <div id="game"><img src="gta-game.jpg" height="100" width ...

Trouble with Styling React-Toastify in TypeScript: struggling to adjust z-index in Toast Container

Currently in the process of developing a React application utilizing TypeScript, I have incorporated the React-Toastify library to handle notifications. However, encountering some challenges with the styling of the ToastContainer component. Specifically, ...

Uninterrupted Horizontal Text Scrolling using Pure CSS

I am currently working on developing a news ticker that displays horizontal text in a continuous scrolling manner, without any breaks between loops. While I hope to achieve this using only CSS and HTML, I'm unsure if it's feasible. Here is my ini ...

Switch or toggle between colors using CSS and JavaScript

Greetings for taking the time to review this query! I'm currently in the process of designing a login form specifically catered towards students and teachers One key feature I'm incorporating is a switch or toggle button that alternates between ...

Utilizing MUI for layering components vertically: A step-by-step guide

I am looking for a way to style a div differently on Desktop and Mobile devices: ------------------------------------------------------------------ | (icon) | (content) |(button here)| ----------------------------------------- ...

Conceal excessive content on elements set in a stationary position

How can we prevent overflow of a fixed div within a container? Initially, I attempted nesting fixed elements inside each other, but that did not solve the issue. The only solution that comes to mind is using "inverted" masks: other fixed divs that hide eve ...

Issue: Elements within an iteration must include a 'v-bind:key' directive. Vue/Require-v-for-key error detected

As someone who is just starting to learn Vue, I recently attempted to create a commercial product page by following a tutorial. However, upon trying to run 'npm run serve', I encountered an error that read: Error: Elements in iteration expect to ...