How to chose which CSS rule to use from multiple style sheets - html

I have bootstrap.css and login.css attached to index.php. Within index.php there are several form elements such as input type['text']. However, both attached CSS files target input type['text']. I have bootstrap.css linked above my other css file:
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/login.css">
How can I specify that I want input type['text'] to use the rules specified within login.css rather that using the rules from bootstrap.css? I generally require bootstrap.css more, but do with to now and then, implement my own rules which I cannot since the rules from bootstrap.css are rendered first since it is listed first.

The more specific you define your css, the more priority it gets in CSS.
You need to define your custom rules more relevant than the bootstrap ones.
Example:
p.test {
color: green;
}
.test {
color: blue;
}
<p class="test"> teststring </p>
You see that p.test is stronger than .test though .test comes after p.test.
hope that helped.
if your remove the p from p.div you will see the color gets blue, because the order is only relevant if both rules are equal in specificty.
.test {
color: green;
}
.test {
color: blue;
}
<p class="test"> teststring </p>

well, there is something like "CSS Rule Hirarchie" in you can use for your disired effekt. In short: The most "upper" CSS Rule wins the Game.
If you Develop with Chrome or FF you can Debug anything with the Developer Tools (i love the chrome one in this case) and look why it's getting overwritten by Bootstrap. Mostly it's a CSS Level Rule or somthing like this.
I would suggest you using your own Class on the Wrapper Element and override the styles as you wish. Give a look on my Example below to get an Idea how I mean it.
Sidenote: I think this Tutorial from CSS-Tricks describes well whats going on with the Rules of CSS and how the most specific rule comes in to the game.
/* Just a Basic Styling */
ul { list-style:none; }
a { text-decoration: none; font-family: Verdana; color:black; }
/* Lets make all list elements Blue. This should be familiar for you,
* this is the standard overwriting of css classes (is this called so?).
*/
.list li a { color: blue; }
/* Look at this Example now, we specify the 3rd list Element.
* As this rule is more specified as the rule from line 9,
* this rule takes effect on the 3rd line, the other ones will stay blue.
* +
* As a proof that you can't "override" this rule, I have put one
* line below to show you, that even another low lvl rule can't override it.
* Line 20 is still more specified then Line 21, even it is comes after it.
*/
.list li:nth-child(3) a { color:pink; }
.list li a { color:blue; }
/* Another Example of higher CSS Hirarchie would be this line.
* We go one DOM Element even higher, and guess what:
* If you comment out this line here, it will take effect.
*/
/* .wrapper li:nth-child(3) a { color:seagreen; } */
<div class='wrapper'>
<ul class='list'>
<li><a href='#'> Magic Link </a></li>
<li><a href='#'> Another Link </a></li>
<li><a href='#'> Rule over the wrapper Link </a></li>
</ul>
</div>

Related

How to set a CSS selector for links ( a Tag ) for a Class

I search for all solutions but nothing help me.
my simple problem is to set a style for a link ( a Tag ) with a class:
<a class="logo"></a>
I don't want a general style for links or for active ones but for a selected Class.
Thank you.
I think you're looking for the CSS class selector.
To apply a style to just a single class you should prefix the class name with a dot (.) in your CSS selector.
In this particular case you would do it like this:
.logo {
/* Styles here */
}
You can also ensure that only link elements are affected by adding the element selector:
a.logo {
/* Styles here */
}
PS. The CSS id selector is # and it works in a similar manner.
There are three different ways to solute this. Since you do not want a global styling for a link this example will not be it:
a{
/* STYLE HERE */
}
Since you simply want to style a link with a surtain class use this example:
a.logo {
/* STYLE HERE */
}
or
logo {
/* STYLE HERE */
}
or
a[class="logo"] {
/* STYLE HERE */
}
The last example is a new way of making this happen, some very old browser wont understand this, so you better stick to the first or second example.
Use like this
<style>
a[class="logo"] {
background-color: yellow;
}
</style>
<a class="logo">test</a>
you can add style rules by targeting class :
a.logo { color: #aeaeae; }

CSS override / don't inherit

I'm trying to place a link in Wordpress quickly and we have a pretty complex style being applied to all a href links in the section. Here's a small sample of the selector and the styles within (there's about 40 lines of styles which I held back)
div.content-rotator li.featured-content a {
margin: 0px;
border: 1px solid rgb(34,56,19);
}
Is there anyway I can place a link in this li and override the parent style? It has to appear within the li with class featured-content.
I don't want to touch the existing CSS at this stage so I'd prefer to implement inline styles on the a element.
Thanks
EDIT: Just in case it wasn't clear, the CSS above is coming from the style sheet and I'd like to zero it out.
There's > 50 lines of styles in this though, I've only shown two for brevity so inline replacing them all isn't really an option.
Just use inline styles or/and add !important to overriden CSS definition, like:
<div class="content-rotator">
<ul>
<li class="featured-content">
...
</li>
</ul>
</div>
or
div.content-rotator li.featured-content a.other {
margin: 3px !important;
border: none !important;
}
Give the selected link an ID and just add !important to the styles. I don't think there is a better alternative unless you plan to go through the entire stylesheet.

How can I override Bootstrap CSS styles?

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;
}
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
Note: If the element has inline styling that automatically wins (1000 points)
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;
}
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.
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).
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; )
Update 2021 - Bootstrap 4 and Bootstrap 5
There are 3 rules to follow when overriding Bootstrap CSS..
import/include bootstrap.css before your CSS rules (overrides)
use more CSS Specificity (or equal) than the Bootstrap CSS selectors
if any rule is overridden, use !important attribute to force your rules. If you follow rules 1 & 2 this shouldn't be necessary except for when using Bootstrap utility classes which often contain !important as explained here
Yes, overrides should be put in a separate styles.css (or custom.css) file so that the bootstrap.css remains unmodified. This makes it easier to upgrade the Bootstrap version without impacting the overrides. The reference to the styles.css follows after the bootstrap.css for the overrides to work.
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
Just add whatever changes are needed in the custom CSS. For example:
legend {
display: block;
width: inherit;
padding: 0;
margin-bottom: 0;
font-size: inherit;
line-height: inherit;
color: inherit;
white-space: initial;
}
Note: It's not a good practice to use !important in the override CSS, unless
you're overriding one of the Bootstrap Utility
classes. CSS
specificity
always works for one CSS class to override another. Just make sure you use a CSS selector that is that same as, or more specific than the bootstrap.css
For example, consider the Bootstrap 4 dark Navbar link color. Here's the bootstrap.css...
.navbar-dark .navbar-nav .nav-link {
color: rgba(255,255,255,.5);
}
So, to override the Navbar link color, you can use the same selector, or a more specific selector such as:
#mynavbar .navbar-nav .nav-link {
color: #ffcc00;
}
For example: https://codeply.com/p/FyQapHImHg
When the CSS selectors are the same, the last one takes precedence, which it why the styles.css should follow the bootstrap.css.
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/)
See https://bootstrap.themes.guide/how-to-customize-bootstrap.html
For simple CSS Overrides, you can add a custom.css below the bootstrap.css
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/custom.css">
For more extensive changes, SASS is the recommended method.
create your own custom.scss
import Bootstrap after the changes in custom.scss
For example, let’s change the body background-color to light-gray #eeeeee, and change the blue primary contextual color to Bootstrap's $purple variable...
/* custom.scss */
/* import the necessary Bootstrap files */
#import "bootstrap/functions";
#import "bootstrap/variables";
/* -------begin customization-------- */
/* simply assign the value */
$body-bg: #eeeeee;
/* or, use an existing variable */
$theme-colors: (
primary: $purple
);
/* -------end customization-------- */
/* finally, import Bootstrap to set the changes! */
#import "bootstrap";
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">Hey</li>
<li class="list-group-item">I was doing</li>
<li class="list-group-item">Just fine</li>
<li class="list-group-item">Until I met you</li>
<li class="list-group-item">I drink too much</li>
<li class="list-group-item">And that's an issue</li>
<li class="list-group-item">But I'm okay</li>
</ul>
</div>
<div class="col-sm-8" style="background-color: blue">
right
</div>
</div>
</div>
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.
I found out that (bootstrap 4) putting your own 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 will override.
It took me some little time to figure this out.
for ruby on rails users--
in your application css file, make sure the bootstrap file is mentioned first then the custom css stylesheet. This way the latter CSS code that you wrote overwrites the former. Use !important if needed as well
*= require 'bootstrap.min'
*= require_self
*= require 'name_of_your_stylesheet'
Inspect the target button on the console.
Go to elements tab then hover over the code and be sure to find the default id
or class used by the bootstrap.
Use jQuery/javascript to overwrite the style/text by calling the function.
See this example:
$(document).ready(function(){
$(".dropdown-toggle").css({
"color": "#212529",
"background-color": "#ffc107",
"border-color": "#ffc107"
});
$(".multiselect-selected-text").text('Select Tags');
});
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;
}
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;
}

Is it possible to select all elements which do not have any CSS class?

The W3C Recommendations defines the ability to select any anchor which has a defined class by using A[CLASS]{/*Code*/}, but is there an antecedent?
Can I select all anchors that do NOT have a class?
My instinct is to use A[~CLASS]{/*Code*/}, but that isn't documented, and A[CLASS=]{/*Code*/} selects all anchors which have a class name which is an empty string (for instance, <A CLASS="" HREF="http://example.com/">This anchor has a class, but it's empty.</A>).
Example usage
In this code, I only want classless links to be green.
<HTML>
<HEAD>
<LINK REL="stylesheet" TYPE="text/css" HREF="http://example.com/externalUneditableStyles.CSS" />
<STYLE>
A.deadLink{
color:#FF0000 !important;
}
A{
color:#00FF00;
}
</STYLE>
</HEAD>
<BODY>
This link is red, as specified by the CSS above.
This link is green, as specified by the CSS above.
This is a link to a child page, and is styled by external uneditable CSS. That CSS doesn't specify a color, so it is green, even though that might not be the intended effect.
</BODY>
</HTML>
Use something like this:
a:not([class]) {
/*some styling here*/
}
Little demo: little link.
That's exactly what cascading is for, if you do:
a { color: #000; } /* this targets all anchors */
Then after this in your stylesheet you can do
a.classname { color: #ffcc00; } /* to override the color for the once with the classname class defined. */
I hope that's clarifies your question.
Try to play with the .foo:not(.bar)-pseudo selector. I also advise you not to code tags in caps. I believe it is allowed in HTML5, but not in XHTML. Even so, the practice is frowned upon.

how can i make different html links different colors . .

I am generating a html file with many different links and they (by default) all show up the regular blue color. Is there anyway i can make certain links different colors. note that this html is getting pushed into outlook as an email so i can't have separate css files.
You can put your css in the <head/> of the <html>. Style your links with the color(s) you want. If you need more than one type of link, use classes. e.g.
a { color: #abcde1}
a.visited, a.hover {color: #1abcde;}
a.special {color:#123456;}
use a css style for the anchor inline:
<a href="foo" style="color:orange"....
There are a couple different ways. CSS can appear in your head tag, so it doesn't have to be a separate style sheet.
One is to use the style attribute:
<a style="color:blue;">...</a>
Another is to use css classes:
<style>
.navLink { color: blue; }
</style>
<a class="navLink">...</a>
There are lots of options. See http://www.echoecho.com/csslinks.htm
You can use classes for your styles:
your CSS file:
.redlinks a
{
color: #FF0000;
}
.greenlinks a
{
color: #00FF00;
}
your HTML file:
<div class="redlinks">my link</div>
you could use internal style sheet or inline styles
assuming the email client doesn't ignore them. of course by messing with link colours you run the risk of users not realising that you have links at all, so be careful.
This is example use of different colours for links:
<style type="text/css">
a.yellowLink { color:#ff0; }
a.greenLink { color:#0f0; }
a.redBlueLink { color:#f00; }
a.redBlueLink:hover { color:#00f; }
</style>
I’m yellow
I’m green
I’m red and blue on hover