Switch to using the local Bootstrap 4 CSS file as a backup

I tried using the answer from , but unfortunately, it didn't work for BS4. Even after attempting André Rocha's solution at the end, my issue remains:

My goal is to load Bootstrap 4 via CDN, with a fallback to a local CSS file if needed.

The problem I'm facing is that the bootstrap 4 css file is being loaded twice, when I only want it to be loaded once.

Below is the code (similar to the one in the aforementioned answer). The fallback function for the CSS file is placed at the very end:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- Bootstrap 4.1.1 CSS CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
    <title>Home | phpMIDAS viewer</title>
  </head>
  <body>
    <!-- jQuery 3.3.1 CDN -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <!-- Bootstrap 4.1.1 JS CDN -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" ></script>
    <!-- Bootstrap 4.1.1 JS local fallback -->
    <script>if(typeof($.fn.modal) === 'undefined') {document.write('<script src="bootstrap/js/bootstrap.min.js"><\/script>')}</script>
    <!-- Bootstrap 4.1.1 CSS local fallback -->
    <div id="bootstrapCssTest" class="hidden"></div>
    <script>
      $(function() {
        if ($('#bootstrapCssTest').is(":visible")) {
          $("head").prepend('<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">');
        }
      });
    </script>
  </body>
</html>

Answer №1

After some experimentation, I discovered that simply changing class="hidden" to class="collapse" was all that was needed.

For those interested, here is the updated code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- Bootstrap 4.1.1 CSS CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
    <title>Home | phpMIDAS viewer</title>
  </head>
  <body>
    <!-- jQuery 3.3.1 CDN -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <!-- Bootstrap 4.1.1 JS CDN -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" ></script>
    <!-- Bootstrap 4.1.1 JS local fallback -->
    <script>if(typeof($.fn.modal) === 'undefined') {document.write('<script src="bootstrap/js/bootstrap.min.js"><\/script>')}</script>
    <!-- Bootstrap 4.1.1 CSS local fallback -->
    <div id="bootstrapCssTest" class="collapse"></div>
    <script>
      $(function() {
        if ($('#bootstrapCssTest').is(":visible")) {
          $("head").prepend('<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">');
        }
      });
    </script>
  </body>
</html>

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

Customize the appearance of disabled dates in the eonasdan-datetimepicker styling

I am seeking to customize the default styles for the "eonasdan-datetimepicker" (https://github.com/Eonasdan/bootstrap-datetimepicker) by implementing a basic hover message feature. The CSS attributes currently applied to disabled dates are as follows: .bo ...

Tips for positioning a button beside a ListItem

Is there a way to place a button next to each list item in ASP.NET? <asp:CheckBoxList ID="lstBrembo" runat="server"> <asp:ListItem Text="Popular" Value="9"></asp:ListItem> // <--- trying to add button here, but getting parse error ...

The .on() function in Jquery seems to be malfunctioning when it comes to handling dynamically

Despite my efforts to find a solution after exploring other questions and attempting various possible fixes, I am still unable to correctly bind the click event. Any assistance would be greatly appreciated. My current project involves a webpage that intera ...

Dynamic font sizing in CSS allows text on a webpage to

I am working on creating a dynamic screen using AngularJS. Within this screen, there are objects with a specific size: .item { margin: auto; margin-bottom: 10px; width: 11vw; height: 11vw; text-overflow: ellipsis; overflow: hidden; } These i ...

Changing the direction to reverse column will cause the navigation component to be hidden

Issue: The <Navigation/> components disappear when using flex-direction: column-reverse. However, if I switch to flex-direction: column, the component appears at the top of the screen and is visible. My objective is to display my <Navigation/> ...

A step-by-step guide on creating an automatic image slider using the Swiper class in HTML

I am facing an issue with the swiper-container swiper-slider class, where the images are not sliding automatically. I need the image slider to work automatically without the need to click the next and prev buttons. Below is my code for reference. Instead o ...

Displaying a checkbox within an input field

I'm attempting to incorporate a datepicker that includes the ability to disable its input field. The goal is to have the input field disabled when "Never Expire" is selected, as shown in the image below. https://i.sstatic.net/qRC1t.jpg Here's w ...

How to Fix the Issue of CSS Font Face Path Not Working in Your Express Application

My current font files are stored in the public/fonts/ directory. The CSS files can be found in public/css/. Despite trying various path and css adjustments, I am unable to successfully load the fonts... @font-face {font-family: trend_sans_oneregular; ...

Can a small white pop-up be triggered by clicking a button?

While exploring the website , I noticed that clicking on Availability Zones opens a small window with text on the right side of the page. Is it possible to achieve a similar feature on a leaflet map without using JavaScript? This functionality would be tri ...

Incorporating personalized CSS and JavaScript into Shopify

Currently, my project involves integrating vertical tabs into a Shopify page that is utilizing the 'Atlantic' theme. Since this particular theme doesn't come with vertical tabs built-in, I have incorporated external JS and CSS files known as ...

Tips for avoiding repetitive animations when using jQuery animate toggle

Check out my Pen here. I'm having an issue with a share button that expands on hover. Everything works fine, except if you hover over it multiple times, the animation repeats as many times as you've hovered. Is there a way to prevent this from h ...

Since updating my background-image, the header images have mysteriously vanished

Currently, I am in the process of building a webpage and want to create a default navigation bar that will be consistent across all pages. Below is the code snippet from the file where I wish to include my navigation bar: <html> <head> < ...

What is the best method to align subtitles in an HTML5 video player? Tips for enhancing the style of subtitles/captions

I am facing an issue where the subtitles in my video do not play in the middle-bottom as desired. I have tried to style them using CSS but they just won't align to the center. The subtitles always appear at the bottom left of the video. Here is the H ...

The <h:outputStylesheet> tag fails to display any content on the HTML page

I am having trouble getting my CSS sheet to load while using JSF2 and PrimeFaces. The CSS file is located at WebContent/resources/css/style.css Here is the xhtml code: <h:head></h:head> <h:body> <h:outputStylesheet name="css/styles. ...

Should input fields be placed inside separate columns or share a column in Bootstrap to keep them together while being right justified?

When using Bootstrap, is it better to place separate input fields inside their own col or share a col to keep them together and right-justified? I have a combo and input that I want to display on the same row at the right-hand side of the screen. If I put ...

Emphasize a specific line of text within a <div> with a highlighting effect

I'm looking to achieve a similar effect as demonstrated in this fiddle As per StackOverflow guidelines, I understand that when linking to jsfiddle.net, it's required to provide some code. Below is the main function from the mentioned link, but f ...

Adjust the height of the svg

My page contains an SVG that is impacting the height of its container and causing other elements to move. Is there a way to adjust the height of the SVG? Currently, the SVG is a square but I would prefer it to be a rectangle as the height is too large. &l ...

Attempting to utilize Bootstrap for containing text inside of a background

<div class="container"> <div class="row"> <div class="col-lg-9"> <h2 style="font-family:times-new-roman">BIOGRAPHY</h2> <div id="demo" class="collapse"> <p> Isaac Newton (4 January 1643 – 31 March 1727) w ...

Align content to the center column with a combination of right-alignment and left-al

This question poses an interesting challenge, but I will do my best to articulate it: I have a central column of content and I am looking to incorporate columns on the left and right of it that seamlessly "hug" the middle column. How can I consistently ce ...

What is preventing me from creating a table with a filter option?

I've been attempting to create a filterable table similar to this example. I followed the code from the w3schools website, but unfortunately, it does not seem to be working for me. When I type in text, nothing gets filtered. <div id="users-tab ...