What is causing the background-image to fail in an external CSS file but work perfectly in an internal one?

Hey there! I'm new to HTML and CSS, so please bear with me as I share my lack of expertise. I've been facing an issue while trying to add a background image via external CSS. Strangely enough, it works when added directly in the HTML file. I searched for solutions to this problem and tried a few, but none seemed to work or explain why this problem is occurring. Can someone please help me out?
I made sure to double-check the path when loading the image through CSS, but still no luck.

The directory structure looks like this:


    |part1|
         |css|
             style.css
         |images|
             header_bg.png
             logo.png
         |js|
            currently empty
         index.html 

--HTML code:

<!DOCTYPE html>
<html>
</head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <meta charset="utf-8">
    
    <style type="text/css">
        .container_banner {
            width: 100%;
            height: 557px;
            background-image: url("images/header_bg.png");
        }
    </style> 
</head>

<body>
    <div class="header">
        <div class="center">
            <img src="images/logo.png">
        </div><!-- close the div center-->
        
    </div><!-- close the header-->
    
    <div class="container_banner">
        <div class="center">
            
        </div><!--close the div center-->
    </div><!--close the container-banner-->
</body>
</html>

--My CSS:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}


.center{
    width: 1280px;
    margin: 0 auto;
}

.header{
    width: 100%;
    height: 60px;
    background-color: #212343;
}
 .header img {
    margin: 19px;  
 }

 .container_banner{
    width: 1280px;
    height: 800px;
    background-image: url("..images/header_bg.png");
 }

Answer №1

When using an external CSS stylesheet file, it will search for images relative to its own URL if there are any relative URLs within it. For example:

includes:

background-image: url("images/header_bg.png"); /* This will point to http://www.external-website.com/css/images/header_bg.png */

Therefore, if your website's URL is and you're linking in it, the relative URLs within that external CSS file will not direct to resources (such as images).

Answer №2

 .header_contents{
    width: 1280px;
    height: 800px;
    background-image: url("../images/header_bg.png");
 }

The /.. directory functions similarly to the /images folder

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you show me a way to use jQuery to delete several href links using their IDs at once?

Currently facing a challenge with removing multiple href links that share the same ID. Here is a snippet of my code: $('.delblk').click(function(e) { e.preventDefault(); var id = $(this).attr('id').substr(7); ...

iPhone failing to interpret HTML entities

I am currently developing a website for a client, who is facing a bug that I have not been able to replicate... For reference, here is an example page: The issue my client is experiencing involves using an iPhone to navigate the website. It appears that ...

What is the reason behind IE7 and IE8 claiming to have 'hasLayout' when in reality it does not possess such a feature?

HTML: <div class="table" > <div class="row" > <span>Table with DIV</span> <span>column2</span> <span>column3</span> </div> <div class="row" > <span>col ...

Visualizing Data with HTML and CSS Chart Designs

I'm seeking assistance with creating an organization tree in a specific layout. https://i.sstatic.net/z9LRc.jpg I am having trouble replicating the tree structure shown in the image. I need help generating a similar tree structure where multiple le ...

I keep encountering an issue with getJson

A snippet of my JavaScript code retrieves a JSON object from a URL. Here is the portion of the code in question: <script> $(document).ready(function(){ $("button").click(function(){ $.getJSON('url_address', {}, function(data) { ...

Can you display a simple HTML view?

I am currently working on a Node.js application with Express framework. In my project, I have a 'views' folder containing an 'index.html' file. However, upon trying to load the webpage, I encountered the following error: Error: Cannot f ...

"Using Mxgraph's getPrettyXml function does not retrieve the value of a custom

I’m having trouble with mxgraph’s getPrettyXml() not capturing the value of Custom elements. In my customized template, it looks like this: <add as="symbol"> <Symbol label="Symbol" description="" href="" data="{[hi :bill]}"> &l ...

Navigating jQuery Tabs by Linking to a Specific Tab in a Separate File

I am currently using Bootstrap 3 to develop a basic website. On the about.html page, I have set up Tabs with various content. <ul class="nav nav-tabs" id="TabSomos"> <li class="active"><a href="#somos" data-toggle="tab">About Us</a> ...

What is the best way to vertically align items within a <div> using CSS in a React application?

The layout of the page looks like this: https://i.sstatic.net/NGCNP.png I am trying to center the Redbox (containing '1') within the "PlayerOneDiv". Similarly, I want to center the yellow box within "PlayerTwoDiv". return ( ...

`Considering various factors to alter class pairings`

I have defined a few style classes in my stylesheet as shown below: .left-align{ /* some styles here */ } .right-align{ /* some styles here */ } .validate-error{ /* some styles here */ } Depending on the type of data, I need to align the content ei ...

issue with HTML forms not triggering correct function when submitted

UPDATE: I have encountered an issue with my HTML page containing two forms, each with a single submit button. I attempted to call a specific function when the submit button is pressed. To achieve this, I included two <script> tags to handle the butto ...

Toggle the visibility of multiple divs depending on a specific attribute value

I previously inquired about this issue, but I believe my wording was unclear and the responses focused on how to display or hide a group of divs when clicking a button. While I understand that concept, my specific challenge is slightly different. Let me pr ...

Incorporating a dropdown feature into the navigation bar

Greetings, I am currently learning on the job as I work on a new website for my family business. I am struggling to make the drop-down navigation menu function properly on both desktop and mobile. I want it to have a simple design similar to the main nav ...

Acquiring information to display in a div using innerHTML

I aim to create a div that displays different animal sounds, like: Dog says Bark Bark I've attempted various methods but I'm unable to get each pair in the array to show up in a div: const animal = [ {creature: "Dog", sound: "B ...

Designing a carousel-style menu list with navigation buttons for moving forward and backward

I'm running into some trouble while attempting to create a carousel. Firstly, the issue I am facing is that when you continuously click on the Next button, the function keeps working even after reaching the last item. I'm not sure how to make th ...

Change the position of a Div by clicking on a different Div using JQuery for custom movements

I have successfully managed to slide the div left/right based on the answers below, but I am encountering some issues with the top div. Here are the specific changes I am looking to make: 1. Make both brown lines thinner without affecting the animations. ...

Where is the best location to store the image files?

I am currently working on a web application that focuses on Image processing, however I have encountered a basic issue. The images are not loading during the runtime of my HTML page. I have tried placing the image files in the src folder, but it seems li ...

Achieving a scrolling body with a fixed header in a Vue b-table

Currently, I have implemented a b-table element on a webpage to display data from a database. The table is paginated, but feedback suggests that users prefer all information displayed in a single scrolling view. However, the issue arises when I attempt to ...

Choose the identical value multiple times from a pair of listboxes

Is there a way to use the bootstrapDualListbox() function to create an input that allows selecting the same value multiple times? I have been searching for a solution to this issue without success. If I have a list like this: <select multiple> <op ...

Incorporate SVG or HTML5 elements into a Box2D World

My SVG logo is primarily composed of elements. I am interested in animating it by placing it in a "gravity world". Although I am new to Box2D and Canvas, I have managed to convert my SVG into HTML5 canvas using canvg. Currently, I am going through the begi ...