Creating components with custom backgrounds using inline styles in JavaScript with base64 encoding

I have a basic block with a base 64 background:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <style>
        #footer {
            background: #00b2d6 url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQoAAAEOCAMAAABGhKntAAAAM1BMVEUAAAAHpswGpssFpMkHpssAn88GpcsGpcwHp8sHpcsHpMwHp88HpswGpswGpcwHpcsHpszyVnbHAAAAEHRSTlMAz58w7xDfv0CAYCCvcFCPOTrUqgAAKt9JREFUeAHsWgmf+...

Can React handle this with inline styles? How can I create a component with a similar background?

var footerStyle = {
  backgroundImage: ...  ,
  backgroundSize: 'cover',
  backgroundRepeat  : 'repeat',
  textAlign: 'center',
  padding: '100px',
  fontSize: '50px',
  boxSizing: 'border-box'
};

const Footer = () => <div style={footerStyle}>Discover unlimited possibilities</div>;

Answer №1

To achieve the desired effect, your first snippet can be modified to use the background property instead of backgroundImage, as you are specifying a fallback color too. Alternatively, you can separate these into backgroundColor and backgroundImage respectively.

Make sure to wrap the entire background attribute in single-quotes (') or backticks (`) since the url is wrapped in double quotes:

var footerStyle = {
  background: '#00b2d6 url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQoAAAEOCAMAAABGhKntAAAAM1BMVEUAAAAHpswGpssFpMkHpssAn88GpcsGpcwHp8sHpcsHpMwHp88HpswGpswGpcwHpcsHpszyVnbHAAAAEHRSTlMAz58w7xDfv0CAYCCvcFCPOTrUqgAAKt9JREFUeAHsWgmf+6AK9EARz/n+n/b9UtvatLLd8938z91toowwDCTm//Z/+7/9z5pnILpu/m+Fb
...', 
  backgroundSize: 'cover',
  backgroundRepeat: 'repeat',
  textAlign: 'center',
  padding: '100px',
  fontSize: '50px',
  boxSizing: 'border-box'
};

const Footer = () => <div style={footerStyle}>Discover unlimited possibilities</div>;
 
ReactDOM.render(<Footer />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

Answer №2

One approach is to utilize the img tag within a React component.

import * as React from "react";

export const Image = () => {
  return (
    <div>
      <img src={"data:image/gif;base64,<data>"} />
    </div>
   );
}

ReactDOM.render(<Image />, document.getElementById("root"));

Answer №3

To achieve the same effect in CSS, simply replace double quotes with single quotes:

let headerStyle = {
    backgroundImage: 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ [... (edited)] ORK5CYII=")',
    backgroundSize: 'cover',
    backgroundRepeat  : 'repeat',
    textAlign: 'center',
    padding: '100px',
    fontSize: '50px',
    boxSizing: 'border-box'
};

const Header = () => <div style={headerStyle}>Unlock endless creativity</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

Tips for creating a header.php and footer.php integrated with CSS files

I am in the process of constructing my website using HTML files. I want to make sure that my header and footer sections are dynamic so I can easily modify them without having to update numerous files each time. While I've attempted a few methods based ...

Exploring the combination of React, Redux, and Saga for server-side rendering, along with addressing the challenge of preventing unnecessary AJAX calls on a page

Currently, I am engaged in a web development project that incorporates server-side rendering using Node.js, Express, and React. To handle data fetching and state management with Redux, we utilize Redux-Saga. However, we are facing an issue where our applic ...

Creating React Components

When I input the command npm install --save-dev create-react-component-folder, the script runs but it seems like the folder is never created. Can anyone please advise on where this folder might be located? Thank you. ...

Issues encountered while optimizing JSON file in a ReactJS program

I'm struggling with utilizing Array.prototype.map() in Javascript Specifically, I'm reformatting a JSON file within my react app, which looks like: { "id": 1, "title": "Manage data in Docker", "description": "How to use v ...

What could be causing the error message 'Please activate javascript' to appear in my React.js application?

I encountered the you need to enable javascript to run this app error in my MERN stack application. Every aspect works well except for an API call made for a specific route. I utilize the axios package and all API calls within the application successfully ...

Customize the appearance of semantic-ui-react components with hover effects

When I assign a specific className to components like <Segment className="Change" color='blue' inverted></Segment> and then in my CSS I apply the following: .Change:hover{ background-color: black; //or any other hover effect } N ...

The appearance of a Kendo dialog box in Angular 2+ inside a Kendo grid is triggered when enclosed within a kendoGridDetail

Is there a CSS method to display the Kendo Dialog outside of the kendo grid when it is wrapped inside kendoGridDetailTemplate? Here's how my component currently appears: <kendo-grid><div *kendoGridDetailTemplate="let dataItem"> <looku ...

Is it possible to simultaneously employ two asynchronous functions while handling two separate .json files?

Is it possible to work with 2 .json files simultaneously? My attempt did not succeed, so I am looking for an alternative method. If you have a suggestion or know the correct syntax to make this work, please share. And most importantly, does the second .j ...

Error in parsing: Looking for the correct JSX closing tag for <Route>?

Having trouble linking my car page so that it redirects to the correct location. I am encountering an error message indicating a JSX closing tag for route, even though all tags appear to be properly closed. Can't figure out why this error keeps occurr ...

Is it possible to concurrently run two instances of ReactI18Next within a single application?

I'm currently attempting to implement 2 separate instances of React-i18Next within the same application. My goal is to have certain parts of the app translated with instance1 and other parts with instance2. I've familiarized myself with createIn ...

Next.js - exceeding 2GB in application size

I recently incorporated Next.js into my frontend application built with React.js (typescript). The size of my Dockerfile ballooned to over 2GB, compared to its previous size of around 300MB (including assets and images). This increase in size has made it f ...

"When using the Fetch API to make a POST request, the req.body is found to

When attempting to call the POST API using the fetch method and passing parameters in the body, I encountered an issue where req.body was undefined on the Node.js backend side. However, when I called the same POST API through Postman with parameters in the ...

Achieving success by correctly reaching the window's edge during an event (onscroll)

This happens to be my 1st inquiry. Specifically, I have a navigation menu with a transparent background and I am attempting to alter the background once it reaches the top edge of the window. window.addEventListener("scroll", navSticky); function navSt ...

A unique feature where a bar emerges from the bottom of the screen, smoothly glides upwards, and then securely fastens to

I'm working on bringing to life a concept that I've been envisioning. The design would feature a long scrolling page with a unique navigation bar behavior. The idea is for the navigation bar to initially start at the bottom of the screen and the ...

Tips for arranging divs in CSS grid with grid-template-areas technique

I am attempting to organize similar groups of elements in a grid: #grid { position: absolute; left: 135px; top: 158px; width: 1080px; height: 1035px; display: grid; grid-template-rows: 99px 1fr 1fr; grid-template-column ...

What is the best way to organize the Ant Design grid in a nested structure

Having set up two columns (each with a span of 6), I am now seeking to incorporate a component beneath these columns. As someone who is well-versed in Android development, I typically rely on linear layouts to address such needs within that platform. How ...

Find the preceding element of the element that includes a specific link text

Can someone help me figure out how to click on a <td> element that precedes the <td> element with the linkText "Main" using Selenium? The element I'm looking to click is shown in the highlighted area of this image: https://i.sstatic.net/Q ...

Is it possible to use v-if in conjunction with a style tag to specify a different source file? Alternatively, is there a more efficient method I

I attempted the example provided below, but unfortunately, it did not function as expected. The reason behind my endeavor is that adding numerous modifiers (--tuned) to achieve the desired outcome seemed impractical. Therefore, I decided to try and link ...

What is the best way to change the size of a QR code

My current HTML code: <input id="text" type="text"/> <div id="qrcode"></div> Previous version of JAVASCRIPT code: var qrcode = new QRCode("qrcode"); $("#text").on("keyup", function () { qrcode.makeCode($(this).val()); }).keyup().focus ...

Conceal the Standard Select Dropdown Arrow in Internet Explorer 9

VIEW DEMO I attempted to use the CSS property appearance: none; to hide the select dropdown arrow, but it still shows in IE9. Refer to the image for clarification. Does anyone have a solution to hide the arrow using either CSS or jQuery? Below is my cod ...