I have the following html5 code. I expected the style for the text Business Ads to be italic and color in yellow. But it comes in red.
Can only certain styles be applied to the aside element?
CSS:
aside h4 {
font-style: italic !important;
color: yellow;
}
article h4 {
font-style: normal;
color: red;
}
HTML:
<div>
<article>
<aside>
<h4>Business Ads</h4>
</aside>
</article>
</div>
This is a result of the way CSS specificity works. The page here:
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
provides a good explanation. In this case, since both style declarations refer to an h1 within a larger element, they have equal specificity, and the latest declared style takes precedence. You can override this with !important, but it's usually considered bad style because it breaks the "cascading" nature of CSS. Instead, use a more specific selector:
article aside h1 {
//style goes here
}
You override the rules the way you have set your CSS. Both rules target same element, so the second one will override the first one and apply to the element.
For example if you set the oppossite order to your rules like this :
article h4 {
font-style: normal;
color: red;
}
aside h4 {
font-style: italic !important;
color: yellow;
}
the second one will aply and h4 will be yellow an italic
So if you have an h4 also inside article you can use this:
article aside h4 {
font-style: italic !important;
color: yellow;
}
article h4 {
font-style: normal;
color: red;
}
DEMO
You must be more specific with the selector so that the rule it is assigned to overrides the "default" one. You can the remove the !important which isn't the best way to override existing rules when you can use other techniques.
DEMO
article aside h4 {
font-style: italic;
color: yellow;
}
article h4 {
font-style: normal;
color: red;
}
You're targeting the same h4 element but you gave it with different styles
and the last one was read. Just delete
article h4 {
font-style: normal;
color: red;
}
and remove the !important in the first selector.
And if you're targeting different h4 tags inside an article or aside tag, what you can do is put classes or span on them.
Related
I run into a situation where I need to override font-weight property. However, even though I use !important to increase its priority, it still gets overridden by other styles. As you can see in the following example, I am expecting hello world to be bolded, but it is using font-weight: normal instead. Any ideas? Thanks
body {
font-weight: bold !important;
}
div {
font-weight: normal;
}
<div>Hello World</div>
You can consider the universal selector if you want to make your font-weight to apply on the div and also other tags. (as i suspect you want to make all the page bold)
In your case you are appling the style to body and not the div element and style of child element always override the parent style even with the use of !important. So this rule will only work with element that inherit the font-weight from the body.
body *{
font-weight: bold !important;
}
div {
font-weight: normal;
}
<div>Hello World</div>
This has to do with binding priority of the styles. A way to resolve this is to make the rule more specific, e.g. by targeting body div instead:
body div {
font-weight: bold !important;
}
div {
font-weight: normal;
}
<div>Hello World</div>
However, please don't do this unless absolutely completely unavoidable. !important is a hack at best. If you are in control of the HTML, just introduce a class or use a more relevant selector.
div.entry-content:not(body.single div.entry-content)
{
font-family: 'Droid Sans', sans-serif;
margin-top:10px;
line-height: 114%;
font-size:15px;
}
the above css doesn't effect any elements at all ....I want div.entry-content elements that exist inside a body with the class single to be styled differently from the rest div.entry-contentelements ..
body.single div.entry-content
{}
and
div.entry-content
{}
do seem to work just fine .. but
div.entry-content:not(body.single div.entry-content)
{}
doesn't seem to
You need to rewrite the selector line to work the way you intend it to:
body:not(.single) div.entry-content {
font-family: 'Droid Sans', sans-serif;
margin-top:10px;
line-height: 114%;
font-size:15px;
}
This CSS Tricks page elaborates on the fact that you may only use a simple selector within a :not pseudo-class, which they define as follows in the footnote:
A simple selector is classified as a Type Selector, Universal Selector, Attribute Selector, Class Selector, ID Selector, or Pseudo Class Selector.
I want div.entry-content elements that exist inside a body with the
class single to be styled differently from the rest
div.entry-content elements
If you really want to use the :not() selector then you need to do something like this:
body:not(.single) div.entry-content {
font-family: 'Droid Sans', sans-serif;
margin-top: 10px;
line-height: 114%;
font-size: 15px;
/* demo*/
background: red
}
<body>
<div class="entry-content">test
</div>
</body>
with class single in body
body:not(.single) div.entry-content {
font-family: 'Droid Sans', sans-serif;
margin-top: 10px;
line-height: 114%;
font-size: 15px;
/* demo*/
background: red
}
<body class="single">
<div class="entry-content">test
</div>
</body>
See more info in MDN about :not
The negation CSS pseudo-class, :not(X), is a functional notation
taking a simple selector X as an argument. It matches an element that
is not represented by the argument. X must not contain another
negation selector.
body:not(.single) div.entry-content {
font-family: 'Droid Sans', sans-serif;
margin-top: 10px;
line-height: 114%;
font-size: 20px;
color:white;
background:green;
}
<div class="single">
this is the single div
<div class="entry-content">
write some text inside entry content
</div>
</div>
Please may someone explain why the text 'impact on market' is green as opposed to yellow?
I was expecting this to be yellow
HTML
<div>
<h4> International news </h4>
<article>
<h4 class= "headline"> news develop</h4>
<aside>
<h4> impact on market </h4>
</aside>
</article>
</div>
CSS
h4 {
color:blue;
}
.headline {
color:red;
}
article {
color:black;
font-style:normal;
}
aside h4 {
font-style: italic !important;
color yellow;
}
article h4 {
font-style:normal;
color: green;
}
It is because article h4 comes after aside h4 and their degree of specificity are equal. CSS files are processed from top to bottom and if another style comes along with an equal or greater specificity, then that will override the previous style.
You can use this:
side > h4 {
font-style: italic !important;
color yellow;
}
article > h4 {
font-style:normal;
color: green;
}
Where > means only affect direct descendants. This is typically better to use than !important, since !important is considered the very last resort.
Further reading on CSS specificity:
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
http://css-tricks.com/specifics-on-css-specificity/
Further reading on !important:
http://css-tricks.com/when-using-important-is-the-right-choice/
The order in which you put them. The last one is the one written last, so therefore more important.
Try:
article aside h4
{
font-style: italic !important;
color: yellow;
}
Specify the structure more.
Alternative, create a class.
<h4 class="level3"> impact on market </h4>
Then add
.level3
{
color: yellow;
}
It is all about CSS weights, here
article h4
is overriding
aside h4
you can change order or edit the
aside h4
for
article aside h4
and it will work. Both will work, change order or edit the selector.
In my style sheet, I have overridden the style of H1 and H2 with the following code:
And in my HTML, I have applied that style to a DIV that contains an H1 tag.
However, this style is also applying to H1 and H2 tags AFTER the div in question.
Replicated here: http://jsfiddle.net/89gkQ/1/
Why is the style applying outside of the div where it's applied, and how do I stop it?
In CSS, the comma doesn't work like it does in English:
.featuredtitle h1, h2 {
color: red;
}
That code is equivalent to this code:
.featuredtitle h1 {
color: red;
}
h2 {
color: red;
}
Which isn't what you want. The comma just allows you to write multiple selectors, so you want to be a bit more verbose:
.featuredtitle h1, .featuredtitle h2 {
color: red;
}
Demo: http://jsfiddle.net/89gkQ/2/
The problem is here:
.featuredtitle h1,h2 {
font-size: 1.5em;
font-weight:bold;
color:#a00;
}
You should write the following instead:
.featuredtitle h1, .featuredtitle h2 {
font-size: 1.5em;
font-weight:bold;
color:#a00;
}
The comma starts a new selector, which in this case made the style apply to all H2 tags, regardless of where they are.
I have a headline:
<h1>THIS IS A HEADLINE</h1>
How do I make the phrase "THIS IS..." not to be bold and the rest without a change?
I couldn't find any relevant tag in text-decoration.
The heading looks bold because of its large size. If you have applied bold or want to change behaviour, you can do:
h1 { font-weight: normal; }
More: 3.2. Font weight: the font-weight property
Try font-weight:normal;
h1 {
font-weight: normal;
}
<h1><span style="font-weight:bold;">THIS IS</span> A HEADLINE</h1>
But be sure that h1 is marked with
font-weight: normal;
You can also set the style with a id or class attribute.
You want font-weight, not text-decoration (along with suitable additional markup, such as <em> or <span>, so you can apply different styling to different parts of the heading)
style is accordingly vis CSS. An example:
<h1 class="mynotsoboldtitle">I'm not bold</h1>
<style>
.mynotsoboldtitle { font-weight: normal; }
</style>
<h1><span>This is</span> a Headline</h1>
h1 { font-weight: normal; text-transform: uppercase; }
h1 span { font-weight: bold; }
I'm not sure if it was just for the sake of showing us, but as a side note, you should always set uppercase text with CSS :)
For "THIS IS" not to be bold, add <span></span> around the text:
<h1>><span>THIS IS</span> A HEADLINE</h1>
And in style
h1 span{font-weight: normal}
You can simply do like that in the HTML part:
<span>Heading Text</span>
And in the CSS, you can make it as an h1 block using display:
span{
display: block;
font-size: 20px;
}
You will get it as a h1 without bold.
If you want it bold, just add this to the CSS:
font-weight: bold;
You can use font-weight:100 or lighter: this is working with i.e. Opera 16 and older, but I do not know why the h1 tags in Firefox are bolder, sorry.
If you want to remove the bold, you can use the code below,
h1 {
font-weight: normal;
}
But for "THIS IS" not to be bold, add <span></span> around the text,
<h1><span>THIS IS</span> A HEADLINE</h1>
And in style,
h1 span {
font-weight: normal;
}
Code example result,
h1 span {
font-weight: normal;
}
<h1><span>THIS IS</span> A HEADLINE</h1>