CSS - Image in the background is not displaying, however the background color is functioning perfectly

Recently, I made some alterations to my HTML-code by introducing external objects in place of certain items such as navigation and footer. However, since making these changes, I have encountered an issue where background images cannot be loaded into the 'collage' section of my website.

In a simplified version of the HTML code, the main focus is on the container which acts as a wrapper for the entire content. The central element of the website consists of multiple images that are linked to respective websites. Upon hovering over these images, a brief description along with a title is displayed.

The .collage class is utilized for styling the overall frame of the element, while the id attribute is intended for adding background images (which was functional before).

Snippet of HTML Code:

<!DOCTYPE HTML>

<head>
   <link rel="stylesheet" type="text/css" href="styles/main.css">
   <title>Homepage</title>
</head>

<html>
    <body>

    <div id="container">

        <article>       
            <div class="collage" id="cg">
                <a href="#">
                    <div class="text">

                        <h2>CG Projects</h2>
                        <p>
                            Computer-generated projects created using the open-source software 'Blender', along with tutorials for some of these projects.
                        </p>
                    </div>
                </a>
            </div>

        </article>

    </div>

    </body>
</html>

The initial part can be used for general purposes.

body: Setting the font-size at 100% for scaling all em elements with one value (optimized for mobile).

#container: Positioned in the center, darker than the overall background, with basic styling.

article .collage: Displayed as table to facilitate proper functioning of image hover-over effect (currently working correctly).

#cg: This specific part is causing issues. While I am able to modify the background-color using this tag, attempts to change the background image have been unsuccessful.

.text parts: Design elements for the hover-over functionality, utilizing opacity to keep them invisible until hovered upon.

CSS Styling:

*{
    margin: 0;
    padding: 0;
}

body{
   font-size: 100%;
   background-color:#2B2B2B;
}

#container{
margin: 0 auto;
margin-top: 100px;
min-height: 50em;
min-width: 70em;
max-width: 80em;
background-color: #2A2A2A;
border: 2px solid white;
}

article .collage {
display: table;
height: 500px;
width: 700px;
margin: 100px 0 0 5px;
border: 1px white solid;
}

#cg{
background: url("cg_collage.jpg");
}

article div .text{
    height: 400px;
    width: 800px;
    background: rgba(0,0,0,0.55);
    position: relative;
    top: 0;
    left: 0;
    vertical-align: middle;
    display: table-cell;
    opacity: 0;
    transition: all ease-in 0.1s;
}
article .collage a{
    text-decoration: none;
}

article .collage .text{
    padding: 0 10px 0 10px;
}

article .collage .text h2{
    text-align: right;
    text-decoration: none;
    color: #ADFF5C;
    line-height: 70px;
    font-size: 40px;
    font-family: monospace;
    border-bottom: 3px ridge #FFFFFF;
    line-height: 50px;
}

article .collage .text p{
    text-align: right;
    font-family: Arial, Helvetica, sans-serif;
    margin-top: 5px;
    color: #ADFF5C;
}

article div:hover .text{
    opacity: 1;
}

Folders

https://i.sstatic.net/ZL7bS.jpg

To summarize, changing the background color works seamlessly and the hover-over feature operates perfectly. The only challenge lies in loading the background images. When directly embedding images in the HTML file, everything functions as expected.

If anyone can provide insights or spot the elusive 'Error 30', it would be greatly appreciated! :)

Answer №1

It is important to note that the issue with your paths lies in the location of your CSS file and image reference. If your CSS file is stored in a folder named "styles," but your image is not located in the same folder, the path will be incorrect.

Instead of using ".." to navigate out of a folder, it is recommended to use relative paths for better flexibility. By starting the path with "/", you instruct the file(s) to begin from the root directory which ensures accuracy even if files or folders are moved.

For example, instead of

background: url('../cg_collage.jpg')
, consider using
background: url('/cg_collage.jpg')
. This way, there is no need to calculate the number of directories to move back, simplifying the process.

Another tip is to organize images into a specific folder with an appropriate name like "img" or "images" for easy access.

For more information on absolute and relative paths, refer to this helpful article on absolute and relative paths.

Answer №2

Make sure to update your CSS file:

#cg{
  background: url("cg_collage.jpg");
}

Should be changed to:

#cg{
  background: url("../cg_collage.jpg");
}

The reason for this change is that the image is located outside of the styles folder where your stylesheet resides.

Answer №3

It seems like CSS is searching for the image file within its designated directory (styles). You might want to attempt the following:

#cg{
    background: url("../cg_collage.jpg");
}

Answer №4

Your code is functioning flawlessly, assuming the file named cg_collage.jpg can be found in the /styles directory. Have you verified its presence within your project?

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

Having difficulty controlling the DOM within an AngularJS template

My website utilizes AngularJS templates, specifically a Single Page Application (SPA). Within one of the templates, I am utilizing a tab control from the AngularUI library named Ui-Tabset. During the rendering process, this control generates a ul element ...

"Enhance your form design with a Bootstrap Horizontal Form label paired with a subtle

How can I add a caption right below the Order label in this Bootstrap Horizontal form? The caption should be <small class="text-muted">Must be at least 200</small>. Where should it be placed? Please note that I need to place this muted caption ...

Rails encounters problems when assets are modified

I recently inherited a small Rails website. When I attempted to make a change to a css file, I encountered an error page (code 500) in Rails with the following message: No such file or directory - /.../cache/assets/sprockets%2F1450c8f5d2b6e201d72fa175586b ...

Determining the presence of an HTML element in the correct manner

Is there a correct method for verifying the existence of an HTML element on a webpage? This is how I usually approach it: if (document.getElementById('container') !== null) {doThis()} What are your thoughts on this approach? Are there alte ...

PHP-generated image of a security code

I am utilizing sessions to generate an image through a seccode.php file, with the image source set as 'seccode.php' on my sign-in page. While this method typically works fine, occasionally users attempting to use the sign-in page multiple times m ...

Calculating the Distance Between Elements within a Flex Container Using JavaScript

I am looking to calculate the margin left and right for children of a flex item <div class="sec images" style="display:flex;justify-content:space-between"> <img src="images/en_mb-mega-01.png" alt=""> ...

When you hover over an image, both the image's opacity and the color of the title beneath it change. However, if the title expands to two lines, it messes up

I am currently working on a project where I have an image and a title displayed below the image. My goal is to change the color of the text when hovering over the image, as well as adjust the opacity of the image. Additionally, when hovering over the title ...

Implementing a download hyperlink attribute for a button with jQuery

How can I create a jQuery function for a button? <input type="submit" id="submit"></input> Once clicked, it should trigger the following action: <a download href="file.doc"></a> <script> $("#submit").click(function(){ ...

Transitioning Opacity in CSS

I found inspiration for my project from the following source: https://codepen.io/plavookac/pen/qomrMw One challenge I'm facing is changing the opacity of the movement, which is not covered by the standard transition markers. .mainInner{ display ...

Ways to arrange two divs side by side and ensure that the content remains within the respective divs

Although I have searched for a similar solution, I haven't found an exact scenario that matches my current issue. Here is the problem: I have a div on the right (with a fixed width), and the entire left side is occupied by another div. While I can pos ...

immediate removal upon pressing the button

Struggling to remove data from a datatable when clicking the delete button for quick admin side action. My code isn't functioning properly, even after attempted fixes. Here's my code: <div class="table-responsive"> <table id="datas" cl ...

Layering one canvas on top of another

Here is the code I am working with. With a JavaScript library, I can draw on the first canvas and then merge it onto the second canvas. My question is, how can I make the first canvas stay in place or float, regardless of where I scroll on the second canv ...

Pressing the dart key will alter the sprite's direction in a 2D game

Recently, I've been working on a Dart game where the user controls a space ship sprite using the W, A, S, and D keys. Pressing these keys will move the sprite up, left, right, and down respectively. Additionally, pressing the Space bar will launch a p ...

What's the difference between using find_all on a bs4.element.ResultSet object and a list in Beautiful Soup

When I use the find_all method on a beautifulsoup object, it returns either an bs4.element.ResultSet object or a list. However, I am unable to directly apply the find_all method on the bs4.element.ResultSet object. One way to work around this is by loopin ...

What is the process for loading an HTML form into a specific target using Angular?

Is there a way to load an HTML form into a target in Angular without the use of jQuery? I have a working script, but I am currently stuck on: <html> <head> <script src="components/angular/angular.js"></script> <script&g ...

Extract the color of an individual character

There is a code snippet in JavaScript using p5.js that functions as a video filter: const density = ' .:░▒▓█' //const density = ' .tiITesgESG' //let geist; let video let asciiDiv let playing = false let ...

Using grid system within the <nav> tag: a comprehensive guide

I've been working on incorporating the grid system with row and col using bootstrap 4.0, but I've run into an issue. When I use the <div> tag, everything seems to be working smoothly: <link href="https://maxcdn.bootstrapcdn.com/boots ...

Modal obstructing BsDatePicker

<!-- The Server Modal --> <div class="modal" id="serverModal"> <div class="modal-dialog" style="max-width: 80%;overflow-y: initial !important;" > <div class=" ...

error being triggered due to relative positioning

On my webpage, I have a set of buttons located at the bottom. When I first open the page, all the buttons are displayed properly. However, sometimes the first three buttons become hidden mysteriously and the layout changes. I am puzzled as to what could be ...

Is there a missing clear button for the search input type in Firefox?

<input type="search" /> input[type="search"] { -webkit-appearance: searchfield } Is there a way to display the clear(x) button in Firefox when something is typed into the input field? Any insights would be greatly appreciated! ...