Utilizing Emotion CSS to incorporate images into buttons

I'm trying to add some style to two buttons, Up and Down, by using emotion CSS but I'm having trouble. Currently, I typically style my elements within a function. Is there a way for me to accomplish this with emotion CSS? I checked out but still can't seem to get it right.

    import up from "../img/up.png";

    function CustomButton(props) {
    let styling = {
    background: `url(${up})`,
    paddingRight: 24,
    paddingTop: 26,
    paddingLeft: 26,
    paddingBottom: 26.6
    };
    return (
    <button style={styling} onClick={() => props.handleClick()}>{props.background}</button>
    );
    }

//A similar code structure is written for CustomButton2


function Post(props) {
    return (
    <div>
                    <Up >
                        <CustomButton src={"../images/up.png"} handleClick= . 
     {props.incrementScore} />
                    </Up>

                    <Down >
                        <CustomButton2 src={"../images/down.png"} 
 handleClick{props.decrementScore} />
                    </Down>
                </Col>
            </Row>
        </Container>
    </div>
    );
} 

Answer №1

If the src attribute contains the background image, it seems necessary to modify the background property in order to incorporate the prop:

background: `url(${props.src})`,

Answer №2

Make sure to utilize the src prop to specify the image path.

// One method is to use css from emotion/react

/** @jsx jsx */
import { jsx, css } from '@emotion/react'

const PostButton = ({ background, handleClick, src }) => (
  <button css={css`
    background: ${`no-repeat url(${src})`};
    padding: 26px 24px 26.6px 26px;
  `} 
  onClick={handleClick}>{background}</button>
)

// Another approach involves using css from emotion/css

import React from 'react'
import { css } from '@emotion/css'

const PostButton = ({ background, handleClick, src }) => (
  <button className={css`
    background: ${`no-repeat url(${src})`};
    padding: 26px 24px 26.6px 26px;
  `} 
  onClick={handleClick}>{background}</button>
)

// A third option is to use css from emotion/css and pass styles as an object

import React from 'react'
import { css } from '@emotion/css'

const PostButton = ({ background, handleClick, src }) => (
  <button className={css({
    background: `no-repeat url(${src})`,
    padding: '26px 24px 26.6px 26px'
  })} 
  onClick={handleClick}>{background}</button>
)

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

What is the best way to incorporate a feature that flips all choices in react-select?

I'm working with a multi-option Select component from react-select, and I need to add a special option that will invert the selected options into unselected ones, and vice versa. When this 'Invert selection' option is chosen, I don't w ...

CSS3 problem with using transparent PNGs as border images

Currently, I am implementing the border-image property using a PNG image that contains a transparent section. The issue arises when the background-color of the div is set to black. Upon applying the border-radius, the transparent section of the pattern dis ...

What could be causing the error during the build process when using eslint version 5.6.0?

After creating a react app using the react-scripts package, I encountered an issue where it required eslint ^5.6.0 as a dependency. However, there was another version (8.30.0) detected higher up in the tree, which is the latest one I had installed. Surpris ...

Tips for aligning text and an image next to each other within a table column using HTML

I have a table where I am displaying an image and text in separate columns, with the image above the text. However, I want to showcase them side by side and ensure that the table fits perfectly on the screen without any scroll bars. Here is my HTML code s ...

The icon in Material UI button is missing from the display

The button is functional, but the icon inside it isn't displaying correctly:https://i.sstatic.net/MXhIH.jpg Here is the code for the button: <Button color="secondary" aria-label="add an alarm"> <AlarmIcon></AlarmI ...

Using type aliases in Typescript to improve string interpolation

After working with Typescript for some time, I have delved into type aliases that come in the form: type Animal = "cat" | "dog"; let a1_end = "at"; let a1: Animal = `c${a1_end}` I initially thought that only the values "cat" ...

Increase the distance between input boxes using CSS styling

I'm encountering an issue with adding spacing between input boxes, despite attempting to use padding. The padding only seems to move the boxes instead of creating space between them. Here is my code: ...

How to dynamically update a list in a React autocomplete input field?

Currently, I am developing an address autocomplete feature that utilizes the Google Places autocomplete API. To fetch data while the user types, I have implemented React Query and utilized a <datalist> where I iterate over the array to display <o ...

Confusion over React Prop being rendered as an object - perplexed by the reason behind it

Having trouble understanding React and need help... I'm attempting to pass my state as a prop in the parent component and then display that state in a child component. Here's what I have: Parent Component class App extends Component { const ...

Selecting the label "for" will activate both the anchor tag and input tag simultaneously

It seems like I'm having trouble making this work, and it appears to not be possible. That's why I'm reaching out for help... Here is the CSS that is being used: label.btn { cursor:pointer; display:inline-block; } label.btn>inpu ...

Step-by-step guide to crafting curved bars in Material-UI X visualizations

Is it possible to create a MUI X Chart with rounded corners from the top? Take a look at this Image import { BarChart, LineChart, PieChart } from "@mui/x-charts"; const users = [ { label: "bachir", value: 1730, }, { label: "Othma ...

Place an element in a higher position than a slideshow

I recently encountered an issue where I was trying to place a png image above my slideshow. Despite trying various solutions, the image always appeared behind or on top without transparency. Based on the erratic display, it seems like the problem might be ...

It is recommended that the page does not scroll while utilizing both the sidenav and toolbar simultaneously in Angular Material

Using md-sidenav without md-toolbar works fine. The sidenav opens correctly, as shown in the image below. https://i.sstatic.net/PjsPp.png However, when a toolbar is added before the sidenav or its containing section, opening the sidenav causes the page t ...

Add a container element resembling a div inside a table without implementing the table layout

I am working with a table that is rendered by DataTable, and I have the requirement to dynamically append new elements inside the table like this: The dark grey area represents the new DOM elements that need to be inserted dynamically. The first row cont ...

Adding a border in jQuery or Javascript to highlight empty form fields

After making the decision to dive into the world of Javascript, I've been dedicated to perfecting a script that will encase empty elements with a border right before the user submits a form. My ultimate goal is to implement live validation in the form ...

"Experiencing a callstack overflow due to multiple carousels on one page using Jquery or Vanilla

Currently, I am working on implementing a Jquery infinite autoplay multiple carousel. What I have done is created two separate carousel blocks on the same page within the body section. <div class="rotating_block"> <div class="bl ...

Using a third-party React component within a web-based forms application

Is it possible to include a Material UI component (https://material-ui.com/getting-started/usage/) in a web forms application? Below is the code I currently have, but it is not displaying the Button component. I am working with an ASPX file and using UMD ...

Issue: The parameter "data" is not recognized as a valid Document. The input does not match the requirements of a typical JavaScript object

I encountered the following issue: Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object. while attempting to update a document using firebase admin SDK. Below is the TypeScript snippet: var myDoc = new MyDoc(); myDo ...

Trouble with Bootstrap 3's nav-justified feature not displaying correctly upon window expansion

Looking at this Bootstrap example page, I noticed a small issue with the nav-justified navigation. When the window is minimized, it transitions correctly to a mobile version. However, when the window is maximized again, the buttons remain in the mobile for ...

Is there a way to decrease the size of the content within this iFrame?

Is there a way to display 6 cameras on a single webpage without them taking up the entire screen within iframe windows? Can the content be shrunken to fit the iframe window? <!doctype html> <html> <head> <meta charset="utf-8"> &l ...