Set background image of a webpage using HTML/CSS &

2019-08-22 04:30发布

I have an HTML file and links to a CSS file. I got it to display a repeating background image under the CSS file by doing this (under body tag in the CSS file) and it works:

body {
    /*Set background image*/
    background: url("../img/background/main.jpg") repeat;
}

But when I try to do it this way (under html tag in the CSS file) it doesnt work:

html {
    /*Set background image*/
    background: url("../img/background/main.jpg") repeat;

According to this site it shows the example using the html tag: This site shows html in css being used with html tag

So how do I make this work, or should I NOT be doing it this way?

1条回答
趁早两清
2楼-- · 2019-08-22 05:06

It's generally considered bad practice to put CSS on the HTML tag, so you should keep it on the body tag. Usually this is for semantic reasons - the HTML element contains child elements that should not and cannot have styles applied to them (ie. the head).

Developers tend to apply styles to the HTML element out of laziness or because they don't know any better or to avoid adding additional elements to the DOM, but that doesn't make it right.

Handily, using the body element already works for you, so nbd. :)

查看更多
登录 后发表回答