简体   繁体   中英

Why this code is not showing the background image?

My HTML code-

 <html> 
 <head> 
 <style type="text/css"> 
 { 
 /*for setting background image*/
 background-image: url('C:/Users/SONY/Desktop/CSS/images.jpg'); 
 background-attachment: fixed 
 } 
 </style> 
 </head> 
 </html>

This code is for setting the background image. But it's not showing the result. I've not created two files. Like one for CSS and another HTML file. I've compiled both the files into one.

Why am I not able to see the background image? Despite giving the correct path for the URL.

You need to specify the element for which want to give particular CSS properties like html , body , .class , #id .

body {
   /*for setting background image*/
   background-image: url('http://lorempixel.com/400/200/sports/1/'); 
   background-attachment: fixed 
}

Here is the link of working fiddle .

You are applying background image to none of the class/id or any html tags like html, body etc. either you need to add a class or id or need to apply style to the inbuilt tags. you can try this

  <html> 
   <head> 
   <style type="text/css"> 
   html{ 
   /*for setting background image*/
   background-image: url('C:/Users/SONY/Desktop/CSS/images.jpg'); 
   background-attachment: fixed;
   } 
   </style> 
   </head> 
   </html>

Hope this helps.

我认为您的代码在css文件夹下,因此该行应为:background-image:url('images.jpg');

Use this style in your code. If want to pick image from local computer.

<!DOCTYPE html>
<html>

<head>
    <style type="text/css">
        html {
            background-image: url(./images.jpg);
            background-attachment: fixed;
        }
    </style>

</head>

<body>

</body>

</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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