Explain Codes LogoExplain Codes Logo

How can I override Bootstrap CSS styles?

html
css-specificity
css-selectors
responsive-design
Nikita BarsukovbyNikita Barsukov·Jan 19, 2025
TLDR
.custom-btn { background-color: red !important; /* Boots off Bootstrap */ }

Up the specificity or utilize the power of !important to boot off Bootstrap styles. Don't forget: your custom CSS should wave after Bootstrap's parade in the HTML, that is, link after it. When eyeing component-specific styles, bring into play unique IDs or nest selectors like a bird for higher priority.

Mastering specificity

When it comes to overriding Bootstrap, adorned your code with the crown of CSS specificity. Got a unique ID or a class selector? Great! Increase your CSS specificity by nesting them and watch your rule claim the throne, overriding Bootstrap's reign.

Your custom CSS file should be cued after bootstrap.css in your HTML, nudging it to the front of the specificity line when the browser renders stylesheets.

Yet, beware the sorcerer's apprentice syndrome: !important wields great power but when misused, un-calms the cascading seas of style.

Dialing up specificity

  • Opt for class-based selectors, their specificity score outplays Bootstrap's.
  • Occasionally, play your ID card to give an extra boost to your specificity score.
  • Build a nest of selectors, domiciling within parent elements for a higher score.

Setting the stage with stylesheet order and specificity boost

  • Like setting up the stage for a grand play, arrange your stylesheets. Let bootstrap.css take the first bow, then roll in your custom.css.
  • Within your custom.css, pen selectors specific enough to outscore Bootstrap's rules using nested selectors or prefixed selectors with classes or IDs.

Using inherit tastefully

  • In the feast of CSS, use the keyword inherit to swim back to the parent's styles, cleaning up after Bootstrap's feast.
  • Treat this keyword as your secret sauce, useful in taking a step back to the default state after Bootstrap has had its meal.

Avoiding mishaps on the road to override

  • A golden rule: Avoid modifying bootstrap.css. Updates can be catastrophic, trust me on this one.
  • Refrain from !important, as it can disrupt the peaceful harmony of cascading rules.
  • Inline styles, though heavyweights in CSS specificity, ruin scalability and maintainability. Use them sparingly and wisely!

Best practices to navigate the override

  • Keep your CSS stylishly modular, isolating Bootstrap overrides in your custom.css.
  • Copy and paste Bootstrap selectors into your custom.css and tweak them as needed, not touching the sacred bootstrap.css.
  • Your CSS should read like a book, with explicit targets. Why beat around the bush?

Code inspection: The magnifying glass of overrides

  • Who needs Sherlock when you have browser inspector tools? Use them to see which Bootstrap rules are being applied, and craft specific overrides.
  • Combing through Bootstrap's source code can be eye-opening, offering precious insights for targeting strategic overrides.

Asserting your code's weight

  • When selectors hold the same score, it's the early bird that gets scrapped. Ensure your custom.css is placed after bootstrap.css.
  • Weaving an intricate and specific selector proves to be your ace in the hole, ensuring Bootstrap bows to your style rule.