The CSS background-image property is not displaying the desired image

Hello there! I'm a beginner in web development and currently working on a small website using HTML, Bootstrap, and CSS. However, I'm facing an issue with the background-image property in my CSS file. I've double-checked the path and the file format, and everything seems to be correct (even when using Brackets IDE, the image thumbnail shows up when hovering over the path). Here's a snippet of my code:

HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="styles.css">
    <meta charset="UTF-8">
    <title>My First Web Project</title>
</head>

<body>
 <!-- NAVIGATION HEADER -->

  <div class="header" id="topheader">
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container text-uppercase p-2"> 
           <a class="navbar-brand" href="#">MY WEBSITE</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li>
                    <li class="nav-item"> <a class="nav-link" href="#">ABOUT US</a> </li>
                    <li class="nav-item"> <a class="nav-link" href="#">SERVICES</a> </li>
                    <li class="nav-item"> <a class="nav-link" href="#">CONTACT US</a> </li>
                </ul>
            </div>
        </div>
    </nav>
      <!-- BODY SECTION -->
   <section class="header-section">
       <div class="center-div">
           <h1 class="font-weight-bold">Lorem ipsum dolor sit</h1>
           <p>Hello we are developing this site</p>
       </div>

       <div class="header-buttons">
           <a href="#">EXPLORE</a>
           <a href="#">CONTACT</a>

       </div>

   </section> 
</div>


    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cecf3ececf9eeb2f6efdcadb2adaab2ac">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>

</html>

CSS:

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

.header
{

    background: url('../images/background.jpg');

}

I appreciate any help you can provide!

Answer №1

Have you taken a look at your browser's DevTools (simply right click on the page and select Inspect or press CTRL+SHIFT+I in Chrome) to check for any CSS overrides?

You could also try changing the URL by switching it to the following:

background-image: url('images/background.jpg');

If you've attempted using a URL directly from the web as suggested, but still encountering issues, consider investigating if there are conflicting styles overriding your CSS. You may want to add !important to your CSS line to see if that resolves the problem.

In some cases, I've found that converting the image format from JPEG to JPG in the CSS resolved the issue. Take a look at your browser's Dev Tools to assess the situation. Hopefully, these suggestions will assist you!

Answer №2

Update your CSS stylesheet with the following code snippet:

.header
{
    background-image: url('images/background.jpg');
}

Answer №3

For a more visually appealing design, it is recommended to specify both the width and height of a div element that contains a background image with a cover effect.


    .header
    {
        background: url('images/background.jpg');
        height: auto;
        width: 100%;
        background-size: contain;
    }

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

Problem with responsive design on iPhone

I'm currently working on developing a responsive chatbot using CSS Bootstrap. However, I've encountered an issue where the header and footer are not fixed when I open the app on an iPhone. The keyboard header is also moving up the screen, which s ...

What is the best way to ensure that the input search bar, microphone icon, and two small boxes adapt to different screen

Upon submitting my exercise for The Odin Project, I was under the impression that everything looked perfectly aligned and centered on my MacBook Pro 15-inch screen. However, when I viewed the completed webpage on a larger computer screen at work, I noticed ...

Tips for resolving an issue with an overflowing Uber React-Vis multicolor bar chart

I am trying to create a multi-colored bar chart using Uber's react-vis library. However, I am encountering an issue where the leftmost bar of the chart is overflowing below the YAxis instead of remaining contained to the right of it. You can view the ...

What is the best way to position three SVG files in the center of a webpage?

I want to combine three separate SVG files to create one single graphic. The first SVG file is a red triangle, the second is a blue circle inside the triangle, and the third is a purple rectangle beneath the triangle with a little space in between. My obje ...

Unable to import JavaScript file from "static" directory in Flask environment

Having trouble importing mainfirst.js onto my HTML page. I've attempted the following methods: <script src="../../../static/assets/js/main.js"></script> <script type="text/javascript" src="{{ url_for('static',filename=' ...

transforming JSON information into tables on a webpage

Can someone help me with the challenge of incorporating a massive JSON file into an HTML table? I am encountering an issue where I continuously receive the error message Uncaught TypeError: v.forEach is not a function. Any guidance would be greatly appreci ...

Executing code upon the completion of jquery's slideUp() function

Is there a method to trigger the execution of html() only after the completion of slideUp()? <p class="test">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tortor elit</p> $(".test").slideUp().html(""); I attempted using ...

Add a style to every div except for the final one

I've attempted to add a unique style to all divs with the same class within a parent div, except for the last one. Strangely, my code doesn't seem to work as expected for this particular case. Can anyone spot what I might be overlooking? Right no ...

executing a controller method in AngularJS

Looking to trigger a controller function from a directive tag. Check out this demo: https://jsfiddle.net/6qqfv61k/ When the 'Export to Excel' button is clicked, I need to call the dataToExport() function from appCtrl since the data is ready for ...

Preserve the original position of the inner <div> when animating the container <div> using JQuery

I am currently working on a small website and encountering some challenges with jQuery animation. My issue involves placing a single character text inside a circle and wanting it to grow when the user hovers over it, while keeping the inner text in its ori ...

Making an element sticky within a container while the parent element is set to overflow hidden

Here is a case where I have generated HTML and CSS code that includes classes for overflow, container height, and sticky positioning: .overflow-hidden { overflow: hidden; } .overflow-x-auto { overflow-x: auto; } .container { max-height: 100px; ...

"Can you explain the method by which reveal.js adjusts the size of

I have been exploring the resizing behavior of elements in reveal.js and trying to understand how they dynamically adjust. If you resize the page height, you can observe that the elements shrink up to a certain extent along with the page. However, when in ...

Interacting with dropdown menus on webpages through select box events

I am looking to create a unique scrollable drop-down menu on a webpage that includes select box features like hover on key press. For instance: Within my drop-down menu, there are approximately 100 elements, Apple Ant Ball Orange .... The menu should be ...

Which child node of the html tag represents the "text"?

In my quest for answers, I have searched extensively but to no avail: As we all know, when a web page is loaded in a browser, it generates a tree-like structure called the Document Object Model (DOM). This allows Javascript to manipulate the elements of t ...

Adding a background image to a box in MUI is a simple task that can enhance

I've been attempting to include a background image in the Box component of mui, but I can't seem to get it to work. Here is the code I've been using: const Main = () => { return ( <Box sx={{backgroundImage:'images/cove ...

Ever since implementing a new section class, the CSS first-of-type selector has ceased to function

After referencing a previous response, I utilized first-of-type to specifically target the initial menuSection with this code snippet... .menuSection:first-of-type { background: red; } <div id="container"> <section class="menuSection"> ...

"Arranging elements with identical class in a single column but on separate rows using CSS grid - a step-by-step guide

I'm facing an issue with organizing choices and matches in columns using grid CSS. Currently, the match column is overlapping with the choice column. Here is how it is coded at the moment: .grid{ display:grid; grid-template-columns: 1fr 1 ...

Is there a way to eliminate the gap between two horizontal rule tags?

What causes the space between two <hr> tags to remain? Despite setting the width of the <hr> tags to 49%, there is still a gap between them. How can this space be removed from the <hr> tags? Shown below is the HTML and CSS code: *{mar ...

Is it possible to include edit links to local files in an HTML report that I am creating?

Looking to enhance the readability of py.test reports that look like this: self = <test_testcenter_views.TestSiteViews testMethod=test_testcenter> def test_testcenter(self): "Test the tctr_update view." > r = self.client.get(&a ...

What is the best way to display the tabcontent when clicking on a menu tab in this code, as neither method seems to work?

Take a look at my JSFiddle demo: <http://jsfiddle.net/xrtk9enc/2/> I suspect that the issue lies within the javascript section. My html code includes an Unordered List that serves as the tab menu. Each href of the menu corresponds to a tab content, ...