How can I prevent Bootstrap from overriding my custom CSS styles?

My application depends on Bootstrap. I am trying to customize the default Bootstrap styles by loading Bootstrap first followed by my own custom CSS. However, the final appearance is not what I expected. What could be causing this issue? Below is a minimal working example.

demo.css

html, body {
    background:#fbf3e8;
}

body {
    margin:0;
    padding:2em 5px;
}

@media (min-width: 640px) {
    body {
        padding:2em;
        font-size:112.5%;
    }

}

index.html

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>SmartMenus jQuery Website Menu - jQuery Plugin</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

<!-- jQuery -->
<script type="text/javascript" src="../libs/jquery/jquery.js"></script>

<!-- SmartMenus jQuery plugin -->
<script type="text/javascript" src="../jquery.smartmenus.js"></script>

<!-- SmartMenus jQuery init -->
<script type="text/javascript">
    $(function() {
        $('#main-menu').smartmenus({
            subMenusSubOffsetX: 1,
            subMenusSubOffsetY: -8
        });
    });
</script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- SmartMenus core CSS (required) -->
<link href="../css/sm-core-css.css" rel="stylesheet" type="text/css" />

<!-- "sm-blue" menu theme (optional, you can use your own CSS, too) -->
<link href="../css/sm-blue/sm-blue.css" rel="stylesheet" type="text/css" />

<!-- My custom CSS -->
<link href="../libs/demo-assets/demo.css" rel="stylesheet" type="text/css" />

</head>

<body>

<nav id="main-nav" class="navbar navbar-default navbar-fixed-top" role="navigation">
  <ul id="main-menu" class="sm sm-blue" data-smartmenus-id="14772210106692266">
    <li><a href="http://www.smartmenus.org/">Home</a></li>
    <li>
      <a href="http://www.smartmenus.org/about/" class="has-submenu" id="sm-14772210106692266-1" aria-haspopup="true" aria-controls="sm-14772210106692266-2" aria-expanded="false"><span class="sub-arrow">+</span>About</a>
      <ul id="sm-14772210106692266-2" role="group" aria-hidden="true" aria-labelledby="sm-14772210106692266-1" aria-expanded="false">
        // More HTML code...
      </ul>
    </li>
    // More menu items...
  </ul>
</nav>

</body>
</html>

If I remove the line #24 loading Bootstrap, the result matches Figure 1 (expected). But when Bootstrap is included, the actual output differs as shown in Figure 2.

Figure 1

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

Figure 2

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

To clarify, my goal is to have additional padding around the menu bar on all sides, however, with Bootstrap loaded there seems to be no padding either at the top or on the sides of the menu bar.

Answer №1

Here are a few important considerations to keep in mind:

  1. Ensure that the CSS selector matches the rule you want to override in the bootstrap file exactly. For example, if it's div > a > span, don't change it to div > span
  2. Check that the bootstrap file does not use the keyword !important
  3. Add a type="text/css" attribute to the bootstrap link
  4. Lastly, experiment with adding the keyword !important to see if it has the desired effect

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

Is there a keen eye out there that can help me pinpoint the mistake in this SVG/Javascript function?

I have created a series of SVG circles that are meant to alternate in color between blue and red with each click. However, I am experiencing some inconsistency in the behavior. In my local environment, the color doesn't change to red until the second ...

Altering the appearance of a button's style without sacrificing its 3D effect

In Firefox and Internet Explorer, disabled buttons appear greyed out and unclickable, while Chrome displays them as enabled. https://i.sstatic.net/67c1n.jpg Is there a way to adjust the styling of a button (such as its color) without losing the 3D effect ...

An overabundance of parallax visuals

I'm currently working on a static website that features several parallax images dividing each section. However, I've run into an issue as I continue to add more sections and parallax images. Some of the images at the bottom of the website are shi ...

The Django framework does not apply color to the Bootstrap Navbar as expected

I'm currently working on building an E-Commerce Website using Django. I have implemented a Bootstrap navbar, but the CSS styles that I obtained from this source are not functioning properly when placed within the {%block css %} tag. I have attached t ...

The visual glitch caused by a radial gradient on IOS is quite noticeable

https://i.sstatic.net/Q5U4u.png https://i.sstatic.net/miWXC.png The images depict a visual artifact created by the radial gradient, which I am utilizing as a mask to achieve an iOS-like effect. background:-webkit-radial-gradient(transparent 0%,transp ...

Achieving cross-browser compatibility with CSS and JavaScript across IE, Firefox, Chrome, and Safari

We are a company that specializes in developing ERP and CRM systems. Currently, our products only support Internet Explorer and Firefox. We are looking to add compatibility for Chrome, Safari, and Opera. Can anyone recommend comprehensive resources or ma ...

Getting access to the CSS class selector within the HTML document in the application

Upon examining this snippet of HTML: <html> ... <iframe> #document <html> ... <div className='change-me'></div> ... </html> </iframe> </html> This iframe has be ...

Using JavaScript to fully load a webpage, rather than just loading a specific div

As a JavaScript beginner, I've encountered what seems to be a simple issue. I am trying to dynamically change the content of a div based on which button is clicked. This code snippet functions properly in JSfiddle, but when I transfer it to my local m ...

Achieve a centered content with a stretched background using Bootstrap 5

Having some trouble with aligning items in a div using align-items-stretch to stretch the background and align-items-center to vertically center the content. It seems that the alignment issue occurs when there's a very long title present. https://i.s ...

Tips for resetting the form input fields in Vue.js after the user has successfully submitted the form

I am facing an issue with my registration page. After the user enters some values and successfully submits the form, I want to clear all the fields. To achieve this, I am using a predefined function called reset() inside the script section. However, the ...

Design a layout featuring an array of boxes in varied sizes

Is it possible to create a grid layout with 3 columns containing div boxes of varying heights, all populated with content extracted from a database? ...

What is the best way to ensure the hamburger menu stays perfectly centered?

My menu is currently aligned to the right and scrolls with the user profile. I want the menu to always be centered and the profile to stay on the right side. I am working with Angular 12 and Bootstrap 5 for this project. Here is the code I have been usin ...

Determine the width of invisible images using JavaScript/jQuery

Please take a look at the jsFiddle Every time I run it, I am trying to retrieve the width of the images, but the values I get are as follows (check in the JS console) 400 0 575 533 0 ... Some values are zero, and I can't figure out why. It seem ...

Troubleshooting: Issue encountered while adding jQuery Slideshow plugin to current website

I am currently facing some challenges with a specific feature on my website. I am trying to integrate Jon Raasch's "Simple jQuery Slideshow Plugin" into an existing site that was not originally built by me. However, when viewing the site in IE 9, two ...

Interactive JavaScript Slider for Customizing Selection

Recently, I came across a range slider and I am trying to make it more dynamic. The current JavaScript code has hardcoded references to specific HTML elements, and I want to have multiple sliders on my HTML page, each functioning independently. The code sn ...

What is the best way to prevent table cell borders from encroaching on the padding area of the cell?

When dealing with table cell elements that have borders, it's common for them to not respect the vertical height of the cell containing them. Sometimes a child element's borders may overlap the padding or border of its containing cell. To prevent ...

What is causing the image to move to the following line?

(https://i.stack.imgur.com/5oi7s.png) The image appears to be shifting to the next line inexplicably. I have experimented with various methods to address this issue, including utilizing the built-in Image component which seemed to show improvement. However ...

Tips for showing text on top of a responsive image using CSS

I am working on a layout with 4 columns, each containing images of different sizes followed by a hover effect with an absolutely positioned mask. The width of the mask is currently exceeding the width of the images. Is there a way to ensure that the mask ...

The content of the external json file needs to be allocated to corresponding html divs with matching names

As I am new to jQuery and JavaScript, I am on the search for a method to incorporate data from an external JSON file into HTML code. The data objects in JSON have the same names as their corresponding HTML divs. This should also occur during the onLoad eve ...

Influencing the movement of one div container using another dynamic div

Apologies for the unclear title. Check out a JsFiddle DEMO to see what I mean. My goal is to move the black container as soon as it touches the red container. I want to prevent the overlap between the two and adjust the position of the red container relat ...