The CSS override in Bootstrap 4 does not take effect unless it is placed within the <head> section of the HTML

After browsing through advice given on this Stack Overflow thread, I attempted to implement my own CSS solution:

#bootstrap-override .carousel.slide {
          max-width: 1920px;
          min-width: 900px;
          overflow: hidden;
          margin-left: auto;
          margin-right: auto;
        }

    #bootstrap-override .carousel-inner {
          width: 1920px;
          left: 50%;
          margin-left: -960px;
        }
    

Unfortunately, this approach was ineffective. Bootstrap 4's default styles prevailed for reasons unknown. Here is a snippet of the Bootstrap CSS in question:

.carousel {
      position: relative;
    }

    .carousel-inner {
      position: relative;
      width: 100%;
      overflow: hidden;
    }

    .carousel-inner::after {
      display: block;
      clear: both;
      content: "";
    }
    // The .slide class doesn't seem to be explicitly defined anywhere.
    

I placed my CSS declarations after Bootstrap's link in the head section of my HTML file:

<head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto&family=Roboto+Condensed&display=swap">
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
      <link rel="stylesheet" href="./css/index.css">
      <title>...</title>
    </head>
    

While inserting my overriding styles directly into the <head> area resolved the issue, I remain puzzled as to why specifying an id and class in my CSS didn't produce the desired result. Despite my flawed understanding of cascading styles in this context, shouldn't my CSS have taken precedence over Bootstrap's? Does anyone have insights into what kind of style supremacy Bootstrap employs?

Update: Below is the relevant HTML structure for reference:

<body id="bootstrap-override">
        <div id="carousel" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators">
                <li data-target="#carousel" data-slide-to="0" class="active"></li>
                <li data-target="#carousel" data-slide-to="1"></li>
                <li data-target="#carousel" data-slide-to="2"></li>
            </ol>
            <a class="carousel-control-prev" href="#carousel" role="button" data-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="carousel-control-next" href="#carousel" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
            <div class="carousel-inner" style="height: 100%;"">

                <div class=" carousel-item active">
                    <img class="d-block" src="./res/image.png" alt="First slide">
                    <div class="carousel-caption d-none d-md-block">
                        <table style="margin-left:auto; margin-right:auto;">
                            <tr>
                                <td class="align-middle">
                                    <img src="./res/logo.png" alt="...">
                                </td>
                                <td class="align-middle">
                                    <h3>...</h3>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

                <div class="carousel-item">Slide #2...</div>
                <div class="carousel-item">Slide #3...</div>

            </div>
        </div>
    </body>
    

Answer №1

Could you take a look at the following possibilities:

  • Have you checked if the linked CSS file is actually working? You can try giving the body a background color to test it.
  • If the CSS class is properly linked, have you ensured that the naming of the classes and IDs are correct?
  • If everything is set up correctly, I can provide an example of customizing Bootstrap's carousel CSS. If this doesn't help, please provide more details such as sample HTML tags for your carousel.

Thank you.

JSFiddle link for reference

Custom CSS

#carouselExampleControls {
  height: 100px;
  text-align: right;
  background: gray;
}

.img-fluid {
  max-height: 100px;
}

HTML

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <svg class="bd-placeholder-img bd-placeholder-img-lg d-block w-100" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: First slide"><title>Placeholder</title><rect fill="#777"></rect><text x="50%" y="35%" fill="#555" dy=".3em">First slide</text></svg>
    </div>
    <div class="carousel-item">
      <img class="img-fluid" src="https://thumbs.dreamstime.com/b/dead-tree-tall-portrait-view-blue-sky-background-57002950.jpg" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="img-fluid" src="https://images.pexels.com/photos/414171/pexels-photo-414171.jpeg" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

https://jsfiddle.net/chris495b/e0jzumnc/3/

If your files are properly linked, they should show in the devtools like the screenshot below. Could you please share that as well? It would greatly assist in debugging any issues!

https://i.sstatic.net/boFAx.png

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

The functionality of changing the checkbox to "checked" by clicking on the span is not

How can I create a toggle button with a checkbox using css and jquery? Clicking on the span representing the toggle button should change the checked property of the checkbox. Currently, the span does not change the property, even though it triggers the c ...

Guide on incorporating clickable hyperlinks in Foundation tooltip

Currently, I am facing an issue with a header tag that has Foundation's tooltip attached to it as per the documentation. The problem is, I want the tooltip to be clickable so that I can display a modal on click, but every time I move away from the div ...

Issue encountered in SQL PHP form script at line 23

I'm currently working on some code to insert data into different tables, but there seems to be an issue on line 23. This code is part of a college project registration form. <?php $con = mysql_connect(","",""); if (!$con) { die('Could not c ...

How can one extract the input value from a Bootstrap modal field and use jQuery to display it in another input field within the same modal?

Here is the HTML code I am working with: <div class="modal fade" id="modalTambahDataTransaksiZakat"> <div class="vertical-alignment-helper"> <div class="modal-dialog vertical-align-center"> <div class="modal-content"> ...

Prevent horizontal HTML scrolling while displaying a layer

I am currently working with a .layer div element that darkens the page to highlight a modal. However, I have encountered an issue where upon triggering the event, the div does not occupy 100% of the screen and the original browser scroll bar disappears. I ...

Guide to making a dynamic clip mask with flowing trails on a digital canvas

I have successfully coded a dynamic path with trails similar to the one shown on However, when I try to replace ctx.fill() with ctx.clip(), nothing appears on the canvas. Here is the JavaScript code snippet I'm using – for the full code, visit http ...

Aligning inline form controls with Bootstrap and Syncfusion widgets

I'm facing a challenge aligning the Syncfusion JavaScript widget - a dropdownlist - to be displayed inline with the label. I am using Bootstrap 3. I haven't been successful in achieving a nice alignment, meaning getting the middle of the label p ...

Isotope animation glitches causing malfunction

I've been experimenting with CSS3 transitions on isotope items, but I'm encountering some strange behavior. My goal is to achieve a fading effect on the items, similar to this example: . Thank you in advance! Here's what I have so far: http ...

The problem of unordered list content being broken when using multi-column CSS

I've encountered an issue with my CSS where multiple ul sections are automatically split into three columns, but the li content is breaking and moving to the next column individually. I want the entire ul to move to the next column as a whole. Here&ap ...

The functionality of passing an ID in Bootstrap Modal is not functioning as expected

I'm facing an issue with passing an ID to my modal. My goal is to have the details of an event displayed in the modal when the corresponding link (which represents the event) is clicked. However, I'm struggling to retrieve the ID and show the ev ...

Fluid layout causing elements in body to be hidden by fixed header

I'm currently tackling a fluid layout challenge, utilizing percentages instead of pixels to achieve it. I've managed to create a fixed heading (#heading) at the top of the page, but now I'm struggling with ensuring that all other elements on ...

The jQuery .accordion() feature is not functioning properly due to the failure to load jQuery into the HTML document

I have exhaustively researched online and visited numerous resources, including Stack Overflow. Despite making countless adjustments to my code, it still refuses to function properly. The frustration is mounting as I struggle with jQuery - a technology tha ...

Having trouble accessing property '0' of undefined in angular

Below is the code I am currently working on. My goal is to enable editing of the table upon click, but I encountered the error mentioned below. Could someone kindly explain why this is happening and suggest a workaround? <tbody> <tr *ngFor="l ...

Steps to store user input into an array and subsequently combine the stored input:

I am currently working on a form that consists of two text boxes: Task and Description. My goal is to be able to log the input from both boxes and save it via a submit button. For example: Task: do laundry Description: do a buttload of laundry (idk lol) I ...

What methods do publications use to manage HTML5 banner advertisements?

We are working on creating animated ads with 4 distinct frames for online magazines. The magazines have strict size limits - one is 40k and the other is 50k. However, when I made an animated GIF in Photoshop under the size limit, the image quality suffered ...

Differences in the way CSS handles 'root' within an external .css file

As I delve into the world of CSS, I've encountered a puzzling issue that has me scratching my head. My objective is to change the background color of the viewport to red. When I tried using the following code in an external .css file and linked it to ...

What is causing my `<nav>` buttons to be left-aligned?

The attempt to center the buttons in the viewport using text-align:center for nav, ul, and li has proved ineffective. The alternative of nav {text-align: left;} was no more successful. CSS Snippets: #container { width: 100%; background-color: bla ...

Looking to create three rectangles on canvas that expand vertically when clicked by the mouse

My current project involves creating three growing rectangles in HTML5's Canvas tags. Each time I click the canvas with my mouse, I expect the rectangles to increase in height vertically. Although I have set up my code to draw the squares and impleme ...

Replica of Spotify - Bottom Section - HTML and CSS Styling

I have been attempting to replicate the Spotify interface using only HTML and CSS. While I have successfully created the player section, I am struggling to center the player and incorporate the other two details as shown in the attached images. The 'G ...

Is there a way to align the Title in the Titlebar to the center?

Hi there! I could use some assistance :) I have an Electron app and I'm working on the title bar. I would like to center the title, but I'm not sure how to do it. Can anyone provide guidance on this? Also, since I am a beginner, I'm unsure i ...