How can I override Bootstrap CSS of an a element?
You need the !important
keyword to override the style.
/* Here's styles.css: */
a { text-decoration: none; color: black !important;}
header { text-align: center; color: black;}
<!DOCTYPE html><html lang="en">
<head> <meta charset="utf-8" /> <title>Blog</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> <link rel="stylesheet" href="css/styles.css" /></head>
<body class="container"> <header> <h5>Blog</h5> <a href="#">Home</a> <a href="blog.html">Blog</a> <!--blog.html is the same as this one which is home.html--> </header></body>
</html>
Overriding Bootstrap CSS
No need of custom style, use built-in class : https://getbootstrap.com/docs/4.1/utilities/borders/
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="card card-sm border-0" id="localcard">
<p> See, no Border! </p>
</div>
Override Bootstrap CSS with custom CSS
Don't add important. That is a bad habit to fall into and will cause you even more headaches in the future. Plus, I doubt it will solve the problem. Instead, try to find out why your targeting (table .table.table-responsive .table-middle) isn't targeting and overriding the table you want.
The easiest way to do this is via Chrome or Firefox's "Inspect Element". Take a look at the table you want to change. See where the margin-bottom lies. Maybe it's on .table.blue
or .container .table
or something.
However, judging by your targeting, I doubt that is an issue. Instead, I believe you aren't targeting the element you want.
table .table.table-responsive .table-middle
will look for all <table>
elements, then look for children of that <table>
element with the classname of table
AND table-responsive
, and then look inside that element for children with the classname of table-middle
.
Your HTML would have to look like this:
<table>
<??? class='table table-responsive>
<??? class="table-middle">
Instead, I'm guessing you have a <table>
element that looks something like this:
<table class="table table-responsive table-middle">
Simply writing table.table
or table.table-responsive
should override bootstrap. Worst comes to worst, .table.table-responsive.table-middle
will almost certainly work.
Remember
.classname .another-classname
with a space goes parent -> child
.classname.another-classname
without a space is one element that has both of those classes.
External stylesheet not overriding bootstrap
You need to look at CSS Specificity rules. Just because you define a rule doesn't automatically mean it overrides a rule declared earlier.
Specificity is calculated on 4 factors abcd
:
a (1 if inline, 0 if in a file),
b number of IDs in CSS rule
c number of attribute selectors, classes and pseudo-classes,
d number of element names and pseudo-elements.
The rule with the largest 4 digit number abcd
wins and overrides lesser declarations, so inline declarations will override anything declared in an included CSS file.
Other CSS Specificity articles
- CSS Tricks
- Specificity calculator
Possible Solutions
It's generally not a good idea to change 'base' styles or to override Bootstrap styles directly.
You can override other declarations by adding
!important
to your rules, but this is generally regarded as a poor solution.Add your own class and/or id to elements which you wish to customise.
If you wish to have a quick effect, then you can add your own specific
id
orclass
to thebody
element and then declare your rules relying on descendents of thatid
orclass
. Using anid
in this way will give your rules a specificity of01xx
in a file, which will probably be enough to override Bootstrap rules.
e.g
CSS in file:
#mypage ul {
list-style-type:none;
}
HTML:
<body id="mypage">
...
<ul>
</ul>
</ul>
Can't override bootstrap style with my own stylesheet
If you make your CSS selectors more specific, there is a level of specificity that determines which styles get applied. So doing:
li.menuitem { color: black; }
Makes it more specific. Also adding !important indicates that this style would have priority:
.menuitem { color: black !important; }
Careful with nested CSS hierarchies and using !important. It could cause issues if you want to override those changes in a different context. Only use when necessary, but it's OK to use.
custom css cannot override bootstrap css
I think the problem is that your css file is properly linked or not linked at all to your HTML page. You should check that first
Related Topics
How to Style a ≪Select≫ Dropdown With Only Css
How to Change Input Type="Date" Format
How to Create an HTML Button That Acts Like a Link
How to Style the Option of an HTML "Select" Element
How to Vertically Align Elements in a Div
How to Center Text (Horizontally and Vertically) Inside a Div Block
How to Center a "Position: Absolute" Element
When Flexbox Items Wrap in Column Mode, Container Does Not Grow Its Width
How to Stop Background from Jumping to the Top on Modal Toggle
Css - How to Have Swiper Slider Arrows Outside of Slider That Takes Up 12 Column Row
One Flex/Grid Item Sets the Size Limit For Siblings
Flex/Grid Layouts Not Working on Button or Fieldset Elements
Font-Size and Img Height Too Big At Website Through Smartphone Oneplus 3T
How to Make Transparent Color White Instead of Black in Html2Canvas
How to Prevent Flex-Items from Overflowing Flex Parent With No Wrap