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;
}
Related
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;
}
I am creating a custom CSS class and i am not sure how to write the code.
The element uses a h2 font size for its heading and a standard paragraph font size for everything else.
I would like to code a custom size for the h2 headings.
my custom class name is hosting-post-slider and my code starts as follows...
.hosting-post-slider {
}
I am not sure how to refer to the h2 heading in my CSS in the above so i can customize h2 just for this element.
if you have some styling in a class such as
.hosting-post-slider {
color: red;
}
Then to make a single h2 tag have this style, simply do this in you HTML:
<h2 class="hosting-post-slider">The heading</h2>
If you want all h2's to have this style, either add that class to all h2 elements, OR change your css to target all the h2s, such as:
.h2 {
color: red;
}
The code below will reference only this instance of the h2 element. Any style you add to it will only be applied to this element.
The complete code will look like this:
.hosting-post-slider h2 {
font-size: 30px;
}
<div className="hosting-post-slider">
<h2>My header</h2>
<p>My paragraph</p>
</div>
Is this what you are looking for, this selects all h2 elements with your custom class
h2.hosting-post-slider{
}
I want to change the CSS of <strong> element as below:
<p><strong>Make it Bold</strong><p>
<p>This is dummy text<strong>Do not make it BOLD</strong></p>
<p><strong>Make it Bold 2</strong><p>
<p>This is dummy text<strong>Do not make it BOLD 2</strong></p>
Can I somehow make a change <strong> CSS where I have written 'Make it Bold', the <strong> elements should only be bold if there is no text between the opening <p> tag and the opening <strong> tag1.
.strong{
font-size: 30px;
}
I tried it with above CSS but I know it will change all strong elements; how can I achieve my particular requirement?
P.S.: I cannot any additional classes
Paraphrased from the following comment:
[I] want element to be bold only if string exists right after <p>.. if there is any text before strong then that strong tag shall not be turned bold.
Quoted from comment, below: Change CSS of first <strong>.
The :first-of-type selector allows us to target the 1st occurence of an element inside it's container.
p strong:first-of-type {
font-size: 30px;
}
I hope I helped some people who are facing the same situation
you can give a class or id to your strong element.
In your current code there are no classes.
You can use, for example:
<p><strong class="big">Make it bold</strong></p>
And in css
.big{
font-size: 30px;
}
Use the :first-child pseudo-class. JSFiddle...
:first-child strong {
font-size: 40px;
}
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.
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 :)