The utilization of bootstrap can lead to CSS interruptions

I am trying to create a navigation button that transforms from 3 horizontal lines to an X shape when clicked.

    var anchor = document.querySelectorAll('button');
    [].forEach.call(anchor, function(anchor) {
      var open = false;
      anchor.onclick = function(event) {
        event.preventDefault();
        if (!open) {
          this.classList.add('close');
          open = true;
        } else {
          this.classList.remove('close');
          open = false;
        }
      }
    });
html,
body {
  height: 100%;
}
body {
  background-color: white;
  font-family: "Source Sans pro", sans-serif;
}
#mbtn {
  display: inline-block;
  margin: 0 1em;
  border: none;
  background: none;
}
#mbtn span {
  display: block;
}
.lines-button {
  padding: 2rem 1rem;
  transition: .3s;
  cursor: pointer;
  /* */
}

(lines and CSS properties are retained)

<button type="button" role="button" aria-label="Toggle Navigation" id="mbtn" class="lines-button x">
  <span class="lines"></span>
</button>

The functionality works perfectly without the presence of bootstrap.min.css in the HTML code. However, when I include my Bootstrap CSS, it conflicts with the existing navigation CSS and causes it to malfunction.

Answer №1

the problem arises because the bootstrap framework already contains predefined styles for the .close class. To resolve this, simply replace the .close class with a different name

check out the demonstration - http://jsfiddle.net/Ah6XA/34/

you can find the bootstrap.min.css included in this fiddle as well

changed .close to .change for better clarity

var buttons = document.querySelectorAll('button');

[].forEach.call(buttons, function (btn) {
    var isOpen = false;
    btn.onclick = function (event) {
        event.preventDefault();
        if (!isOpen) {
            this.classList.add('change');
            isOpen = true;
        } else {
            this.classList.remove('change');
            isOpen = false;
        }
    }
});

here are the updated css styles

.lines-button.change {
    -webkit-transform: scale3d(0.8, 0.8, 0.8);
    transform: scale3d(0.8, 0.8, 0.8);
}
.lines-button.x.change .lines {
    background: transparent;
}
.lines-button.x.change .lines:before,
.lines-button.x.change .lines:after {
    -webkit-transform-origin: 50% 50%;
    transform-origin: 50% 50%;
    top: 0;
    width: 4rem;
}
.lines-button.x.change .lines:before {
    -webkit-transform: rotate3d(0, 0, 1, 45deg);
    transform: rotate3d(0, 0, 1, 45deg);
}
.lines-button.x.change .lines:after {
    -webkit-transform: rotate3d(0, 0, 1, -45deg);
    transform: rotate3d(0, 0, 1, -45deg);
}

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

`CSS Animation fails to run in Internet Explorer and Microsoft Edge browsers`

My issue with the CSS animation is that while the opacity animates properly, the translate effect does not seem to work as expected. You can see the problem here: https://jsfiddle.net/bLxb8k3s/ It appears that the reason for this issue is because IE and ...

Is it possible for CSS to accurately crop images by pixels [Sprite Box]?

I attempted to replicate the math used for the previous image cuts and added my own image, but it appears blank. Can you identify where the issue lies? #paymentForm { -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; -webk ...

What is the best way to display Bootstrap dynamic tabs in a single row without them overflowing and requiring a scroll button to access all the tabs

Is there a way to dynamically display Bootstrap tabs? I want the content in the tab to show when the link is clicked, and also have a close button. I need the tabs to be aligned in a single row without wrapping down if they don't fit. Here's an ...

How can I make my navbar visible once a specific section has been reached?

Is there a way to conditionally display my navbar in react only when a specific section is reached? Currently, I am monitoring scroll position but this method becomes unreliable on larger screens due to varying scroll positions. I need the navbar to beco ...

Animation of underline on hover for NavLink

As a newcomer to React, I am currently exploring how to create an animated underline effect when hovering over a NavLink from reactstrap. I have managed to get the underline working on hover thanks to the solution provided at , but I am struggling with imp ...

Exploring PHP Queries with Div Classes

I'm in desperate need of assistance. I'm currently working on putting together a website and pulling data from a mysql database. The specific div that I'm trying to populate is provided below. The issue I'm facing is how do I integrate ...

What is the best way to cut off multiple lines of text and display the remaining strings at the

Currently, I am working on a project that involves implementing a gallery feature. The challenge lies in displaying file names which can be quite lengthy, and we are restricted to showing only 2 lines of text. While this can be achieved using line clamp an ...

Revamp selected bootstrap attribute

Imagine a scenario where there is: @media (min-width: 576px) { #projects-edit-middle-column .equal-columns-wrapper .equal-columns { width: 50%; padding-right: 25px; } } This is being utilized within a container: <div class="equal-columns ...

Unusual display of fonts on Chrome

Recently, I encountered a strange issue with font rendering on Chrome/Opera compared to Firefox. The problem can be seen in the preview below - pay attention to the pink text. It appears blurry initially but sharpens once text input is focused. This same i ...

What is the best way to locate an item reference for a specific DOM element?

Imagine a vast DOM structure with approximately 10,000 HTML elements, including 1,000 span tags. None of the span elements have any identifiers and are buried deep within other objects with long, complex Xpath paths that are not user-friendly for selectio ...

Zoom out the slider when scrolling

Take a look at this link and as you scroll down the page, notice how the image transitions to iPhone. Can anyone provide insight on how this effect is achieved? ...

What is the best way to rearrange the order of divs when the screen is being resized?

My task involves rendering a page with react-bootstrap on desktop, structured like this: <Row> <Col xs={12} md={8} className="a"> </Col> <Col xs={6} md={4} className="b"> </Col> <Col xs={6} md={4} className ...

Add the content of a JavaScript variable to a label without using any scripting

Is there a way to concatenate the value of a JavaScript variable with a label without utilizing jQuery? For example: Var global_message = "1234"; <label id="my-label">Test</label> I desire the concatenated value "Test1234" to be displayed o ...

Adjust the size and position of the text input field

I'm struggling to find a way to both resize and move the box without one action interfering with the other. Whenever I attempt to change the size of the box, it triggers the move event at the same time. Clicking on the box should make it resizable, bu ...

What is the best way to position an image beside a jquery dialog box?

Apologies for not finding a suitable topic to discuss. Is it possible to place an image next to a jQuery dialog, similar to the following example? If so, how can this be achieved? ...

Automatically expanding PrimeNG Turbotable rows

I am using a PrimeNg turbotable that has a row expansion feature enabled. I am looking for a way to automatically expand the rows by default when the table loads. Shown below is my code: HTML <p-table [columns]="cols" [value]="cars" dataKey="vin"> ...

What is the best way to align paragraphs vertically within a required square-shaped div using CSS (with images for reference)?

I am trying to style a table in a specific way, but I'm facing some challenges. First, I want to make the internal div inside the table square with a percentage height. Next, I need to have two paragraphs stacked on top of each other within the same ...

Having trouble with submitting an HTML form using JavaScript and PHP

My webpage has an HTML form that sends data to a PHP file for processing. One issue I'm facing is with a dynamically generated combo box that works fine when the page loads, but the selected value is not being passed when the form is submitted. The J ...

What are the steps for integrating bootstrap into an Angular project?

I recently added Bootstrap to my Angular project using the npm install bootstrap command and referencing the necessary CSS file in the styles property of the angular.json file. However, when I run my application, I notice that Bootstrap is not being applie ...

Inflexible lists refusing to wrap

I have a straightforward requirement: I need a list <ul /> containing items <div />s of the same fixed size. These items should be displayed from left to right, filling up all available space before wrapping onto the next line. Below is the ba ...