The: active pseudo- class is not the same as adding a class to style an element.
The: active CSS pseudo-class represents an element (such as a button) that is activated by the user. When using a mouse, “activation” usually begins when the mouse button is pressed and ends when it is released.
We are looking for a class, for example .active , which we can use for the .active navigation element.
For a clearer example of the difference between :active and .active see the following snippet:
li:active { background-color: #35495E; } li.active { background-color: #41B883; }
<ul> <li>:active (pseudo-class) - Click me!</li> <li class="active">.active (class)</li> </ul>
View router
vue-router automatically applies two active classes, .router-link-active and .router-link-exact-active , to the <router-link> component.
This class is automatically applied to the <router-link> component when its target route matches.
This works by using inclusive compliance behavior. For example, <router-link to="/foo"> will apply this class if the current path starts with /foo/ or is /foo .
So, if we had <router-link to="/foo"> and <router-link to="/foo/bar"> , both components would get the router-link-active class when the path was /foo/bar ,
This class is automatically applied to the <router-link> component when its target route matches exactly . Bear in mind that both classes, router-link-active and router-link-exact-active , will be applied to the component in this case.
Using the same example, if we had <router-link to="/foo"> and <router-link to="/foo/bar"> , the class router-link-exact-active would only apply to <router-link to="/foo/bar"> when the path is /foo/bar .
Suppose we have <router-link to="/"> what happens if this component is active for each route. Perhaps this is not what we need, so we can use the exact support, for example, like this: <router-link to="/" exact> . Now the component will apply the active class only when it exactly matches / .
CSS
We can use these classes to style our element, like this:
nav li:hover, nav li.router-link-active, nav li.router-link-exact-active { background-color: indianred; cursor: pointer; }
The <router-link> been modified using tag prop, <router-link tag="li"/> .
Change default classes globally
If we want to change the default classes provided by vue-router globally, we can do this by passing some parameters to the vue-router instance, for example, like this:
const router = new VueRouter({ routes, linkActiveClass: "active", linkExactActiveClass: "exact-active", })
Change default classes for component instance ( <router-link> )
If instead we want to change the default classes for <router-link> and not globally, we can do this using the active-class and exact-active-class attributes, like so:
<router-link to="/foo" active-class="active">foo</router-link> <router-link to="/bar" exact-active-class="exact-active">bar</router-link>