Customize the color of router-link text in your Vue.js app

Within my coding project, I am utilizing vue.js in the App.vue file.

I have set up route-links for goods, ratings, and seller components.

Now, I am trying to customize the colors of these links using a style module.

Below is the template section of the App.vue file:

<template>
<div id="app">
 <v-header></v-header>
 <div class="tab">
<div class="tab-item">
  <router-link to='/'>goods</router-link>
</div>
<div class="tab-item">
  <router-link to='/rating'>ratings</router-link>
</div>
<div class="tab-item">
  <router-link to='/seller'>seller</router-link>
</div>
</div>
<router-view></router-view>
</div>
</template>

Next is the style module within App.vue:

<style lang="stylus" rel="stylesheet/stylus">
 #app
  .tab
    display: flex
    width: 100%
    height: 40px
    line-heigh: 40px
    .tab-item
      flex: 1
      text-align: center
     & > router-link
      display: block
      color: rgb(240, 20, 20)
 </style>

However, I noticed that this specific definition wasn't effective:

& > router-link
      display: block
      color: rgb(240, 20, 20)

Upon inspecting it in Chrome's Elements tab, I observed that:

<route-link to='/'>goods</router-link>

had been modified to

<a href="#/" class="router-link-exact-active router-link-active">goods</a>

Even when I tested using a regular 'a' tag instead of route-link, the issue persisted, like so:

& > a
 display: block
 color: rgb(240, 20, 20)

I'm unsure why this behavior is occurring. Can anyone offer assistance?

Answer №1

Regrettably, you are unable to target a custom Vue.js component by using the css selector notation directly. One workaround is to assign a class to your router-links and then select them based on that class name. While not as straightforward as selecting by element type, this method will suffice. For instance, transform

<router-link to='/seller'>seller</router-link>
into
<router-link to='/seller' class="routerlink">seller</router-link>
. Afterwards, in your CSS code, modify this:

& > router-link
  display: block
  color: rgb(240, 20, 20)

To look like this:

& > .routerlink
  display: block
  color: rgb(240, 20, 20)

For further insights, refer to this related query: Vue: What is the cleanest way to select a component via CSS?

Answer №2

Apply this CSS Style to your component:

<v-btn flat class="font-weight-bolder text-white" to="/contact">

.v-toolbar__items .v-btn:not(.v-btn--floating):not(v-btn--icon),
.v-toolbar__items .v-menu,
.v-toolbar__items .v-menu__activator {
  text-decoration: none;
}

Answer №3

Another option is to implement a {custom CSS style} as it will automatically change to a hyperlink, this solution proved effective for me.

Answer №4

You have the option to define a default tag:

<router-link :to="/path" exact tag="div"> </router-link>

In your CSS section, you can also customize the tag with:

.router-link-exact-active{ // add your unique style 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

The hovering effect on the image link is causing the surrounding div to stretch too far

Whenever I hover over specific points below an image link wrapped in a div, it triggers the hovering effect for the CSS-created image link. Despite trying various solutions like overflow:hidden and display:inline-block, nothing seems to resolve this issue. ...

The npm run watch command in Laravel 7 is not functioning properly with Node v10.15.0 and NPM v6.5.0

I encountered a problem while using Laravel and Vue. After attempting to compile everything with npm run watch, I started receiving the following error messages: It seems that additional dependencies need to be installed. Please wait a moment. Running: y ...

What are your thoughts on the combination of Vue.js and Codenvy

I have been attempting to set up a basic Vue.js workspace, but everything I try seems to be unsuccessful. Whether I build it from scratch or use a Git repository, nothing seems to work at all. Is it even possible to get it to work? To Reproduce: Create ...

Adjusting the image placement within a modal window (using bootstrap 3)

Behold, an example modal: <!-- Large Modal --> <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg"> <div class="modal-content"> ...

Dropdown Navigation - Utilizing jQuery and CSS

I am encountering an issue with a dropdown menu I have been working on. Take a look at this screenshot for reference: Below is the snippet of HTML code: <ul class="topnav"> <li><a href="#">Home</a></li> <li> ...

integrating material design components into Vue.js applications

My website is built on the Vue framework and webpack technology. I recently discovered Google's Material Components Web CSS framework, which can be easily implemented using a cdn or npm package for basic html/javascript websites. However, I am encoun ...

Tips for Accessing Values in a Dynamic Array in Vue.js

ArrayOrdered :[ { name :"PRODUCT 1", price :"20", amount:"10", Total 1:" ", discount : "" , Total 2:" " }, { name :"PRODUCT 2", price :"50", amount:"20", Total 1:" ", discount : "" , Total 2:" " }, { name :"PRODUCT 3", price :"15.5", amount:"10", Total ...

Aligning content within a table cell using the Div tag

I am struggling with a <td> that looks like this: <td valign="middle" class="table_td td top" style="width: 347px"> <div id="Style" style="position:absolute;"> <img src="images/additional-keywords-image.gif" style="background: ...

Is building scalable single-page applications with websockets more challenging than it seems?

As a newcomer to web technologies, I recently learned that frontend frameworks often require separate application servers for the frontend and backend server (API). For example, when using vue.js, a vue server is needed for the frontend while a MEAN stac ...

The Vue warning message indicates that the prop "scrollThreshold" did not pass the type check. The expected type was Number, but a String was provided instead

Every time I open my App.vue toolbar, I encounter this error. [Vue warn]: Invalid prop: type check failed for prop "scrollThreshold". Expected Number, got String. <v-toolbar dark color="pink darken-4" class="toolbar" flat fixed ...

The img-fluid class is not properly adjusting the height of the image

I'm currently working with Bootstrap 4 and facing a challenge with creating a grid of 4 images - 2 on top and 2 at the bottom. I've applied the img-fluid class, but the image resizing is based solely on width, leading to the height being too larg ...

React: Animate with CSS

How can I make sure that every time a new user is prepended, the first div has a CSS animation? Currently, my code only works for the first prepend and the animation does not trigger on subsequent ones. const [userList, setUserList] = useState<any> ...

When creating a dynamically generated class name, the class will not display any arbitrary values

I have encountered a strange issue: I am trying to dynamically populate the content of an after: pseudo element within a computed value. What's interesting is that it works fine if the key is a string literal before returning the object, but not if t ...

css center arrow-aligned underline text

Trying to emphasize my text with an additional arrow centered on the line is proving to be a challenge. When attempting this, the text does not align in the center of the page. .souligne { line-height: 17px; padding-bottom: 15px; text-transform: u ...

Top section of a table repaired

It seems that the position fixed attribute is not working as expected in this scenario. I might be missing something. I'm trying to keep the input boxes in the first row frozen when scrolling, but none of the solutions I've found seem to be effe ...

Developing User-Friendly Websites with Responsive Design

While I was working on my project, I realized that using only pixel values in tables and sidebars was a big mistake. This caused issues because if someone had a different screen resolution, my website would look distorted and unappealing. Could you please ...

Tips on avoiding duplicate selection of checkboxes with Vue.js

Recently delving into vue.js, I encountered a challenge with multiple checkboxes sharing the same value. This resulted in checkboxes of the same value being checked simultaneously. How can this issue be resolved? var app = new Vue({ el: '#app&apo ...

Top tips for effectively handling two CSS frameworks simultaneously within a project

In the midst of developing a Single Page Application (SPA) project, I am faced with the challenge of integrating two conflicting CSS frameworks: one that is considered "new and fresh" and another that is deemed "old and painful". The requirement is that ev ...

Unexpected Error with Background Position Variable

Hello, I am attempting to create an animated background effect that moves up and down using the .animate() and .hover() methods in jQuery. Within my DOM, there is a div with id="#menu" containing a UL list where each item has a background positioned at dif ...

Do my media queries accurately reflect the design?

I am new to web design and currently taking a course. As part of my assignment, I need to make my website responsive. However, I am having trouble with my media queries. Despite hours of research and tweaking, some elements are not responding as expected. ...