How to make parent property not override descendants - html

I am using a css file I got online for a website I'm making, but I'm only embedding some components into the website. This css file uses global styles, and they are overriding all of the global styles on my website. I figured I would just wrap the global styles and make them descendants of a class, and then make that the parent class of my component. Here's an example of what I'm saying
h1 {
color: red;
}
.text {
color: blue;
}
But now all of my h1 tags on the page end up red. I decided to wrap all the global styles and make it so only the descendants of a certain class would be affected by that style. Here's what the new css looks like
.parent-class h1 {
color: red;
}
.text {
color: blue;
}
and make my html look something like this
<h1>This should not be affected by any css</h1>
<div class="parent-class">
<h1 class="text">Hello</h1>
<h1>How's it going</h1>
</div>
The first part actually works. My top h1 is not affected by the global css, when it was before.
But here's the problem I'm running into. Before, the text class was overriding the global h1 style, and my Hello ended up blue, while my How's it going was red. Now that h1 has been wrapped by a parent, Hello is also ending up red.
I know that the parent style appears first in the css file, so I don't think it's a question of what is being rendered first. Also, I know that everything is using classes, and not IDs, so that priority issue isn't occurring either.
I'm guessing that this is occurring because of the .parent-class h1 now has two rules while .text only has one. If that's the case, is there a way to mitigate this problem?
One thing I could do is just wrap the parent around the child, like .parent-class .text, but the css file I found online has close to 25,000 lines of code, while the global rules only had about 300, so that would be extremely time consuming, because there's thousands of classes I would need to alter.
Is there another way to fix this problem? If not, is there a way to wrap a parent rule around multiple blocks of code, in a way like this
.parent-class {
.text {
color:blue;
};
h1 {
color: red;
};
}
Or is that not possible?

Make sure your second selector is having the same (o higher) specifity by combining it with something else. You can for example add nth-child(n) which will not change the behavior of your selector but simply increase its specificity:
.parent-class h1 {
color: red;
}
.text:nth-child(n) {
color: blue;
}
<h1>This should not be affected by any css</h1>
<div class="parent-class">
<h1 class="text">Hello</h1>
<h1>How's it going</h1>
</div>
You can also duplicate the class:
.parent-class h1 {
color: red;
}
.text.text {
color: blue;
}
<h1>This should not be affected by any css</h1>
<div class="parent-class">
<h1 class="text">Hello</h1>
<h1>How's it going</h1>
</div>

If you need to update multiple stylesheet rules loaded from another source, you can use the CSSStyleSheet API to delete and insert rules:
const styleSheet = document.styleSheets[0]
const rules = Array.from(styleSheet.cssRules).map(r => r.cssText) // get the rules texts
rules.forEach(() => styleSheet.deleteRule(0)); // remove the rules from the stylesheet
rules.forEach(cssText => styleSheet.insertRule(`.parent-class ${cssText}`)) // generate new rules with the namespace
h1 {
color: red;
}
.text {
color: blue;
}
<h1>This should not be affected by any css</h1>
<div class="parent-class">
<h1 class="text">Hello</h1>
<h1>How's it going</h1>
</div>
If you just need to exclude a single case, you can use the :not() pseudo-class to disable for the h1 elements with the class .text:
.parent-class h1:not(.text) {
color: red;
}
.text {
color: blue;
}
<h1>This should not be affected by any css</h1>
<div class="parent-class">
<h1 class="text">Hello</h1>
<h1>How's it going</h1>
</div>

just use !important like this
h1 {color: red;}
.text {color: blue !important;}

Related

How to enforce css style over existing classes (specially in Bootrstap)

When I want to apply a certain style to a div (specially using bootstrap 3), I create my own class like this:
.myClass {
width: 30%;
padding-right: 0px;
}
<div class="myClass"></div>
But sometimes the div style is overwritten by the bootstrap classes or another inherited properties (I don't understand completely the inheritance in CSS3), but if I apply directly in the div:
<div style="width: 30%;padding-right: 0px;"></div>
2 ways to force CSS on an element in this case :
You have you custom CSS located in a local .css file : put the <link> tag for this custom stylesheet after the Bootstrap css file.
Set the CSS rule !important after each properties so they will get an extra authority upon others
CSS inheritance
.myClass is less than div.myClass which is less than body div.myClass.
The Bootstrap is using usually more than one identifier. Like .ourClass.theirClass.yourClass which is hard to overwrite. Inspect your element in your browser to see the inheritance and try to overwrite it the css way before using any !important attributes.
The last rule defining a style of the element will be aplied to it.
So if you have various stylesheets in your page, the order of the files should be in the order you want them to be applied. example:
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="secondStyle.css">
Every style rule(not an entire block) that is written in the second file will be the definitive one in the website.
the same rule apllies within files, for example:
.ClassOne {
color: red;
}
... othes styling ...
.classOne {
color: Black;
}
In this case the color in the browser will be Black because it was the last one and it overwrites the first one.
There is another rule that can affect styling - The more specific rule will be the definitive one, example:
.one .two .three {
color: red;
}
.two .three {
color: blue;
}
.one .three {
color: green;
}
<div class="one">
<div class="two">
<div class="three">
some text
</div>
</div>
</div>
Question: In which color will the text show?
Answer: red.
Why? because in the case above, we call the .three element in a more specific way when we declared the red color.
check it here:
https://jsfiddle.net/wxaw3205/
The same example with more elements:
https://jsfiddle.net/wxaw3205/1/
The last way is using the !important declaration, it provides a way for You to give a CSS value more weight than it naturally has.
For the last example, lets assume that we have the same html markup of the example above, which will be the color now?
.one .two .three {
color: red;
}
.two .three {
color: blue;
}
.one .three {
color: green !important;
}
Answer: green.
Link to live example: https://jsfiddle.net/wxaw3205/2/
And just a little tip: never style the element using the style="" attribute, unless you have too! and either the !important.
Most of the time when you have to use them its because you'r stylesheet needs to be reordered.
That's all, I hope it helped you understand.

CSS one class to ignore another

I have something like this
<div class="text-holder">
<h2>this is text-holder</h2>
<p>this is text</p>
</div>
<a href="#" class="ignore">
<div class="text-holder">
<h2>this is text-holder</h2>
<p>this is text</p>
</div>
</a>
<a href="#">
<div class="text-holder">
<h2>this is text-holder</h2>
<p>this is text</p>
</div>
</a>
And CSS that does this
.text-holder {
color: green;
}
a {
color: red;
other css
}
.ignore {
other css
}
Is there a way that the link can ignore the css style for the global a style, and just use the ignore? I don't want to use !important because there will be other instances of text-holder that will want to use the global a style.
EDIT
Thanks for all the replies, but let me be a little more clear so hopefully you understand better. (Not the best at explaining)
The problem is text-holder has it's text styling from global p, h2 and etc. Which are above a in the hierarchy of the stylesheet. so p, h2, a, .ignore, .text-holder etc
a has a lot of info on it, hover, visited, focus, color, font-weight and etc. Now for all the divs I wanted to ignore this info I was looking to see if there was a simpler way of just creating an ignore rule, rather than for all the divs I want to ignore it to overwrite them with all the rewritten information.
CSS doesn't support "ignoring", but part of its nature (the Cascading part of Cascading Style Sheets) supports "overwriting"; Newer CSS properties will overwrite older CSS properties of the same name, so you just need to give .ignore a different color value than your previous a selector's color value.
Is there a way that the link can ignore the css style for the global a style, and just use the ignore?
No. If a selector matches then all applicable rules in it will be applied.
.ignore is at least as specific as all preceding rules, so you just need to set the properties you want to override to the desired value.
Yes, basically what you're trying to do is already how CSS works.
The key to understand is the concept of specificity.
CSS rules applied through the style="" attribute have a weight of 1000.
Rules applied against an #id selector have a weight of 100.
Rules applied against a .class selector have a weight of 10.
And rules applied against an element tag name or :pseudo-selector get a weight of 1.
So for example, if you have...
a { color: red; }
.ignore { color: black; }
The weight of the red links is 1, while the weight of black text is 10, so the black has higher specificity and would win.
The important concept is that .ignore doesn't tell it to ignore its old assignment, it is instead a way to override the assignment.
EDIT
I should also add that cascading rules have no weight, so any definition in a child element will override them.
For example:
a { color: red; }
.ignore { color: black; }
div { color: blue; }
<a class="ignore"><div>hello world</div></a>
The text will be blue, not black, because the div tag has a rule applied to it which overrides the cascading black from the .ignore class.
You can try:
:not(.ignore) .text-holder {
color: green;
}
Or if you move your ignore class to .text-holder element
.text-holder:not(.ignore) {
color: green;
}

Why is body class overriding footer html

I am curious why would the second color get over-written by the first one? Is there a way around this by not adding !important or wrapping it in the body class?
body.football h3 {
color: #a07a40;
}
footer h3 {
color: white;
}
Cascading stylesheets go by a hierarchical scheme if two selectors exist targeting the same element. The more specific one will take precedence.
However, your body probably should not have a class on it like that, just as a best practice. It's begging for headaches.
Your options:
Quick and dirty, you can use a descendant selector > to specify the immediate child. For instance:
body.football > h3 {
color: #a07a40;
}
footer > h3 {
color: white;
}
<body class="football">
<h3>Football is the Bees Knees</h3>
<footer>
<h3>Hi Ma Hi Pa</h3>
</footer>
</body>
Alternately, per your OP, you can also wrap the body content in a section or div and apply styles to that, IE:
section#football h3 {
color: #a07a40;
}
footer h3 {
color: white;
}
<body>
<section id="football">
<h3>Football Yo</h3>
</section>
<footer>
<h3>Yo Football</h3>
</footer>
</body>
Edit: I should add that the empty space between the two elements means that you're going to hit ALL the elements that are descendants of the parent elements.
It is getting overridden because css will use the most specific selector over any other selector.
To resolve this you will either need to use the !important flag or change the footer selector to be more specific, the easiest way to do that is use an id.
An Example:
#footer h3{
color: white;
}
<footer id='footer'><h3></h3></footer>
CSS likes to prefer more specific declarations over less specific declarations. Without seeing accompanying HTML it may be a little hard to be sure, but it is most likely because your body.football declaration is more specific.
There is html mistake on writed code or css structure.
HTML:
<body>
<div class="football">
<h3>ho</h3>
</div>
<footer>
<h3>test</h3>
</footer>
</body>
CSS:
body.football h3 {
color: #a07a40;
}
footer h3 {
color: red;
}
Example on Fiddle.
Or if football is on footer section, then you need put a class.
footer h3 {
color: red;
}
footer .football h3 {
color: #a07a40;
}
Fiddle

How do I select an element that has a certain class?

My understanding is that using element.class should allow for a specific element assigned to a class to receive different "styling" than the rest of the class. This is not a question about whether this should be used or not, but rather I'm trying to understand how this selector is intended to work. From looking at a ton of examples on the internet, I believe the syntax is correct and do not understand why this is not working.
Here is an example:
CSS:
h2 {
color: red;
}
.myClass {
color: green;
}
h2.myClass {
color: blue;
}
HTML:
<h2>This header should be RED to match the h2 element selector</h2>
<div class="myClass">
<h1>This header should be GREEN to match the class selector</h1>
<h2>This header should be BLUE to match the element.class selector</h2>
</div>
It should be this way:
h2.myClass looks for h2 with class myClass. But you actually want to apply style for h2 inside .myClass so you can use descendant selector .myClass h2.
h2 {
color: red;
}
.myClass {
color: green;
}
.myClass h2 {
color: blue;
}
Demo
This ref will give you some basic idea about the selectors and have a look at descendant selectors
h2.myClass refers to all h2 with class="myClass".
.myClass h2 refers to all h2 that are children of (i.e. nested in) elements with class="myClass".
If you want the h2 in your HTML to appear blue, change the CSS to the following:
.myClass h2 {
color: blue;
}
If you want to be able to reference that h2 by a class rather than its tag, you should leave the CSS as it is and give the h2 a class in the HTML:
<h2 class="myClass">This header should be BLUE to match the element.class selector</h2>
The element.class selector is for styling situations such as this:
<span class="large"> </span>
<p class="large"> </p>
.large {
font-size:150%; font-weight:bold;
}
p.large {
color:blue;
}
Both your span and p will be assigned the font-size and font-weight from .large, but the color blue will only be assigned to p.
As others have pointed out, what you're working with is descendant selectors.
h2.myClass is only valid for h2 elements which got the class myClass directly assigned.
Your want to note it like this:
.myClass h2
Which selects all children of myClass which have the tagname h2
The CSS :first-child selector allows you to target an element that is the first child element within its parent.
element:first-child { style_properties }
table:first-child { style_properties }

CSS: How to address specific tags

I'm just beginning to learn CSS (and XHTML) and I ran into a problem of assigning different properties to tags which have the same tag name.
For example: I have two h3 headers, but want to address them specifically using CSS because I want to make them different colours.
I believe this has something to do with naming the headers differently (i.e. h3.a), but trying this didnt work. Help would be appreciated!
Besides the tag name CSS can be applied by Class and ID. Note that it's best to make sure the case in your tags matches the case in the tags.
.myClass may not apply to class="myclass"
IDs:
<style>
#FirstHeading {color: red;}
#SecondHeader {color: blue;}
</style>
<h3 id="FirstHeading"></h3>
<h3 id="SecondHeader"></h3>
Classes:
.redHeading {color: red;}
.blueHeader {color: blue;}
<h3 class="redHeading"></h3>
<h3 class="blueHeader"></h3>
The purpose of IDs are typically to point to one specific element in your page, classes are designed to work with multiple different elements
Classes can also be combined, so you don't need to load all the styles into one class.
<style>
.redHeading {color: red;}
.blueHeader {color: blue;}
.boldHeader {font-weight: bold;}
</style>
<h3 class="redHeading boldHeader"></h3>
<h3 class="blueHeader boldHeader"></h3>
You can assign a class to each element and use CSS to target only that class. For example:
HTML:
<h3 class="green">Green heading for this one</h3>
<h3 class="red">Red heading for this.</h3>
CSS:
h3.green { color:green; }
h3.red { color:red; }
Add different class attributes to each h3, then address them in CSS using .className.
e.g:
HTML:
<h3 class="class1">One header</h3>
<h3 class="class2">Another header</h3>
CSS:
.class1 {
color: #00f;
}
.class2 {
color: #f00;
}
This is where classes come in handy.
CSS
.myFirstClass { color:green; }
.mySecondClass { color:red; }
HTML
<h3 class="myFirstClass">Text</h3>
<h3 class="mySecondClass">Text</h3>
There are so many different ways to target selectors.
You can give them class names:
<h3 class="makeblue">This should be blue</h3>
<h3 class="makegreen">This should be green</h3>
// in you css
h3.makeblue { color: blue; }
h3.makegreen { color: green; }
You can use "advanced selectors":
<div class="container">
<h3>This should be blue</h3>
<p>
<h3>This should be green</h3>
</p>
</div>
// in your css
div.container > h3 { color: blue; }
div.container p h3 { color: green; }
have a look here: http://css.maxdesign.com.au/selectutorial/
A useful thing to keep in mind when naming classes is to avoid names that imply how the class is styled. Naming classes after their styles leaks design information into the HTML, and if you later do a redesign, you will either have class names that no longer match the design, or you will have to edit both the HTML and the CSS to keep it up to date.
A much better practice is to create classes with semantic meaning, such as: subtitle, navigationHeader etc. Additionally, it's a good practice to give multiple classes and thus "extend" objects instead of repeating yourself:
<h2 class="subtitle forum">Forum</h2>
<h2 class="subtitle group">Groups</h2>
.subtitle {
font-size: 14px;
font-weight: bold;
color: green;
}
.subtitle.forum {
color: blue;
}
.subtitle.group {
color: red;
}
In CSS by addressing a tag you address all copies of that tag unless you are more specific.
e.g.
a h3 {} would address all h3 tags within an a tag.
However if you want to style individual elements or want more freedom you should be using a class or an id.
An id can be used on one element and works like so:
<h3 id="header"></h3>
you can then use
#header {
// your css style here
}
to style it.
Or you can use a class which can be used on multiple elements like so:
<h3 class="red"></h3>
<a class="red"></a>
you can then use
.red {
// your css style here
}
to style it.
Google provides some good video tutorials here: HTML, CSS and Javascript from the ground up
Make a class in CSS, like this:
h3.class1
{
color: blue;
}
Then just say:
<h3 class="class1"></h3>
You can use the class or a parent to define it. If you use a class it would be defined like:
h3.colorOne {
color: #ff0000;
}
h3.colorTwo {
color: #0000ff;
}
Then they would be used like:
<h3 class="colorOne">I'm red</h3>
<h3 class="colorTwo">I'm blue</h3>
Alternatively you can specify settings by a parent using an id field in a div of sorts:
#divOne h3 {
color: #ff0000;
}
#divTwo h3 {
color: #0000ff;
}
Which would be used like:
<div id="colorOne"><h3>I'm red</h3></div>
<div id="colorTwo"><h3>I'm blue</h3></div>
The usage all depends on the needs of your layout and the extensibility of your styles.