简体   繁体   English

如何设置背景图片?

[英]How do I set a background picture?

I wrote the CSS code for background-image, but it doesn't show when set in my CSS sheet. 我为背景图片编写了CSS代码,但是在CSS工作表中设置时却没有显示。

I tried and it only works when written in the body... 我尝试过,并且只有当写在身体上时才起作用...

<body background="Tokyo.png">

but not in style sheet. 但不在样式表中。

html {
  background-color: black; 
  margin: 10px;
}

body {
  background: "Images\tokyo.png";
} //CSS code in style sheet//

I expected background image to be shown but I don't. 我希望显示背景图片,但不会。

您需要将路径包装在url

background-image: url("Images\tokyo.png");

If it's an image you have to make sure to include url and then put the path in parenthesis like below. 如果是图像,则必须确保包含url,然后将路径放在圆括号中,如下所示。 Also make sure your CSS is linked in the HTML. 另外,请确保您的CSS已链接到HTML。

body {
 background-image: url("Images\tokyo.png");
}

First you have to make sure to include url and then put the path correctly in parenthesis like below. 首先,您必须确保包含url,然后将路径正确放在括号中,如下所示。 Also make sure your CSS is linked in the HTML. 另外,请确保您的CSS已链接到HTML。

body { background-image: url("Images\\tokyo.png"); }

Note : If your css file is inside css directory then the path should be like- 注意 :如果您的css文件位于css目录中,则路径应类似于-

body { background-image: url("../Images/tokyo.png"); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM