技术问询:网页中main.jpg背景图片无法显示的问题排查
问题原因及解决办法
1. 路径转义字符错误
你的CSS中背景图片路径使用了反斜杠 \:
background-image: url('pictures\main.JPG');
在CSS字符串里,反斜杠是转义字符,会让\m被解析成特殊字符,实际路径变成picturesmain.JPG,导致找不到图片。
解决:把反斜杠替换成正斜杠 /:
background-image: url('pictures/main.JPG');
2. 容器无高度设置
承载背景图的<div>是空元素,默认高度为0,就算图片路径正确,也没法显示出来。
解决:给这个div添加高度样式,比如:
div { height: 400px; /* 可根据需求调整高度 */ }
修改后的完整代码片段:
<!DOCTYPE html> <html> <title>W3.CSS Template</title> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="description" content="middleEastern furniture" /> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css" /> <head> <style> body { background-color: white; } .bg-container { background-image: url('pictures/main.JPG'); background-size: cover; background-position: center; height: 400px; } </style> </head> <!-- Header --> <header> <br /> <br /> <br /> <br /> <br /> <br /> <div class="bg-container"></div> </header> </html>
内容的提问来源于stack exchange,提问作者davidjack
相关产品推荐
相关产品推荐

