How to force span to use parent font-family in CSS - html

When HTML span has CSS font-family, I can't force it to use parent font-family
.Parent {
font-family: tahoma !important;
}
.Child {
font-family: cursive, geneva;
}
<div class="Parent">
<span class="Child">Text</span>
</div>
Why span don't use parent font-family?
How can I make this work?

You could select all the children elements using .parent * and then set font-family to inherit. This will effectively override the child element font and force all children elements to inherit the value of whatever the closest parent element's font is.
.parent {
font-family: tahoma;
}
.child {
font-family: cursive, geneva;
}
.parent * {
font-family: inherit;
}
<div class="parent">
<span class="child">Text</span>
</div>
And if you only want to target the .child element, you would obviously change the selector to .parent .child.
Depending on what you're trying to achieve, it's also worth mentioning that you can use the direct child selector, >, in order to only target direct children: .parent > *.
.parent {
font-family: tahoma;
}
.descendant {
font-family: cursive, geneva;
}
.parent > * {
font-family: inherit;
}
<div class="parent">
<span class="descendant">Direct child. <em class="descendant">Not a direct child</em></span>
</div>

CSS priority goes something like this:
Rules on this element marked with !important
High to low rule scoring based on the number of ID tags, classes, etc., for rules applying to this element.
The browser's default user agent styles.
For rules that are inherited by default, like font-family (but not others like background-color), the current value of the parent(s).
What your child node is getting is not number 1. in that list, but 4. The !important flag is making sure that the parent has that font set, but that importance does not carry over to children. You could set font-family: inherit !important if you really, really want every element to take its parent font.
Word of advice, though: Only use !important in extreme situations. You can often one-up another CSS rule's priority in a much more gentle way.

Related

How to exclude parent css style in child elements?

here I have HTML elements stacked like this,
.center-badge p {
font-size: 12px;
}
<div class="center-badge">
<div>
<p>Get a</p>
<p><strong>2% rate reduction</strong></p>
<p>with a</p>
<p>co-applicant</p>
</div>
</div>
and I have added the font size as 12px for the center-badge CSS. In this, we need to exclude the strong tag with p. Only the 12px styling has to apply all the p tags but a strong tag.
We have added a global styling for the p as 16px. How to exclude a particular element to not apply the parent CSS.
Is any way to solve this. can we can use the :not() for this scenario.
If an element has font-size: inherit or font-size: someUnitRelativeToTheParent — and it doesn't matter if that is set explicitly or by the browser stylesheet — then you can't make it not base the font-size on that of the parent element.
Likewise there is no way to write a selector with a condition "Unless the element's children include X" to avoid applying the font-size in that particular p in the first place.
You can either:
Explicitly style the strong element with a font-size that uses absolute units or
Change the DOM in such a way that you can exclude the p (e.g. by adding a class to it and adding :not(.that_class) to the stylesheet.
you have applied the global css like this I think.
p {
font-size: 16px;
}
but once you apply css using the parent class like this way
.center-badge p {
font-size: 12px;
}
it overrides your global css for <p> tag.
now <strong> has no browser default font size as <p> tag for the font-size property.
so you have to define it globally like this way
strong {
font-size: 16px;
}
or using parent class also you can apply the css like this way.
.center-badge strong {
font-size: 16px;
}
or you can apply it by giving the font-size: initial to the <strong> tag like this way.
.center-badge strong {
font-size: initial;
}

Difference of setting the font-size on the div vs h1

What is the difference between setting some properties directly on the div container vs directly on the element in it. For example font-size:
<div class="the_last_of_us">
<h5>Cookie Settings</h5>
</div>
Major different between setting the font-size on div vs h1 or h5 in the above example is, setting the font-size will not be inherited by the h5 by default, as it will pick the styles from the User Agent Stylesheet, unless you explicitly define it to inherit, for example
h5 {
font-size: inherit;
}
Whereas setting the font-size explicitly on the h5 will override the user agent stylesheet and set the font-size you have defined for the h5 element.
In other scenarios, it makes sense to set the properties on the Parent element, which will be inherited by a few elements. This will help you keep your selector specificity low. For example, setting color to the div can be inherited by the h1 element.
So instead of a selector with a property like
div h5 {
color: #f00;
}
You can use
div {
color: #f00; /* Also applies color to any element inside
the div which can inherit color from the parent element */
}

Nested div not being given parent's style, using * style instead

I'm not understanding why the innermost nested div below isn't the same size as the #smaller-size. It's a direct descendant. Is it because the global * style is more specific? This is really gut-punching my page. What am I missing? Thanks!
* { font-size: 15pt; }
#smaller-size { font-size: 0.8em; }
<div>
This should be the size specified by * (and it is)
<div id="smaller-size">
This should be the smaller size (and it is)
<div>
This is nested in #smaller-size, so it should also be smaller (but it's not!)
</div>
</div>
</div>
The specificity of the "*" selector is the lowest possible (0,0,0,0), but it applies to every element applicable, so every div will be:
* { font-size: 15pt; }
By the time you are setting the new specificity with the # selector:
#smaller-size { font-size: 0.8em; }
Only the element with the corresponding id selector will have higher specificity than the rest, affected by *, which are all of them.
Creating in this particular element (0,1,0,0) especificity value.
This id applies to that particular element only.
If for example you stablish something like:
#smaller-size>div { font-size: 0.2em }
Then all elements div under id smaller-size will have higher specificity than the ones affected by just *
Creating something like that:
<div> -- specificity (0,0,0,0) = 0
<div id="smaller-size"> -- specificity (0,1,0,0) = 100
<div> -- specificity (0,1,0,1) = 101
</div>
</div>
</div>
With this logic:
* { font-size: 15pt; }
#smaller-size { font-size: 0.8em; }
#smaller-size>div { font-size: 0.7em; }
#smaller-size>.smaller { font-size: 0.6em; }
<div>
specificity of this element (0,0,0,0) = 0
<div id="smaller-size">
specificity of this element (0,1,0,0) = 100
<div>
specificity of this element (0,1,0,1) = 101
</div>
<div class="smaller">
specificity of this element (0,1,1,1) = 111
</div>
<div class="smaller" style="font-size: 0.5em;">
specificity of this element (1,1,1,1) = 1111
</div>
<p>
specificity of this element (0,0,0,0) = 0
</p>
</div>
</div>
Here is a very good post explaining how specificity works.
Is it because the global * style is more specific?
No. It is because it matches the element and #smaller-size does not.
Specificity only matters when both selectors match the same element.
If you didn't have * { font-size: 15pt; } then the inner div would have the default value for font-size which will be something along the lines of 100% (i.e. the same size as the parent element).
You should probably just set the font size on body and let inheritance take case of the rest. Although there are a few gotchas:
Form controls don't default to 100% so you might want to explicitly set the size for input elements and the like
In Quirks mode fonts may not inherit into tables by default, so ensure you have a Doctype that triggers Standards mode
pt is a unit designed for physical media. It does not get rendered accurately on screen. Consider using a different unit (like pixels).
Yes, it is because your global * style. Your nested div has no class or id, and the browser tries to find the first applicable style element, and it is the *.

CSS *:not(div p) does not select anything

I have the following a HTML code:
<div>
<p> linux version</p>
<h1> new tool </h1>
And some CSS for it that should select <h1> but does not select anything.
*:not(div p) {
font-family: sans-serif;
}
The following does not work too:
*:not(div>p) {}
I have so many such <div> <p> in the HTML whereas the following selects and apply the font:
div p {
font-family: sans-serif;
}
As others have stated in the comments: the usage of the not selector is like this:
E:not(s) - an E element that does not match simple selector s
where
A simple selector is either a type selector, universal selector,
attribute selector, class selector, ID selector, or pseudo-class.
So if you want you code to work you'll have to add a class to the <p> elements which you don't want styled with that font-family.
*:not(.classname) {
font-family: sans-serif;
}
Alternatively: If you need to apply a font to all your elements - it is generally done by setting it in the body element, then the other elements in the document inherit that rule.
Then you can style your <p> elements within the div differently.
body
{
font-family: sans-serif;
}
div p
{
/* the special font-family that you need for paragraphs within a div */
}
<div>
<p> linux version</p>
<h1> new tool </h1>`
</div>
Now consider the following CSS code-
*:not(div p) {
font-family: sans-serif;
}
This code selects all elements except the <p> inside <div>. So a <div> is also selected by the selector *:not(div p) and hence all the contents of the <div> gets the style: font-family: sans-serif. So the text in the <p> element in the <div> also gets the style.
N.B. You should keep track so that two CSS declaration don't contradict each other. Then if such contradiction arises the declaration that applies some style wins over the declaration that forbids that style to be applied on that element.
Hence the following code will run fine
div>:not(p)
{
font-family: sans-serif;
}
This selector will select the elements inside a <div> except <p>-elements. So you may use this instead.
Well, it won't be exactly the same thing, but in this case you can use
div>*:not(p)
instead of
*:not(div>p)
Demo
It seems in your question, that given the markup:
<div>
<p>linux version</p>
<h1> new tool </h1>
</div>
<h1> elements are the special ones. So be specific. Rather than define styles for "all-but-me" (as you do with "*:not(div p)" clause) set and standard for "all-of-them" and then you overwrite the one you consider special. Just like here:
div {
font-family: serif;
}
div > h1 {
font-family: sans-serif;
}

CSS not recognising classes

I feel a bit silly asking this as the answer is bound to be obvious. I set a h3 tag with a class on a WP template but for some reason the header tag is getting picked up as the default setting but the class is not and I can't understand why.
Html:
<h3 class="contact-heading">Get in Touch</h3>
CSS:
.contact-heading h3{color: #3C0; font: 2.0em Verdana, Geneva, sans-serif;font-weight:
normal;letter-spacing: 5px; text-transform:uppercase;}
Change css to:
h3.contact-heading { .... }
.contact-heading h3 means "apply these styles to all h3 elements inside an element with the class contact_heading"
h3.contact-heading means "apply these styles to all h3 elements which have the class contact_heading"
The selector .contact-heading h3 will select any <h3> element within an element that has the class .contact-heading.
If you change the selector to h3.contact-heading it will select the <h3> element that has the class .contact-heading.
Remember to NOT put a space bewtween h3 and .contact-heading, or you're changing the selector :)