How to customize the navigation bar color with Vue 3 and Bootstrap 5

Here's the navigation bar code:

 <nav class="navbar bg-body-tertiary navbar-expand-lg" 
data-bs-theme="dark"      :class="{'navbar-expand-lg navbar-dark bg-dark ' : useDarkNavbar, 'navbar-expand-lg bg-light' : !useDarkNavbar}"  >
    <div class="container-fluid">
      <a class="navbar-brand" href="#">My Vue</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
          <li v-for="(page, index) in pages" class="nav-item" :key="index" >
            <a class="nav-link"
            :class ='{active : activePage == index  }'
            aria-current="page" 
            v-bind:href="page.link.url" 
            :title="`This is ${page.link.text} page`" 
            @click.prevent="activePage = index"
            >{{page.link.text}}</a>
          </li>
          <!-- <li class="nav-item">
            <a class="nav-link disabled">Disabled</a>
          </li> -->
        </ul>
        <form class="d-flex">
          <button class="btn btn-outline-success"
          @click.prevent="useDarkNavbar = !useDarkNavbar"
          >
            Toggle Navbar
          </button>
        </form>
      </div>
    </div>
  </nav>

This is the form for the button that toggles the navbar color:

<form class="d-flex">
          <button class="btn btn-outline-success"
          @click.prevent="useDarkNavbar = !useDarkNavbar"
          >
            Toggle Navbar
          </button>
        </form>

And here is the Vue setup:

const app = Vue.createApp({
// template: '<h2>Hey there</h2>',
data(){//Options object
    return {
        useDarkNavbar:true,
        activePage : 0,
    pages : [
        { link : {text:'Home',url:'index.html'},
            pageTitle : 'Home page',
            content:'This is the home content'
        },
        {
            link : {text:'About',url:'about.html'},
            pageTitle : 'About page',
            content:'This is the about content'
        },
        {
            link : {text:'Contact',url:'contact.html'},
            pageTitle : 'Contact page',
            content:'This is the contact content'
        }
    ],

    }
} }) app.mount('body')

When the button is clicked, the theme alternates between light and dark, but the navbar disappears when switching to dark theme. How can this issue be resolved?

Answer â„–1

There are a number of issues...

  1. useDarkNavbar is located in the parent/app rather than the navbar component. You should utilize a prop to transmit it to the Navbar
  2. The class selections are incorrect. How are you applying data-bs-theme="dark and bg-body-tertiary? This will override any class logic that is being modified
  3. Make sure to utilize navbar-dark and navbar-light as well. These classes determine the foreground/text color for the Navbar.

For more information:


const navBar = {
  template: '#navBar',
  data() {
    return {
        activePage : 0,
        show: true,
        pages : [
            { link : {text:'Home',url:'index.html'},
                pageTitle : 'Home page',
                content:'This is the home content'
            },
            {
                link : {text:'About',url:'about.html'},
                pageTitle : 'About page',
                content:'This is the about content'
            },
            {
                link : {text:'Contact',url:'contact.html'},
                pageTitle : 'Contact page',
                content:'This is the contact content'
            }
        ],

    }
  },
  props: {
      useDark: Boolean
  },
}

Vue.createApp({
  components: {
      navBar
  },
  data() {
    return {
        useDarkNavbar: true
    }
  }
}).mount('#app')

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

How can I efficiently load AJAX JSON data into HTML elements using jQuery with minimal code?

I have successfully implemented a script that loads an AJAX file using $.getJSON and inserts the data into 2 html tags. Now, I want to expand the JSON file and update 30 different tags with various data. Each tag Id corresponds to the key in the JSON strin ...

Animating a child element while still keeping it within its parent's bounds

I have researched extensively for a solution and it seems that using position: relative; should resolve my issue. However, this method does not seem to work in my specific case. I am utilizing JQuery and AnimeJS. My goal is to achieve the Google ripple eff ...

Is there a way to keep a button in a "pressed" state after it has been clicked?

Could someone kindly share the code with me? I am looking for a button that remains pressed after clicking it. At times, I feel embarrassed for struggling with seemingly simple tasks. Grateful for any guidance or solution provided by this community! Man ...

Revamp the Bootstrap Form Upload feature

I am looking to redesign the Bootstrap Form Upload field. The main reason behind this is that in Bootstrap, the file upload field is treated as one single component, making it impossible to separate the file upload button from the file upload text. The fi ...

Display or conceal a div element depending on the user's choice

I am looking to hide inactive divs, but whenever I reload the page, all tab contents merge into one. The screenshot below shows the issue I'm facing. Screenshot Below is the code snippet: $('.tab a').on('click', function ...

Sophisticated CSS design featuring a heart-shaped cutout from the background

I am trying to design a shape using only CSS (no images) that is the opposite of a heart shape. It's a bit hard to explain, so here is a visual representation: The blue represents the background, but the shape I am aiming for is not a heart, it is ac ...

Implement an expand/collapse effect using CSS3 transitions

How can I implement a smooth expand/collapse effect? function expandCollapse(shID) { if (document.getElementById(shID)) { if (document.getElementById(shID + '-show').style.display != 'none') { document.getElem ...

Connecting an external SVG file to its corresponding HTML code

Can I connect my SVG file to my CSS file for a line animation? I'm uncertain about including the SVG code directly in my HTML file. ...

Tips for maintaining an open sub menu while hovering over a link

I'm currently working on a navigation menu using jQuery that includes categories, subcategories, and their subsequent subcategories. However, I am facing some issues with it not functioning as intended. To avoid cluttering this space with excessive HT ...

Conceal the div element when located beneath an ordered list with a designated

I need help hiding the display of comment information if it is a child comment. Below is my attempt to hide the div with the id "info" if the "ol" element has a class of "children". If you have another method for hiding the div when the comment is a chil ...

Activate the div when hovering over the span

Experiencing an issue with triggering a visible div while hovering over a span. Here is the code structure: <ul class="products"> <li> <a href="somelink"> <img src="some image"> <div class="overlay"> Some Text</div> & ...

Tips for incorporating a personalized CSS stylesheet while utilizing Bootstrap

When trying to use my custom CSS alongside Bootstrap, I'm running into some issues. The !important tag doesn't seem to have any effect (I've also heard that using !important is not recommended). Additionally, using IDs instead of classes isn ...

The size of the elements inside a div should be proportional to its parent div

I am struggling with sizing elements within a div. I have set the max-width to 40% and max-height to 25%, and inside is an image that I want to be 80% of its parent div. Here is the code snippet for reference: <div className="inputbox-container&qu ...

Using column-count within a media query is not supported

I am encountering an issue with my CSS code that includes the column-count property within a media query, and for some unknown reason, it doesn't seem to be working! .masonry { -webkit-column-count: 3; -moz-column-count: 3; column-count: ...

JavaScript Cookie to Ensure Form Submission is Limited to a Single Instance

Seeking assistance with automatically submitting a form on page load using JS cookies. Here is the code I have, but it's not functioning as expected. Any guidance would be greatly appreciated. Thank you. I'm unsure about the section in my code th ...

Is there a way to customize the default settings for a blueprint’s menu item?

https://i.sstatic.net/aNeJY.jpg In my react project, I implemented the Menu using MenuItem from '@blueprintjs/core'. The issue I encountered is that when hovering over a Menu item with children, they open from the right side. Is there a way to m ...

Trouble with CSS submenus: Are your lists not appearing as they should? Potential style override

I am encountering an issue with my dropdown mega menu. I have two sets of list items that should be displayed vertically side by side with bullet points in the dropdown, but only one list is appearing despite coding for both. Additionally, the list elemen ...

Box2dweb: Interaction Point of Collision

Currently working with box2dweb for a game development project. Need assistance in determining the contact point between a "Circle" and "Box". Aware that this can be achieved through b2ContactListener and the Post-Solve Event. Seeking guidance on impleme ...

Resizing Images with JavaScript

I have been attempting to create a functionality where an image enlarges itself upon a user click and reverts back to its original size when the cursor is moved away. However, I am facing difficulties as the image is not responding, it's a large 800 p ...

Display the button exclusively on responsive mobile views

I'm looking to enhance my website by adding a call support button with a responsive design feature. The goal is to display the button exclusively in mobile view and replace it with text in other views. Encountering challenges in toggling the visibili ...