how do i add an image in the background in css fro

2019-08-02 16:34发布

I have an image, and from what I've seen in W3schools you can just use
body background = farm.jpg
And that works, but it says and I've seen people say this, that it can work with css with

background-image: url(farm.jpg) (with and without quotes)

yet whenever i try it can't find the picture.Do I need to put it in a separate folder?

2条回答
Emotional °昔
2楼-- · 2019-08-02 16:54

(1) https://www.w3schools.com/tags/att_body_background.asp

Unfourtunately the background attribute is not supported in HTML5. You must use CSS instead.

(2) You can use the code example form this page, what works:

https://wiki.selfhtml.org/wiki/CSS/Eigenschaften/Hintergrundfarben_und_-bilder/background-image

For example:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Your page</title>
  <meta name="description" content="The HTML5 page">
  <meta name="author" content="Your Name">
  <link rel="stylesheet" href="css/styles.css?v=1.0">
  <style>
      body {
        background-image: url("background.png");
      }
  </style>
</head>
<body>
  <p>Test.</p>    
</body>
</html>

Hope this helps.

查看更多
做自己的国王
3楼-- · 2019-08-02 17:10

It depenteds to where is your css file , put your css file and image file into same place, and try below

background-image: url("frame.jpg");

Or you can use / to start from root folder.

background-image: url("/frame.jpg");

Read this.

查看更多
登录 后发表回答