Tips for customizing the CSS on desktop to ensure it does not interfere with content on tablets above it

My goal is to create a grid layout where the buttons align in the same row on desktop, stack with the search bar on one row and the rest of the row below it with some space on the left side. For mobile, I want the FilterButtons stacked on top of each other. I've tried many combinations but something seems off. Currently, it looks correct for desktop.

You can view the sandbox here: https://codesandbox.io/s/responsive-grid-u5uuud?file=/src/Responsive.jsx

This is my current code:

<Grid item lg={12} sm={12} xs={12} sx={{ mt: 2 }}>
            <Grid container spacing={2} height="100%">
              <Grid item lg={6} sm={6} xs={6} display="flex" alignItems="center" justifyContent="start">
                <Typography fontSize={"20px"}>Applications</Typography>
              </Grid>
              <Grid item lg={6} sm={6} xs={6} display="flex" alignItems="center" justifyContent="end">
                <Button sx={appStyle.newAppButton}>+ New Application</Button>
              </Grid>
            </Grid>
          </Grid>
          <Grid item lg={12} sm={12} xs={12} >
            <Grid container spacing={0} height="100%">
              <Grid item lg={3} sm={12} xs={12} display="flex" alignItems="center" justifyContent="start">
                <TextField
                  type="search"
                  variant="outlined"
                  margin="normal"
                  fullWidth
                  InputProps={{
                    startAdornment: (
                      <InputAdornment position="start">
                        <SearchIcon />
                      </InputAdornment>
                    ),
                  }}
                />
              </Grid>
              <Grid item lg={6} sm={0} xs={0} /> {/* empty column */}
              <Grid item lg={2} sm={9.9} xs={12} display="flex" alignItems="center" justifyContent="end">
                <FilterButton label="Product" options={productOptions} />
                <FilterButton label="Application Status" options={applicationStatusOptions} />
                <FilterButton label="Ticket Status" options={ticketStatusOptions} />
              </Grid>
              <Grid item lg={1} sm={1} xs={1} display="flex" alignItems="right" justifyContent="end">
                <Button style={{ fontSize: "14px", width: "150px", whiteSpace: "nowrap" }}>Clear All Filters</Button>
              </Grid>
            </Grid>
          </Grid>

This is how it should look on tablets: https://i.sstatic.net/WXHtv.png

And this is how it should look on mobile phones:

https://i.sstatic.net/IebpU.png

Answer №1

Enclose the FilterButtons within a single Grid and adjust the flex-direction based on screen width. Similarly, group the FilterButtons and Clear All Filter Button in a single Grid and modify its flex-direction accordingly.

For example:

/*
 * Styles for filterButton Grid
 */
const StyledFilterButtonGrid = styled(Grid)`
  display: flex;
  min-width: fit-content;
  max-width: 100%;
  justify-content: flex-end;
  flex-direction: column;
  align-items: flex-start;
  gap: 1em;
  padding: 0px;
  & > * {
    background-color: white;
  }

  /* For Desktop screen */
  @media screen and (min-width: 1024px) {
    flex-direction: row;
  }
`;

/** Styles for FilterButtons and Clear All Button Grid */
const StyledFilters = styled(Grid)`
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-block-start: 0.5em;

  /* Small Mobile Screen */
  @media (max-width: 400px) {
    flex-direction: column;
    align-items: start;
    gap: 1em;
  }
`;

You can access the sandbox code here

https://codesandbox.io/s/responsive-grid-forked-7h0p16?fontsize=14&hidenavigation=1&theme=dark

Demo Screenshots

On Mobile Screen

https://i.sstatic.net/VDkSF.png

On Desktop Screen

https://i.sstatic.net/74spt.png

On Smaller Mobile Screen

https://i.sstatic.net/tREkX.png

------------------------------- Update ---------------------------------

To align the search box and other filter buttons in desktop mode, provide appropriate values to lg attribute in TextField Grid and SearchFilters Grid. The code has been updated in the sandbox.

Updated Desktop Screen View

https://i.sstatic.net/SvPYs.png

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

Ways to access and retrieve data from Vuex store values?

Combining vuex and vuejs 2 for the first time. I'm a beginner with vuex and I need to monitor changes in a store variable. Trying to implement the watch functionality within my vue component. This is my current code snippet: import Vue from ' ...

Eliminate any vacant areas within a container and shift the container, along with its contents, on the webpage

I want to eliminate the empty space within the container while maintaining some spacing around the black box, and ensure responsiveness. Additionally, how can I shift the container with its content downwards and to the right of the webpage, while still ke ...

Terminate the XHR request in Cujojs Rest.js

Seeking a solution to cancel requests using cujojs/rest when a new one comes in. Trying to ensure only the latest request is processed, while aborting all other unfinished ones. Previously used beforeSend method with another tool for this task. Now strugg ...

Is there a method to verify if a GeoJSON feature is in the shape of a rectangle and locate its corner coordinates?

I am looking for a way to identify all the corner coordinates of GeoJSON features that are in rectangular or square shape. Some of the features have more than five coordinates but still represent a rectangle or square shape. Is there an algorithm or third- ...

Using string.startsWith() with Wildcards or Regular Expressions

Currently in the process of refactoring code that relies on string.startsWith() in JavaScript. The documentation here does not mention the use of wildcards or Regular Expressions with this method. Is there an alternative approach I can take? ...

Even though the jQuery addClass function successfully adds the class, there is no observable visual change happening in the browser

Currently, I am experimenting with Angular 1.6 and jQuery 3.3.1 to enhance my Angular skills. The layout of my simple application screen is shown below. https://i.sstatic.net/JkPVe.png In the snippet of code for the event handler provided below, there is ...

Ways to incorporate a php file based on the user's selection

I have numerous div elements, possibly a dozen or two, such as... <div class="mydivs" id="firstdiv"></div> <div class="mydivs" id="seconddiv"></div> <div class="mydivs" id="thirddiv"></div> <div class="mydivs" id="fo ...

The onClick action kicks in as soon as the page finishes loading

One particular line of code in the Button Tag has caught my attention. let content = this.props.searchResult.map((ele, i) => { let card_id = ele.user.username + "_card"; return( <div className="col s12 m6 l4" key={i} id={card_id}> ...

Transfer nokogiri through manual installation from one machine to another

I rely on nokogiri as the HTML parser for my website. However, when attempting to deploy my website on a shared hosting service, I encountered an issue with installing nokogiri due to restricted access to gcc. This restriction prevents nokogiri from buildi ...

What is the best way to ensure my gif shows up in the top row and spans two columns in this grid?

I am attempting to create cards with a unique design where the first row consists of 2 columns, one being a GIF that shows the card fitting into a cell with a border. However, all I see is an empty cell without any display. Below is the code snippet I am ...

Styling Challenges with CSS/AngularJS Accordion in Footer

I want to create a page layout as shown below: +---------------------------+ | Auto-fill / v scrollable ^| | || | || | v| +---------------------------+ | Fixed [][][] ...

Prevent the AngularJS bar-footer from covering up the page content

I am currently working on developing an app using Ionic and AngularJS. I am facing a challenge with the bar-footer overlapping the content at the bottom of the page. I want the content to be fully visible before the footer appears, without any overlap. Can ...

I'm unable to adjust the price to meet the quantity of units needed

$.ajax({ dataType: "json", url: "https://private-70cb45-aobara.apiary-mock.com/product/list", success: data =>{ const price = data[0].unitPriceInCents; var quantity = 1; //PRICE $("#Product-Price").text("$ " + data[0].unitPriceInCe ...

Take away the CSS class from an element after reCAPTCHA verification is complete in Next.js

I'm struggling with removing the CSS class btn-disabled from the input button element upon successful verification of a form entry. I have implemented a function called enableForm to remove the btn-disabled CSS class when reCAPTCHA is verified. Howe ...

JavaScript Variables Lose Their Values

Similar Inquiry: How can I get the response from an AJAX call in a function? I have written a function that fetches numerical data from an online file. Although the file retrieval is successful (verified by the alert message), I encounter an issue whe ...

"Unexpected token l in JSON at position 0" is causing an error, yet miraculously the code still functions - how is this possible?

Recently delving into the world of websockets with Go for the first time and encountering a strange error that does not disrupt the program flow. The client in use is a ReactJS single page application. JavaScript Client: const socket = new WebSocket(&quo ...

The useQuery hook failed to trigger a re-render upon receiving a response

After making some updates on the backend using the useQuery Apollo hook, I am trying to redirect the user back to the previous step. export const useOrderUnlock = () => { const [isLoading, setIsLoading] = useState(false); const isBlocked = useS ...

What is the hierarchy of fields in package.json?

After submitting a pull request to a repository to include a typings field in the package.json file, the maintainer suggested the following modification: - "typings": "./src/index.d.ts", - "main": "./src/index.js" ...

A method to verify the presence of a specific element within a list using JavaScript

I'm trying to validate if the list retrieved from the application contains the expected element. Can you please review my code and let me know where I might be making a mistake? this.verifyOptionsInDropdown = async function(){ var optionList = a ...

An error occurred: [object Object] does not support the 'popup' method

I've been attempting to close the jQuery popup by using $('#partsPopup').popup("close"); However, I encountered an error stating that there is no method called "popup". Below is the sequence of scripts that I have followed. Any assistance wo ...