Styling didn't work on the default selected nav-link in my Vue 3 project using Bootstrap 5

My first visit here, please be patient.

I am trying to emphasize the selected navigation link, and it seems to work fine for all except the first one (small).

Even after selecting other options (medium or large) which do change color upon selection, when I reselect the small option, its color remains unchanged.

Additionally, the small option is originally black in color.

Here is my code, utilizing Vue with Bootstrap installed:

<template>
  <div >
    <nav>
      <div class="nav nav-tabs" id="nav-tab" role="tablist">
        <button
          class="nav-link car-head active"
          id="nav-ssprinter-tab"
          data-bs-toggle="tab"
          data-bs-target="#nav-ssprinter"
          type="button"
          role="tab"
          aria-controls="nav-ssprinter"
          aria-selected="true"
        >Small
        </button>
        <button
          class="nav-link car-head"
          id="nav-msprinter-tab"
          data-bs-toggle="tab"
          data-bs-target="#nav-msprinter"
          type="button"
          role="tab"
          aria-controls="nav-msprinter"
          aria-selected="false"
        >
          Medium
        </button>
        <button
          class="nav-link car-head"
          id="nav-xlsprinter-tab"
          data-bs-toggle="tab"
          data-bs-target="#nav-xlsprinter"
          type="button"
          role="tab"
          aria-controls="nav-xlsprinter"
          aria-selected="false"
        >
          Big
        </button>
      </div>
    </nav>
    <div class="tab-content" id="nav-tabContent">
      <div
        class="tab-pane fade show active"
        id="nav-ssprinter"
        role="tabpanel"
        aria-labelledby="nav-ssprinter-tab"
        tabindex="0"
      >
Small Text
      </div>
      <div
        class="tab-pane fade"
        id="nav-msprinter"
        role="tabpanel"
        aria-labelledby="nav-msprinter-tab"
        tabindex="0"
      >
Medium Text
      </div>
      <div
        class="tab-pane fade"
        id="nav-xlsprinter"
        role="tabpanel"
        aria-labelledby="nav-xlsprinter-tab"
        tabindex="0"
      >
Big Text
      </div>
    </div>
  </div>
</template>




<script>

export default {
  components: {},
  methods: {},

};
</script>

<style>
.car-head{
color: black !important;
font-weight: bold !important;
}
 .car-head + .active {
  color: rgb(243, 158, 0) !important;
  background: rgb(220, 220, 220) !important;

}
</style>

I have verified the IDs and classes, and even adjusted the CSS styling order at the end, although this resolved a different issue, the problem with the small option persists.

I was hoping that there might be a typo in the class names of the small navigation link causing it to remain black initially and when reselected. However, I couldn't find any other reason for this behavior.

Answer №1

You have implemented the + selector, which indicates selecting the adjacent next sibling element. This method is effective only in specific situations.

In your particular scenario, it seems like you intend to style elements with the car-head class when they also have the active class. To achieve this, the appropriate selector combination to use would be: .car-head.active

To deepen your understanding of CSS selectors, refer to: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

You can also watch a comprehensive tutorial on this topic: https://www.youtube.com/watch?v=l1mER1bV0N0

.car-head {
  color: black !important;
  font-weight: bold !important;
}

.car-head.active {
  color: rgb(243, 158, 0) !important;
  background: rgb(220, 220, 220) !important;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84e6ebebf0f7f0f6e5f4c4b0aab7aab5">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div>
  <nav>
    <div class="nav nav-tabs" id="nav-tab" role="tablist">
      <button class="nav-link car-head active" id="nav-ssprinter-tab" data-bs-toggle="tab" data-bs-target="#nav-ssprinter" type="button" role="tab" aria-controls="nav-ssprinter" aria-selected="true">Small
        </button>
      <button class="nav-link car-head" id="nav-msprinter-tab" data-bs-toggle="tab" data-bs-target="#nav-msprinter" type="button" role="tab" aria-controls="nav-msprinter" aria-selected="false">
          Middle
        </button>
      <button class="nav-link car-head" id="nav-xlsprinter-tab" data-bs-toggle="tab" data-bs-target="#nav-xlsprinter" type="button" role="tab" aria-controls="nav-xlsprinter" aria-selected="false">
          big
        </button>
    </div>
  </nav>
  <div class="tab-content" id="nav-tabContent">
    <div class="tab-pane fade show active" id="nav-ssprinter" role="tabpanel" aria-labelledby="nav-ssprinter-tab" tabindex="0">
      Small text
    </div>
    <div class="tab-pane fade" id="nav-msprinter" role="tabpanel" aria-labelledby="nav-msprinter-tab" tabindex="0">
      medium text
    </div>
    <div class="tab-pane fade" id="nav-xlsprinter" role="tabpanel" aria-labelledby="nav-xlsprinter-tab" tabindex="0">
      big text
    </div>
  </div>
</div>

Answer №2

implementing anchor element and applying the :visited pseudo-class can be a better alternative compared to using buttons

example :

   

.car-head:visited{
   background-color : blue;
}

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

What is the best way to remove the empty space on the right side of a webpage while ensuring it remains responsive?

I noticed that there seems to be some extra space on the right side of the layout. I'm not sure how to adjust the Bootstrap code or CSS to fix this issue, or if I need to apply margin 0 or padding 0 somewhere in the code. * { box-sizing: border-b ...

Fetching limited items from the HackerNews API using Nuxt.js' useFetch

Is there a way to limit the items fetched from the hackernews API in this code snippet? <template> <p>Products</p> <div> <div class="grid grid-cols-4 gap-5"> <div v-for="s in results" :key=&qu ...

Exploring the efficiency of different CSS selectors

I am struggling with the following code: HTML: <ul class="list"> <li class="list-item"> item 1 </li> <li class="list-item list-item_active"> Active item </li> <li class="list-item"> item 3 </li> ...

AngularJS date and time picker featuring user-defined input validation

Is there an AngularJS component available that offers both a date/time picker and the ability to input dates using the keyboard? ...

Creating a standalone card using Bootstrap

Trying to create this... https://i.stack.imgur.com/PMRSI.png However, I'm unsure of how to achieve it. <div class="row justify-content-center"> <div class="col-3 me-3" style="background-color: #F1F3F8; border-r ...

Utilizing Bootstrap's styling features for iOS browser: customizing borders and text in HTML

On the iPhone browser, I am encountering an issue with my code. Specifically, the text disappears and the div border or shadow changes, but when I test it on Safari browser, everything looks fine. The developer version of the website can be found at: Conta ...

`Eliminate a few bbcodes in the code snippet`

I am currently experiencing an issue with a bbcode parser that converts to HTML. Whenever I try to parse the code, I encounter problems with tags that are not in my approved set by the parser. For instance: The parser only permits [b], [center], and [i] t ...

Trying out a newly added function in a Vue.js/Nuxt.js component to test its dynamic capabilities

Currently, I am designing a test for dynamically generated methods within a particular component. The code snippet is outlined below: <template> <div id="app"> <div @click="executeDynamic('myCustomFunction& ...

Display the tooltip only when the checkbox is disabled in AngularJS

My current setup includes a checkbox that is disabled based on a scope variable in Angular. If the scope variable is true, the checkbox stays disabled. However, if the scope variable is false, the checkbox becomes enabled. I am looking to implement a too ...

How can I reduce the size of a Bootstrap 5 carousel that spans the entire screen?

As someone new to web development, I have found using bootstrap to be extremely helpful. However, I am currently struggling with creating a carousel for my website. My goal is to display some pictures in one corner of the page, but the carousel keeps str ...

Error encountered: Trying to access the 'firstname' property of an undefined element while loading a component with an inner component

form.vue <template> <div class="form-content"> <base-input type="text" id="name" v-model="name"/> </div> </template> <script> import BaseInput from 'BaseInput.vue' export default { data () { ...

Switch Bootstrap Tab

I have successfully implemented a bootstrap tab on my webpage and it is functioning as intended. Now, I am interested in adding an additional feature to the tabs. My question is, is it possible to toggle the content area if the same tab is clicked again? ...

Center aligning text within an accordion button using Bootstrap 5 framework

I'm struggling to center align the text inside the accordion button while keeping the arrow on the right side Here is an example of what I'm trying to achieve: https://i.sstatic.net/2brrY.png In the Bootstrap 5 documentation, there's an e ...

Is there a way to add a <video> tag in tinymce editor without it being switched to an <img /> tag?

I am attempting to include a <video> tag within the tinymce editor, but it keeps changing it to an <img> tag. Is there a way to prevent this conversion and keep the <video> tag intact? I want to enable videos to play inside tinymce whil ...

Tips for aligning the timing of two CSS animations

I'm struggling to achieve a synchronized CSS animation where a progress bar fills up and a background changes simultaneously. For example, I want the progress bar to fill in 5000ms, with the background image changing every 5000ms as well. Despite se ...

What are the main differences between Fluid Layout and Grid?

It seems like there's some vital information I haven't come across yet. Are fluid layouts and grid layouts interchangeable? I keep hearing about this baseline concept, but I can't quite grasp its purpose. Does it serve as a guide for alignin ...

Attempting to display a larger version of an image sourced from miniature versions fetched with the assistance of PHP and

I'm dealing with the challenge of displaying thumbnails fetched from a database. PHP is successfully interacting with and presenting my thumbnails. I'm currently faced with the issue of passing the id from the database to the imageID in my JavaSc ...

Saving modified input data for submission using Vue

Objective: The main goal is to have a form interface loaded with an object's current data for editing. This allows the user to open a modal with the form already filled out with the existing information, giving them the option to either edit it or lea ...

When the height of a sibling element restricts the inner content and scroll bar due to the parent element's height being defined in vh

I'm currently working on creating a sidebar that occupies a fixed percentage of the viewport. Inside this sidebar, I want to have an element that remains fixed at the top while allowing the rest of the content to scroll if it exceeds the height of the ...

Eslint for Typescript in Vue is throwing an error with a message "Unexpected token, expecting ','. Make sure to

Whenever I attempted to utilize vue's type assertion, I kept encountering this eslint error. To illustrate, consider the following snippet: data: function () { return { sellerIdToListings: {} as any, // 'as' Unexpected to ...