What is the best way to utilize MUI breakpoints for displaying images based on different screen sizes?

I need help displaying an image based on the screen size using MUI breakpoints. I'm struggling to understand how to implement this with MUI. Can someone assist me with the breakpoints?

interface EmptyStateProps {
  title: string;
  description: string;
  handleOnClick: () => void;
  buttonText: string;
  picture: string;
}

export const EmptyState = ({ title, description, handleOnClick, buttonText }: EmptyStateProps): ReactElement =>{
  const { buyingTheme } = useBuyingTheme();
  const isDark = buyingTheme.palette.mode === 'dark';

  return (
    <ThemeProvider theme={buyingTheme}>
      <Box display='flex' width='100%' justifyContent='center' alignItems='center'>
        <Box display='flex' sx={{ height:'445px', width:'445px', border: '1px solid red' }}>
          <Box
            height='306px'
            width='333px'
            sx={{
              paddingTop: '70px',
              paddingBottom: '70px',
              paddingRight: '70px',
              paddingLeft: '47px',
              backgroundImage: `url(${isDark ? './picture.png' : './pictureLight.png' })`,
              backgroundPosition: 'center',
              backgroundRepeat: 'no-repeat',
              backgroundSize: 'contain', //like obect-fit
            }}
          />
        </Box>
        <Box display='flex' flexDirection='column' alignItems='left' sx={{ height:'136px', width:'445px', border: '1px solid blue', marginLeft: '20px' }}>
          <Typography variant="headings.h3" sx={{ height:'24px', paddingTop:'4px', paddingBottom:'4px' }}>{title}</Typography>
          <Typography
            variant="body.default"
            color="text.secondary"
            sx={{
              whiteSpace: 'pre-line',
              wordWrap: 'break-word',
              marginTop: '4px', height:'48px',
              paddingBottom:'4px', marginBottom: '4px',
            }}
          >
            {description}
          </Typography>
          <Button
            sx={{ height: '40px', width: '93px', padding:'20px', marginTop: 'auto' }}
            onClick={handleOnClick}
          >
            {buttonText}
          </Button>
        </Box>
      </Box>
    </ThemeProvider>
   );
 }
;

I am trying to render different images based on two screen sizes:

Image 1 - Height: 306px, Width: 333px Screen Size: Width:1920px, Height: 1080px

Image 2 - Height: 285px, Width: 285px Screen Size: Width:1280px, Height: 832px

How can I dynamically switch between these images based on the screen sizes?

Answer №1

To implement breakpoints, insert them into the sx prop as an object in this format:

<Box
  height='306px'
  width='333px'
  sx={{
    backgroundImage: {
      xs: `url(${isDark ? './pictureXS.png' : './pictureLightXS.png' })`,
      md: `url(${isDark ? './pictureMD.png' : './pictureLightMD.png' })`,
      lg: `url(${isDark ? './pictureLG.png' : './pictureLightLG.png' })`,
    },
    ...
  }}
/>

You have the option to use the default breakpoints (xs, sm, md, lg, xl) with their preset values, or you can personalize the breakpoint names and values by setting up your own custom breakpoints in the createTheme configuration.

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

Is it possible to modify the background color of a checkbox?

I am looking to modify the background color of the checkbox itself, specifically the white square. I have spent a lot of time researching this topic but most articles suggest creating a div and changing its background color, which does not affect the whi ...

I'm having trouble figuring out how to access response headers with HttpClient in Angular 5. Can anyone

I recently developed an authentication service in Angular 5, where I utilize the HttpClient class to make a POST request to my backend server. The backend server then responds with a JWT bearer token. Here is a snippet of how my request looks: return thi ...

Is it possible to generate two output JSON Objects for a JavaScript line chart?

How can I display two lines in a Chart.js line chart using data from a JSON file with 2 objects stored in the database? When attempting to show both sets of data simultaneously, I encountered an issue where the output was always undefined. Why is this hap ...

Please display the Bootstrap Modal first before continuing

Currently, I'm facing a challenge with my JS code as it seems to continue running before displaying my Bootstrap Modal. On this website, users are required to input information and upon pressing the Save button, a function called "passTimeToSpring()" ...

Transferring elements from one array to multiple arrays

I have been working with the basics of JavaScript arrays and here is my code snippet: let arr = ['students', 'exams', [{'sub1':80, 'sub2':60},{'grade_sub1':'A', 'grade_sub2':'B&apos ...

Checkbox ensemble computes total score

I am currently working on a project that involves multiple checkbox groups, with each group containing 3 checkboxes labeled as (1, X, 2). My goal is to assign a value of 100% to each group, distributed evenly among the checkboxes within it. The distributio ...

Issues with Select2 Ajax functionality not functioning as intended

I am currently using the select2 library to create a dropdown menu with ajax functionality, but I am encountering some issues. Below is my code: $("#guests").select2({ multiple: true, minimumInputLength: 1, formatInputTooShort: fun ...

Limitations of GitHub's rate limiting are causing a delay in retrieving user commit history

I have developed a code snippet to retrieve the user's GitHub "streak" data, indicating how many consecutive days they have made commits. However, the current implementation uses recursion to send multiple requests to the GitHub API, causing rate-limi ...

The name 'Landbot' cannot be located. Have you meant to type '_landbot' instead?

I'm currently in the process of integrating Landbot into my React.js application with TypeScript. I'm following this [doc] 1. However, I'm facing an issue where the code inside useEffect (new Landbot.Container) is causing an error. 'C ...

What methods can be used to accomplish this effect using CSS and/or Javascript?

Is there a way to achieve the desired effect using just a single line of text and CSS, instead of multiple heading tags and a CSS class like shown in the image below? Current Code : <h2 class="heading">Hi guys, How can i achieve this effect using j ...

Initiate the React project by executing the command to start React Native development

Typically, I start my projects using npm start, but one time I accidentally ran npx react-native start and all the installation process for React Native completed. Now, I'm not sure if I need to worry about this or if I should remove React Native. If ...

Is it possible to execute a function once another function has been called within a specific interval

Currently, I am working on a Greasemonkey script and have encountered an issue. The website contains a function that runs at regular intervals: jQuery(function1.run); setInterval(function1.run, function1.interval); I aim to execute my function immediatel ...

Printing specific div content from an HTML page using a print button

I have a situation where I need to control the printing functionality of my HTML page. Currently, I have two div tags in my code with a single print button at the top of the page. When I click on this button, it prints the content from both divs. However ...

The event listener for the custom cursor in Nuxt.js is failing to work properly when the route

I am currently in the process of constructing a new website for our studio, but am encountering difficulties with getting the custom cursor to function correctly. I implemented a custom cursor using gsap, and it worked perfectly; however, once I navigate t ...

How can I send a Vue.js object to a Laravel controller?

I am working with a Vue component that includes an object like this - dataObj = [{id:1,name:'sanaulla'},{id:1,name:'parvez'}] When I try to send a post request to the Laravel Controller using the following code - axios.post("/api/ ...

How to prevent links from being affected by the Gooey effect in D3

Issue: When applying the Gooey effect, the links are also affected, resulting in a teardrop shape instead of a circle. The code snippet includes a dragged() function that allows users to detach node 1 from node 0 and reconnect them by dragging. The code s ...

What causes the event parameter to be lost during recursion?

Below is the given code snippet: <html> <head> <meta charset="UTF-8"> <title>title...</title> <link type="text/css" rel="stylesheet" href="jquery-ui-1.10.4.custom.css" /> <script type="text/javascript" src="jq ...

SVG padding will only take effect if two or more sides are specified

What is the reason behind an SVG requiring at least 2 areas to be padded for padding to apply? For instance, applying padding-top: 50px to an svg element will have no effect. However, adding padding-top: 50px; padding-left: 1px will result in the padding ...

Tips for creating a vertical scrollbar within a row component

Is there a way to create a scrollable parent v-row with a child v-row overflowing with content so that I can nest another v-row inside the v-col and ensure it remains within the scroll element? <v-row> <v-col> <v-row> <p ...

Utilizing JSON and AJAX to mine data from databases

I've been working on creating a basic "search" box using Javascript, AJAX, PHP, and JSON. I'm focusing on the "world" database that I downloaded from the MySQL website for this particular project. I've hit a roadblock when trying to retrieve ...