Tips for utilizing Vue.js scoped styles with components that are loaded through view-router

I want to customize the styling of Vue.js components loaded through <view-router> using scoped styles.

This is the code I have:

<template>
  <div id="admin">
    <router-view></router-view>
  </div>
</template>

<style lang="scss" scoped>
#admin {
  .actions {
    display: none;
    span {
      cursor: pointer;
    }
  }
}
</style>

When I navigate to the /posts page, a component called Posts is loaded. Inside this component, there is a

<div class="actions">
  some content
</div>

The issue is that the styles defined in #admin are not being applied to the .action element when the styles are scoped. Everything works fine when the styles are not scoped. Is there a way to achieve this while still keeping the .actions styling within the scoped style tag of the admin component?

Answer №1

Here is a helpful link you may find useful

If your code snippet utilizes SCSS, you will need to utilize the /deep/ combinator.


To make a selector in scoped styles affect child components as well, you can use the >>> combinator:

<style scoped>
.a >>> .b { /* ... */ }
</style>

The above code will be translated into:

.a[data-v-f3f3eg9] .b { /* ... */ }

Some pre-processors like SASS may struggle with parsing >>> correctly. In such cases, the /deep/ combinator can be used instead - it functions as an alias for >>> and behaves the same way.


As a result, your code will look like this:

<style lang="scss" scoped>
  #admin {
    some-property: some-style;

    /deep/ .actions {
      some-property: some-style;
    }
  }
</style>

Answer №2

To organize your styling better, consider putting them in a dedicated file and linking it to all the components that require it:

<link href="path/to/your/styles" rel="stylesheet" type="text/css" />

Answer №3

One of the main reasons why styling won't pass to a child element within a router-view tag is due to its nature as an unstyle-able HTML tag. Despite attempting to apply styles, it simply won't render any changes.

Explanation: The router-view essentially serves as a placeholder for Vue to insert the corresponding component or View based on the route. Consequently, this specific tag doesn't appear in the compiled markup; only the loaded View or Component will be visible.

This functionality resembles how the App Entry point functions for a Vue app within the index.html file, based on my personal experience and knowledge.

In addition, building on @Antonio Trapani's insight, you can streamline your styling process by creating a global SCSS file containing all necessary styles for multiple components. By importing this file into the App.vue, you can ensure consistent styling across the entire application. This approach has been effective in my experience as well.

I hope these tips prove helpful. Cheers!

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

Declarations and expressions in Node.js using JavaScript

Recently diving into Node Js, I stumbled upon the following code snippet in a tutorial const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const questions = [ " ...

Creating CSS3D matrices

My goal is to achieve a specific effect using CSS3 and JavaScript, creating a MouseOver effect when the mouse moves over the center div. I've developed a small library that takes in three arguments (element, sourcePoints, destination points) and retu ...

Is it possible to manually trigger a version change transaction in IndexedDB?

I have been working on a Chrome extension that uses the IndexedDB to store data client-side in an IDBObjectStore within an IDBDatabase. The data structure requires users to be able to modify the object store freely, such as adding new objects or modifying ...

Utilizing Django Data for Dynamic Google Charts

Observation: def view_page(request, template = 'home.html'): if request.user.is_authenticated(): data = [['jan'],[12],[-12]] context = { 'data' : data, } return render( request, te ...

What is the best way to make a div span the entire height of the body?

I have a menu that I want to wrap inside a yellow div. The yellow div should take up the entire height of the green square, regardless of the content size in the green square. For example, if the orange square expands, the yellow div should also expand. h ...

Generate an animated sequence transitioning smoothly from the left side to the center

I am in need of creating an animation that will cause a menu to appear from the left side of the screen when a button is clicked. The menu should expand to cover 55% of the width of the main page. You can view a demo of this animation on JSFiddle by follo ...

When a directive is passed a string with HTML tags, the tags are not displayed/rendered as expected

When it comes to passing a string with HTML content to my Angular directive, I've encountered an issue where the rendered output treats the HTML as text. While researching possible solutions, most of them seem to rely on using ng-bind-html directive, ...

How Vue.js code behaves differently when running in development mode

I have a setup where my front-end is built with Vue.JS and the backend runs on NodeJS using ExpressJS. The Express server also serves the statically-built Vue app. The front-end communicates with the Express backend through REST and websockets, utilizing ...

Strict mode does not permit the use of octal literals

I am currently working with Angular 2. When I incorporate this code in my SCSS file, everything runs smoothly. .text::after { content: "\00a0\00a0"; } However, if I move it to the styles: [``] I encounter the error: Uncaught SyntaxErro ...

Is there a way to determine if a string is empty, even if it contains hard returns?

I am currently working on a function that checks if a string is empty or not, but it seems to be missing the detection of new lines. export const isStrEmpty = function(text: string): boolean { return !text || text.match(/^ *$/) !== null; }; I attempted ...

Text woven seamlessly with a series of different scenes

After contemplating the use of three.js for incorporating 3D visualization into my Sphinx extension for model visualization, I realized that the current tool I am using is outdated and not being maintained. The three.js library offers a variety of example ...

Tips for resolving rendering page issues in an express app

My application is a straightforward blog platform that showcases a schema for the title, entry, and date of each blog post. There is also an edit/delete feature that is currently under development. When attempting to use the edit/delete button on a selecte ...

I attempted to utilize React state mapping, but unfortunately, I am not receiving any data in return

I am new to React and encountered a challenge today, seeking some assistance. The task for the Home component is to show a card with data retrieved from an API. Here is my attempt: <div> { allRecipes?.map((e) => { ...

What is the best way to update a Vue.js input element using JavaScript?

Looking to use a Vue.js input for automated testing purposes. How can I successfully set a value and submit it without it being automatically changed back to default data? I have tried the following method to set the value, but it keeps getting overridden ...

Something strange happening with the HTML received after making a jQuery AJAX request

My PHP form handler script echoes out some HTML, which is called by my AJAX call. Below is the jQuery code for this AJAX call: $(function() { $('#file').bind("change", function() { var formData = new FormData(); //loop to add ...

Flexbox implemented for responsive Three Column Layout

Recently, I set up a small poll with three layout columns. On Desktop, everything looks great but the problem arises when viewing it on a mobile screen. People are displayed one under the other followed by the questions. I want to rearrange it so that each ...

PHP and MySQL combination for efficient removal of multiple dropdown choices

I'm struggling to figure this out. Essentially, it's a form with multiple dropdown menus where users can select one or more items to filter MySQL data results. I want to dynamically update the other dropdown menus as the user makes selections. Fo ...

Transforming the inputted URL into a clickable hyperlink

I have a text input field where any text entered is displayed below using angular.js. However, I am trying to convert any http URL entered into a clickable link. I found reference to the solution on this Stack Overflow page. Despite successfully converting ...

JavaScript: The hyperlink provided in the data-href attribute is not functioning properly within the carousel

I have a carousel with images that I want to link to specific pages: JS Fiddle. My goal is to have each image in the carousel direct users to a different webpage when clicked. For example: Clicking on the wagon image should go to wagon.com Clicking on th ...

Tips for verifying the direct function invocation in nestjs

I have been attempting to make a direct function call and have set up the DTO, but it doesn't seem to be working. Here is the code segment where I define my DTO in the controller: @Post('/Send') async SendE(body: Send) { const mail = ...