Updating spacing in JSX for CSS styling

Is there a way to remove white space from an object field in order to display it as a background image in CSS using JSX?

renderImage(singleMedia, type) {
  let mediaType = singleMedia.mediaType;
  if(type == "image")){
    var mediaPreview = singleMedia.mediaPreview.replace(/\s+/g, '%20');
    console.log("The original image was", singleMedia.mediaPreview, "The NEW ONE IS", mediaPreview);
    var divStyle = {
        backgroundImage: 'url(' + mediaPreview + ')'
    }
  }

 return(
        <div className="image-logo" style={divStyle}> </div>
    );
 }

While I can render the image successfully if mediaPreview contains no spaces, I am still unable to view it even after adding %20.

Upon running my code, the following results appear: (However, the output remains blank. The image only displays when sent without any white space)

The original image was http://api.testing.ca/media/ad/no-smoking-sign (1).png The NEW ONE IS http://api.testing.ca/media/ad/no-smoking-sign%20(1).png

I'm curious if there is a way to strip the white space and convert it to %20 within the divStyle itself.

Answer №1

The issue lies not in the presence of whitespace, but rather in the inclusion of the end parenthesis in the file name. To address this, instead of replacing spaces with %20, substitute the closing parenthesis with %29:

var mediaPreview = singleMedia.mediaPreview.replace(/\)/g, '%29');

According to the CSS specification, the url token concludes upon encountering the first non-escaped ).

Alternatively, you have the option to encapsulate the entire URL within quotation marks:

var divStyle = {
  backgroundImage: 'url("' + mediaPreview + '")'
}

This approach would necessitate escaping any existing quotes within the string.

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

Customize typography and color palette across all components with the MUI 5 theme

My goal with MUI 5 is to utilize ThemeProvider and createTheme to establish a consistent font-family and color scheme for all MUI components in use. Here is the specific MUI installation I am working with: npm install @mui/material @mui/styled-engine-sc s ...

Troubleshooting React: Issues with editing textboxes and submitting data as an array

I have a list displayed and you can access the sandbox here: https://codesandbox.io/s/solitary-butterfly-4tg2w0 Unable to edit textboxes. The description-id serves as the primary key. When making a Post API call, how can we save changed textbox values wit ...

"Resolving the issue of overflow in dropdown submenus using Css

Observing my CSS menu in the provided image: https://i.sstatic.net/cJMge.jpg Upon hovering over "Haschildren," I notice: https://i.sstatic.net/gJ5M8.jpg The concern arises regarding the overflow issue (grey area around sub-voices). Despite tweaking the u ...

Challenges arise with dynamic tables (Foundation 5) within Rails application

I have tables set up in my Rails 4 App with the ZURB Foundation 5 framework. The problem lies with the mobile version of the table. It works fine on browsers and tablets, but on mobile phones, it shows the first column twice before scrolling through the r ...

mismatch between margin-left and auto not aligning

Having trouble with margin-left: auto not working 1- Looking to create a header with an image and a navbar 2- Want two unordered lists in the navbar 3- Trying to position one list on the left side and the second on the right side When I use margin-left: 8 ...

Utilize Drag-and-Drop feature to dynamically generate columns upon dropping the element

I am currently utilizing react-beautiful-dnd to copy and drop an element into pre-existing columns. However, I am curious if there is a way for me to dynamically create a column when dragging an element and dropping it. Is this possible? If react-beautif ...

Can a person select a characteristic like "height" using Javascript?

Is it doable to set a height for an image in CSS, then detect this gradient using JS and double the width based on the height x2.25? Could this be achieved? ...

Unoccupied whitespace created by using Bootstrap

I'm experiencing a challenge with the code below in terms of large spaces being left when columns are collapsed. Is there a way to eliminate these spaces? body { font-family: sans-serif; max-width: 100%; overflow-x: hidden; } .poster { heig ...

Organize items without consideration of their dimensions

I am in the process of creating an alphabetized list of categories, from A-Z. I am utilizing ul and li elements to achieve this. My goal is to have the elements displayed consecutively, regardless of their height. While my current code successfully arrange ...

jQuery UI Accordion - Adjusting Panel Height to Content Size

Currently, I am utilizing jQuery UI's Accordion feature from http://jqueryui.com/demos/accordion/, and my goal is to adjust it so that it resizes based on the contents of each panel rather than just fitting the largest one. In addition, I am seeking ...

CSS tooltip box with embedded HTML for interactive content display

After doing some research, I came across a code that almost does what I need. The code allows me to hover over text and display a tooltip box with HTML content such as images and links. It worked perfectly with jQuery, but the challenge is that eBay doesn ...

Managing state in React using useEffect and useState - Avoiding undefined values

Encountering the same undefined error in various components. After verifying that the data is coming from the API, not blocked by CORS, and accessible in the useEffect callback, it appears that the issue lies in setting the state. I attempted to replace th ...

Tips for aligning an image and paragraph in the center of a table row using Material UI

I designed a table using material UI that includes images and paragraphs in the same row. However, I am facing an issue where they do not align at the same height as required for them to be centered in the row. Currently, the view shows a mismatch in heig ...

Tips for making a DIV span the entire width of a page when it is within a column with the Bootstrap class 'col-md-6'

I'm having a little trouble arranging divs next to each other on a page. Specifically, I want to ensure that if there is only one instance of the col-xs-12 col-md-6 div class on the page, it should take up the full width (100%) instead of just 50%. Th ...

Changing the Image Path According to the Device's Retina Ratio

I'm currently setting up a website for a photographer and working on creating a gallery. To streamline the process, I have automated many aspects such as file names, paths, widths, lightbox classes, etc. All I have to do in the HTML is write an <a ...

Place the logo/brand on the right side

I am attempting to position the brand/logo on the right side of my navbar, but I am facing an issue where the logo remains inside the collapse menu when viewed on mobile devices. Here is the correct positioning in desktop view: https://i.sstatic.net/ZNxm9 ...

Personalize your Slick.js

I want to customize the navigation buttons for my Slick.js jQuery slider by replacing the default dots. Since I'm not very experienced with jQuery, I need help writing code that will sync the slider with my custom buttons. ...

The message from create-react-app states, "To use Create React App, you must have Node version 14 or higher installed." However, when attempting to update Node, an error message pops up saying, "npm does not work with

I encountered an issue while attempting to develop a react app, and received the following message: $ npx create-react-app react-demo npx: installed 67 in 6.045s You are running Node 10.19.0. Create React App requires Node 14 or higher. Please update your ...

The Apollo mutation is not showing up in the props list

Take a look at my component below: import React, { Component } from 'react'; import { gql, graphql, compose } from 'react-apollo'; export class ExampleComponent extends Component { render() { console.log("Checking this.props: " ...

Problem with the spacing in the Bootstrap website after the breadcrumbs due to flexbox issues

I'm facing a challenge with one of my Bootstrap-based pages that I can't quite figure out. We recently implemented breadcrumbs for navigation, which is also built with Bootstrap. However, some pages are displaying a significant gap between the br ...