html - 为什么 body 背景图像不显示?

标签 html css background-image

我有一个简单的代码,在 html 的 body 标签内有一个文本,一个 css 样式表只是为了有一个背景图像,我不确定为什么背景图像根本不显示。

这是我的 html:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
    Test    
</body>

</html>

CSS 就是这样的:

#body { 
    background-image: url(images/ninja.jpg);
}

我在图像文件夹中有 "ninja.jpg"。并且仍然没有显示背景图像。我也尝试过将 images/ninja.jpg 放在引号内(即:"images/ninja.jpg"),但它不起作用。

最佳答案

不要使用#body
使用body
尝试

body { background-image: url(images/ninja.jpg); }

用于 CSS

#body指的是属性为id="body"

的元素

body {background-color: green;} /*this is the actual body*/

#body{background-color: red;} /*this is the paragraph*/
This is green

<p id="body">This is red</p>
    

关于html - 为什么 body 背景图像不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38369960/

相关文章:

javascript - 无需页面加载即可提交帖子表单

javascript - 更改样式属性

html - 部分png文件的背景图像

javascript - 刷新后使用 javascript 更改 div 背景时出错

HTML 将一个 div 对齐成一行

html - div 关闭问题,它的 href 溢出到其他内容

angularjs - 如何将最大长度属性添加到 Angular-xeditable?

html - 关注链接中的内容

html - 使用 Angular 制作可配置的水平滚动菜单

html - 背景无法在 Chrome 中正确呈现