How to set a background image using a lengthy URL in CSS

I am attempting to add a background image through a URL, but the image is not showing up. Here is the code I am using:

<div style="background-image:url(https://i.amz.mshcdn.com/pr8NZRyZf0Kmz4FSGMkLtxUwZ70=/575x323/filters:quality(90)/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fstory%2Fthumbnail%2F65469%2F23ede933-7d84-40fb-a9ee-eb7787a3feac.jpg);">content goes here...</div>

I have verified that the URL works by pasting it into a browser where the image shows up. However, it does not display as a background when used in my code. My WordPress site automatically fetches URLs for background images, so I am confused why this is not working. Any suggestions on how to solve this issue would be greatly appreciated.

Answer №1

It is important to include quotes when working with special characters in URLs. While it may not always be necessary, in certain cases like yours, it can prevent confusion.

For instance, having a closing ) within the URL could prematurely end it.

<div style="background-image:url('https://i.amz.mshcdn.com/pr8NZRyZf0Kmz4FSGMkLtxUwZ70=/575x323/filters:quality(90)/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fstory%2Fthumbnail%2F65469%2F23ede933-7d84-40fb-a9ee-eb7787a3feac.jpg');height:200px;">content goes here...</div>

UPDATE

If you are unable to manually add quotes to the URL (e.g., due to automatic generation with WordPress), consider using this jQuery solution that targets a specific div element.

//change the selector to get only the needed DIV
var sel = $('div')

var url = sel.attr('style');
url = url.replace("url(","url('"); //add the first quote
url = url.replace("jpg)","jpg')"); //add the last quote

sel.attr('style',url);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="background-image:url(https://i.amz.mshcdn.com/pr8NZRyZf0Kmz4FSGMkLtxUwZ70=/575x323/filters:quality(90)/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fstory%2Fthumbnail%2F65469%2F23ede933-7d84-40fb-a9ee-eb7787a3feac.jpg);height:200px;">content goes here...</div>

Note that this method is specific to this scenario and may require modifications for other situations.

Answer №2

Check out the official specification:

Certain characters in an unquoted URI need to be escaped with a backslash for the URI value to be valid, such as parentheses, white space, single quotes ('), and double quotes ("). Example: '(', ')'.

You have a few options:

  • Escape those special characters
  • Use a quoted url() like in the example below
  • Avoid using URLs with those problematic characters

    <div style="background-image:url('https://i.amz.mshcdn.com/pr8NZRyZf0Kmz4FSGMkLtxUwZ70=/575x323/filters:quality(90)/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fstory%2Fthumbnail%2F65469%2F23ede933-7d84-40fb-a9ee-eb7787a3feac.jpg');">content goes here...</div>

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

html tables cannot be aligned row by row

I am attempting to display messages row by row and then show a text box for input to send a message. The issue I am facing is that I can display the messages row by row, but the text box appears next to the displayed messages horizontally rather than at th ...

Eliminate the excess padding from the Material UI textbox

I've been struggling to eliminate the padding from a textbox, but I'm facing an issue with Material UI. Even after setting padding to 0 for all classes, the padding persists. Could someone provide guidance on how to successfully remove this pad ...

Struggling with ajax: Converting a JavaScript variable to a PHP variable

I'm trying to convert a JavaScript variable into a PHP variable in order to use it in an SQL query, but for some reason it's not working as expected. Here is the HTML code: <select id = "dep_ID" name = "dep_ID" onchange="myFunction()"> A ...

Is there a method in AngularJS to submit form data when the input fields are pre-populated using a GET request?

How do I submit form data in AngularJS? I have a div that populates the input field from a GET request. Now, I want to send this data using a POST method. How can I achieve this? Below is the form div: <div class="row" ng-app="myApp" ng-controller="myC ...

table rows are styled with hover effects and alternating colors using CSS

Styles for even and odd table rows are set, but hovering over the table rows is not working. Test it out here: http://jsfiddle.net/w7brN/ CSS style : #table_even tr:hover { background-color:#fffbae!important; } /* hover effect */ #table_even tr:nth-ch ...

How can we have a div stay at the top of the screen when scrolling down, but return to its original position when scrolling back up?

By utilizing this code, a div with position: absolute (top: 46px) on a page will become fixed to the top of the page (top: 0px) once the user scrolls to a certain point (the distance from the div to the top of the page). $(window).scroll(function (e) { ...

Display a PNG image in React with a sleek and stylish transparent background

Whenever I try to set an image with the following CSS properties: width: 100%; height: 100%; background: url(example.com/image.png) no-repeat; background-size: cover; The background appears as white. Is there a way to display it with grey ...

Create an interactive HTML5 drag-and-drop interface that restricts the dropping of nested elements to only within form fields. This functionality is achieved

I have implemented Sortable.JS in a form builder to enable drag and drop functionality. My goal is to allow users to drag sections (which is mostly working) and questions within those sections, either within the same section or into other sections. The is ...

I am experiencing a problem trying to send an email query to multiple email accounts using PHP

I'm encountering a problem when trying to send an email query to multiple email accounts using PHP. Can anyone offer assistance? Here is the HTML form version: <form id="contact-form" action="#"> <div class="form-field-item"> ...

Prevent jQuery UI dialog from adjusting its position relative to the browser when scrolling

When I launch a jQuery UI dialog, the position of the dialog changes when I scroll the browser. How can I make it remain in the same position relative to the browser window? ...

Tips for positioning div elements with css tables

Is it possible to align the div elements in such a way that the first column element spans two rows, while the second column elements are stacked on top of each other using CSS tables? Below is a snippet of my HTML code: <html> <head><link ...

Loop through a nested array and output the playerId, playerName, and playerCategory for each player

update 3: After thorough debugging, I finally found the solution and it has been provided below. let values = { "sportsEntitties": [{ "sportsEntityId": 30085585, "sportsEntityName": "490349903434903490", "sportsEntityStartDate": "7878 ...

Issues with AngularJS radio button functionality

Hey there, I am currently using AngularJS in my project. However, when I attempted to add a span to one of my input fields, all the radio buttons related to it stopped functioning. Can anyone offer some suggestions on what might be going wrong here? < ...

The jQuery function is running double even after I cleared the DOM with the empty() method

Twice, or multiple times if going back and forth, the Jquery function is triggered. Upon loading LoginMenu.jsp, WorkOrder.jsp is loaded within a specified ID. Once WorkOrder.jsp loads, it then loads schedule.jsp in the schedule tab defined in WorkOrders.j ...

Do overlay elements have to be positioned at the bottom of the HTML body every time?

As I delve into the world of JavaScript frameworks, one common trend I've noticed is that elements like dialogs, tooltips, and alerts usually appear at the end of the body tag. With my own implementation of these UI elements, I am determined to make ...

Retrieving Information From a Targeted div Identifier with PHP

Possible Duplicate: read XML tag id from php How can I retrieve data from a specific div ID using PHP? I want to extract data from a div with the ID of <div id="content">, so that all the content within that div can be stored in a variable. Alt ...

What is the best way to establish a maximum limit for a counter within an onclick event?

I'm struggling to figure out how to set a maximum limit for a counter in my onclick event. Can someone help me? What is the best way to add a max limit to the counter of an onclick event? Do I need to use an if statement? If yes, how should it be w ...

Tips for creating a fixed sidebar menu that is aligned to the right side of the page

I've been working on this for quite some time now and I'm trying to figure out how to keep a side menu in place, while having it float on the right side. Every time I try using the fixed element, it ends up moving my sidebar from right to left. A ...

Trying to align a Material UI button to the right within a grid item

I'm attempting to right float the button below using material-ui, but I'm unsure of how to achieve this: <Grid item xs={2}> <Button variant="contained" color="secondary&quo ...

Is there a specific HTML tag available to instruct the browser to cache my HTML and/or CSS files for future use?

Up to this point, my research has indicated that enabling JavaScript and CSS browser caching typically requires server side settings such as .htaccess. Is there any HTML tag or configuration within the HTML page or JavaScript that can instruct the browse ...