What happens if I have both a background-color and background-image property on the same element?
Answer
When we have both a background-color and background-image property applied to the same element, whichever property that is physically lower in the CSS file will be applied. In the example code our background-image property is physically lower in the CSS file and will be rendered to the page instead of the background-image property. If however, the background-image URL value fails to load, the background-color property will be applied instead.
There is a typing error in 2nd sentence, you meant" In the example code our background-colour property is physically lower in the CSS file and will be rendered to the page instead of the background-image property.? right?
Position in the physical file applies to selector rules only, not the declaration blocks. They are object like and have no order.
To experiment and see for yourself what happens, comment out or remove the background-color property and create a new selector rule with the same selector.
div {
background-color: rebeccapurple;
}
Write that rule AFTER the one in your post above, and save. Refresh the page. What do you see? Background color, only?
I came to this topic from a link in “A Closer Look at CSS” lesson of the “Make a Website” course. I think @aubsrey’s answer is incorrect. Within a rule, it doesn’t seem to matter if you have background-color or background-image first. background-image, if it loads successfully, overrides the background-color.
In fact, it doesn’t even matter if you try to override the background image by placing background-color into the inline CSS of a specific element. background-image still displays. This is not surprising, because W3C states:
In the days of the browser wars browsers were often quirky so it was common to see ‘rule of thumb’ type recommendations come up in the community.
color
background-color
background-image
was the order of the day. Today’s browsers tend to conform better to recommendations and more vendors adhere to the specs. Many of the old rules have fallen away.
I am completely lost in this section as whatever answer I gave did not run in the CSS even when the correction or solution is given, I see no difference. So frustrating.
May be this very class is not for me or something wrong with the main.CSS.
When I ask for help in just one question, the rest of then answers get supplied and I wonder how then I can progress.
I was only supposed to change 32px to 48px; yet it did not run but kept asking if I had changed the value to 48px.
it started with change the color of the font to red, I did for over 20 mins but it would not run, when I got tired and asked for one solution, I got the entire solution given. so sad.
I clicked on next experience similar error.
Yes, even with that color of #ffffff; the code refused to run.
I have however moved on with their solution that changed the color to entirely different mixture of color.
I also noticed that when I moved back one step or refreshed or keep off for sometime, the code ran ok. I sincerely do appreciate your timely responses though.
I am in the last segment right now.
Thanks again.