You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在HTML/CSS中显示背景图?我的作品集背景图无法生效

Troubleshooting Your Background Image Not Displaying

Hey there! No worries about your English at all—let's work through why your portfolio's background image isn't showing up. Here are practical, step-by-step checks tailored to your code:

Common Causes & Fixes

  • Double-check the image path
    Your CSS uses url(../img/dev-dots.png)—relative paths depend on where your CSS file lives, not your HTML file. For example:

    • If your CSS is in a /css folder and your image is in the root /img folder, ../img/ is correct (it moves up one level from /css to the root, then into /img).
    • If your CSS is in the same root folder as /img, you need url(img/dev-dots.png) instead.
      Test the path directly: paste the full image URL (like http://your-site.com/img/dev-dots.png) into your browser’s address bar. If it doesn’t load, the path is wrong or the file is missing.
  • Give .main element height
    Your .navbar uses position: fixed, which takes it out of the normal document flow. That means your .main element has no content to give it height (it defaults to 0px tall), so the background image has no space to display. Add this to your .main CSS:

    .main {
      background-image: url(../img/dev-dots.png);
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      min-height: 100vh; /* Makes it take up the full viewport height */
    }
    
  • Verify the image file itself

    • Ensure the file exists in the img folder with the exact name dev-dots.png (file names are case-sensitive on most servers—Dev-Dots.png won’t match if your CSS uses lowercase).
    • Open the image directly on your computer to confirm it’s not corrupted.
  • Check for CSS conflicts
    Open your browser’s DevTools (right-click the page > Inspect), select the .main element, and look at the Styles tab. If background-image is crossed out, another CSS rule is overriding it. You can fix this by making your selector more specific (e.g., body .main) or adjusting the order of your styles.

  • Clear browser cache
    Sometimes browsers cache old versions of your CSS or images. Force a hard refresh (Ctrl+F5 on Windows/Linux, Cmd+Shift+R on Mac) to load the latest files.


内容的提问来源于stack exchange,提问作者mooncrunch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 18:03:11