When utilizing content: url() for a local image, it does not show up, whereas it will display for a web URL. What could be causing this discrepancy

I'm attempting to convert this image into a button. It works fine when acquiring the image through a website URL (not from my own website), but it doesn't work when using a relative path. The image just won't show up.

For instance:

.my-class {
     content: url(http://themedemo.wpeka.com/wp-content/themes/apppress/images/icons/included/color.png);
}

will function, however, the following example with the URL as a file path with the same downloaded image from the above URL:

.my_class {
     content: url(/img/color.png);
}

Leaving out or including quotes didn't lead to any success.

My directory structure looks like: -CSS->styles->general->style.css -color.png

So, for testing purposes, I placed color.png and the CSS folder at the same level within the project.

I did get it to work when I tried:

contents: url(../../../color.png);

but not:

contents: url(/color.ong);

EDIT: I feel silly now. Thanks everyone for your assistance - something finally clicked in my brain.

Answer №1

Assuming your website file structure looks like this:

CSS

www.YourWebsite.com/css/yourStyle.css

and your image is located here:

www.YourWebsite.com/pictures/yourPicture.jpg

Your relative path should be

../pictures/yourPicture.jpg

The ../ moves back one directory and then specifies the pictures/ folder.

It's important to mention that the pictures folder in the original example is /pictures/, while in the relative path explanation you used /img/

Answer №2

Please double-check the correct file path or consider removing the / from the file path and give it another try.. like this:

.my_class {
     content: url(img/color.png);
}

For more information on using forward slash, check out this article

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

Unusual SVG Cubic Bezier Animation Transforming Curves into Points and Lines

As a beginner in SVG CSS animation, I am currently working on morphing three curved paths into three rectangles using cubic bezier routes. While I have managed to make it work, there is an issue during the transition where parts of it skew inward in a stra ...

Getting elements in order with bootstrap

Can you help me align the "Format Example" string with the textual labels of the input fields without using static width in AngularJS with Bootstrap? The current alignment is right-justified but I would like it to be more dynamic as shown in the screenshot ...

Turn off automatic spelling correction and predictive text in a content-editable section

I'm currently working on a cross-browser application using Script#. I've incorporated a contenteditable div where users can input text. However, I am facing an issue with the auto correct/auto completion feature altering the user's text. Co ...

Nested pages are causing jQuery plugins to malfunction

I am currently working on a website, but I am facing some issues with the product listing pages and the tips and tricks page. It appears that there is an issue with jMenu and jFlipBook plugins not functioning properly. Since I didn't develop the origi ...

I am attempting to integrate the file path of my images using PHP

Let me start off by showing you the file structure of my website. File Structure Of my website In order to organize my code, I created a header file named header.html.php which contains all the necessary elements for the header, including an image tag fo ...

Can you explain the concept of collapse as it pertains to bootstrap?

I'm currently delving into bootstrap on Bootstrap's official website. The section on navbars caught my attention: Navbars are dynamic components that act as navigation headers for your application or website. They begin in a collapsed state (a ...

What is the process for triggering data-dismiss after the href has been activated?

Could someone help me with this issue? <a class='btn btn-danger' href='page.html'>Delete</a> I am trying to activate the "data-dismiss" attribute after the "href" is activated. My initial attempt was using this code: < ...

Tips for updating the content of a DIV element on a separate page

One question that often arises is how to change the content of a div element on a different page within a website. While it is common to use Ajax to dynamically update div content within the same page by fetching data from an external .html file, the proce ...

Anomalies encountered during the iteration of a table

As I work on building a table by looping through an API array, I've encountered a few obstacles. Here is the code snippet that's causing me trouble -> $html = " <tr class='mt-2'> <td>{$rank}.</td> ...

What is the best way to identify the appropriate element to display based on the anchor inside the element?

Incorporating various Twitter Bootstrap collapse groups on my website is a key feature. These groups consist of multiple anchors: <div class="accordion" id="accordion"> <div class="accordion-group"> <div class="accordion-heading" ...

Angular template src variable issue with no solution in sight

The videoSrc variable is not evaluating correctly images/{{videoSrc}}.mp4 When I write just videoSrc, it works fine. But when I concatenate it with other strings, it doesn't work. Check out this jsfiddle ...

Converting Markup Language to PDF or HTML

Are there any markup languages that work seamlessly with a popular .NET open source project to produce highly customized PDF or HTML documents with precise control over styling and positioning? These documents will consist of a combination of static conte ...

Why can't we use percentages to change the max-height property in JavaScript?

I am currently working on creating a responsive menu featuring a hamburger icon. My goal is to have the menu list slide in and out without using jQuery, but instead relying purely on JavaScript. HTML : <div id="animation"> </div> <button ...

When the user clicks on the element, the onclick function captures the input data from

I'm currently learning JavaScript and attempting to redirect to a different page based on user input. /MenuDemo1/src/home.jsp After filling in the two text boxes above and clicking the button, I manage to retrieve the username and password. However, ...

What are some strategies for disregarding global CSS styles?

I am facing an issue on my html page with a specific div structure. The <div id="container">/* lots of nested html with elements like div, ul, li etc */</div> is causing some trouble in my global css file called style.css. The style. ...

Exploring jQuery: Techniques for Hovering, Changing, and Toggling Images

Currently, I am busy working on my project and I am attempting to achieve this by... I ideally want everything to be operational through click actions for each individual image, allowing them to have their unique "paper". I am aiming for a hover effect an ...

Preventing HTML Theft

Currently, I am in the process of creating a website and one of my main concerns is protecting the code from potential theft. My goal is to implement measures that will prevent users from extracting the code from the site, and trigger an error message if ...

Autofocus Styling with CSS Bootstrap

Currently, I am working on creating a grid of large buttons using React and Bootstrap. I have implemented CSS for hover and focus effects on these buttons. My main issue arises when I try to load the screen with one button already focused. I attempted to ...

Using AJAX for initiating Liquid code upon page load

I was trying to create an onClick function that would add liquid code to the div when a certain link is clicked. However, instead of adding both the liquid code and its content, it was only adding the code itself. Here's the code snippet I used: Inde ...

After installation, the Tailwind classes seem to be malfunctioning

Let me start by mentioning that although the title of this question is reminiscent of this other question on Stack Overflow, I have already tried the solutions provided there with no success. I initially attempted to set up Tailwind using their official C ...