可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I need to modify bootstrap.css
to fit my website. I feel it\'s better to create a separate custom.css
file instead of modifying bootstrap.css
directly, one reason being that should bootstrap.css
get an update, I\'ll suffer trying to re-include all my modifications. I\'ll sacrifice some load time for these styles, but it\'s negligible for the few styles I\'m overriding.
Hw do I override bootstrap.css
so that I remove the style of an anchor/class? For example, if I want to remove all the styling rules for legend
:
legend {
display: block;
width: 100%;
padding: 0;
margin-bottom: 20px;
font-size: 21px;
line-height: inherit;
color: #333333;
border: 0;
border-bottom: 1px solid #e5e5e5;
}
I can just delete all that in bootstrap.css
, but if my understanding about best practices on overriding CSS is correct, what should I do instead?
To be clear, I want to remove all those styles of legend and use parent\'s CSS values. So combining Pranav\'s answer, will I be doing the following?
legend {
display: inherit !important;
width: inherit !important;
padding: inherit !important;
margin-bottom: inherit !important;
font-size: inherit !important;
line-height: inherit !important;
color: inherit !important;
border: inherit !important;
border-bottom: inherit !important;
}
(I was hoping there\'s a way to do something like the following:)
legend {
clear: all;
}
回答1:
Using !important
is not a good option, as you will most likely want to override your own styles in the future. That leaves us with CSS priorities.
Basically, every selector has its own numerical \'weight\':
- 100 points for IDs
- 10 points for classes and pseudo-classes
- 1 point for tag selectors and pseudo-elements
Among two selector styles browser will always choose the one with more weight. Order of your stylesheets only matters when priorities are even - that\'s why it is not easy to override Bootstrap.
Your option is to inspect Bootstrap sources, find out how exactly some specific style is defined, and copy that selector so your element has equal priority. But we kinda loose all Bootstrap sweetness in the process.
The easiest way to overcome this is to assign additional arbitrary ID to one of the root elements on your page, like this: <body id=\"bootstrap-overrides\">
This way, you can just prefix any CSS selector with your ID, instantly adding 100 points of weight to the element, and overriding Bootstrap definitions:
/* Example selector defined in Bootstrap */
.jumbotron h1 { /* 10+1=11 priority scores */
line-height: 1;
color: inherit;
}
/* Your initial take at styling */
h1 { /* 1 priority score, not enough to override Bootstrap jumbotron definition */
line-height: 1;
color: inherit;
}
/* New way of prioritization */
#bootstrap-overrides h1 { /* 100+1=101 priority score, yay! */
line-height: 1;
color: inherit;
}
回答2:
In the head section of your html place your custom.css below bootstrap.css.
<link href=\"bootstrap.min.css\" rel=\"stylesheet\">
<link href=\"custom.css\" rel=\"stylesheet\">
Then in custom.css you have to use the exact same selector for the element you want to override. In the case of legend
it just stays legend
in your custom.css because bootstrap hasn\'t got any selectors more specific.
legend {
display: inline;
width: auto;
padding: 0;
margin: 0;
font-size: medium;
line-height: normal;
color: #000000;
border: 0;
border-bottom: none;
}
But in case of h1
for example you have to take care of the more specific selectors like .jumbotron h1
because
h1 {
line-height: 2;
color: #f00;
}
will not override
.jumbotron h1,
.jumbotron .h1 {
line-height: 1;
color: inherit;
}
Here is a helpfull explantion of specificity of css selectors which you need to understand to know exactly which style rules will apply to an element.
http://css-tricks.com/specifics-on-css-specificity/
Everything else is just a matter of copy/paste and edit styles.
回答3:
It should not effect the load time much since you are overriding parts of the base stylesheet.
Here are some best practices I personally follow:
- Always load custom CSS after the base CSS file (not responsive).
- Avoid using
!important
if possible. That can override some important styles from the base CSS files.
- Always load bootstrap-responsive.css after custom.css if you don\'t want to lose media queries. - MUST FOLLOW
- Prefer modifying required properties (not all).
回答4:
Link your custom.css file as the last entry below the bootstrap.css. Custom.css style definitions will override bootstrap.css
Html
<link href=\"css/bootstrap.min.css\" rel=\"stylesheet\">
<link href=\"css/custom.css\" rel=\"stylesheet\">
Copy all style definitions of legend in custom.css and make changes in it (like margin-bottom:5px; -- This will overrider margin-bottom:20px; )
回答5:
If you are planning to make any rather big changes, it might be a good idea to make them directly in bootstrap itself and rebuild it. Then, you could reduce the amount of data loaded.
Please refer to Bootstrap on GitHub for the build guide.
回答6:
A bit late but what I did is I added a class to the root div
then extends every bootstrap elements in my custom stylesheet:
.overrides .list-group-item {
border-radius: 0px;
}
.overrides .some-elements-from-bootstrap {
/* styles here */
}
<div class=\"container-fluid overrides\">
<div class=\"row\">
<div class=\"col-sm-4\" style=\"background-color: red\">
<ul class=\"list-group\">
<li class=\"list-group-item\"><a href=\"#\">Hey</a></li>
<li class=\"list-group-item\"><a href=\"#\">I was doing</a></li>
<li class=\"list-group-item\"><a href=\"#\">Just fine</a></li>
<li class=\"list-group-item\"><a href=\"#\">Until I met you</a></li>
<li class=\"list-group-item\"><a href=\"#\">I drink too much</a></li>
<li class=\"list-group-item\"><a href=\"#\">And that\'s an issue</a></li>
<li class=\"list-group-item\"><a href=\"#\">But I\'m okay</a></li>
</ul>
</div>
<div class=\"col-sm-8\" style=\"background-color: blue\">
right
</div>
</div>
</div>
回答7:
To reset the styles defined for legend
in bootstrap, you can do following in your css file:
legend {
all: unset;
}
Ref: https://css-tricks.com/almanac/properties/a/all/
The all property in CSS resets all of the selected element\'s
properties, except the direction and unicode-bidi properties that
control text direction.
Possible values are: initial
, inherit
& unset
.
Side note: clear
property is used in relation with float
(https://css-tricks.com/almanac/properties/c/clear/)
回答8:
I found out that (bootrap 4) putting your onw css behind bootstrap.css and .js is the best solution.
Find the item you want to change (inspect element) and use the exact same declaration then it wil override.
It took me some little time to figure this out.
回答9:
Give ID to legend and apply css. Like add id hello to legend() the css is as follw:
#legend legend {
display: block;
width: 100%;
padding: 0;
margin-bottom: 20px;
font-size: 21px;
line-height: inherit;
color: #333333;
border: 0;
border-bottom: 1px solid #e5e5e5;
}
回答10:
Use jquery css instead of css . . . jquery have priority than bootstrap css...
e.g
$(document).ready(function(){
$(\".mnu\").css({\"color\" : \"#CCFF00\" , \"font-size\": \"16px\" , \"text-decoration\" : \"overline\"});
);
instead of
.mnu
{
font-family:myfnt;
font-size:20px;
color:#006699;
}