Button spanning the entire width, specifically designed for extra small screens

I am currently utilizing the Material UI framework to build a form, which includes a <Button> component nested inside a Grid.

My objective is to maintain the standard width and height of the button on md screens and above, while ensuring it occupies full width with some additional vertical padding on xs screens. Unfortunately, I'm struggling to achieve this. Any suggestions?

For the complete code, you can visit: or view in codesandbox

import "./styles.css";
import Grid from "@mui/material/Grid";
import Button from "@mui/material/Button";

export default function App() {
  return (
    <Grid container spacing={2}>
      <Grid item xs={12}>
        <Button variant="contained">Submit</Button>
      </Grid>
{/* 
      <Grid item xs={12}>
        <Button fullWidth variant="contained">
          Submit
        </Button>
      </Grid>
 */}
    </Grid>
  );
}

Answer №1

To adjust the width, simply follow these steps:

<Button variant="contained" sx={{ width: { xs: "100%", md: "auto" } }}>
  Submit
</Button>

Feel free to reach out if you have any more questions!

Answer №2

My solution to this problem involved utilizing the useMediaQuery[1] MUI hook in the following manner:

Unfortunately, I do not have a code sandbox to share as useMediaQuery does not seem to function properly with it.

import "./styles.css";
import Grid from "@mui/material/Grid";
import Button from "@mui/material/Button";
import useMediaQuery from "@mui/material/useMediaQuery";

export default function App() {
  const isFullWidth = useMediaQuery((theme) => theme.breakpoints.only("sm"));
  return (
    <Grid container spacing={2}>
      <Grid item xs={12}>
        <Button fullWidth={isFullWidth} variant="contained">
          Submit
        </Button>
      </Grid>
    </Grid>
  );
}

I believe there should be a simpler syntax for achieving this functionality, for example:

// This is just a hypothetical example
<Button fullWidth={(theme) => theme.breakpoints.is("sm")} variant="contained">

But that's just my opinion.

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 activating the default 500 error page in Next.js

I'm having trouble getting Next.js to display its default 500 error page. While most sources discuss creating a custom error page, the Next.js documentation only briefly references their built-in 500 error page. I want the default page to show up when ...

creating images using express.js

Exploring the world of node.js and express.js is an exciting journey for me as a newcomer. I've been working on upgrading my front-end project with these technologies, and everything seems to be falling into place perfectly except for one roadblock - ...

What is the process for turning off automatic RTK Query auto-fetch requests?

When using an auto-generated hook from createApi, the hook automatically makes a request to the server. However, I only need this request to be made under certain conditions. I attempted to follow the implementation in the Redux documentation (https://redu ...

"The PHP script was executed despite having a MIME type of "text/html", which is not an appropriate JavaScript MIME type" error

I'm encountering an issue while trying to add data to an Oracle database using PHP from a Bootstrap form. When attempting to run the HTML file in Mozilla Firefox, the following error is displayed: The script from “http://localhost/CustomerPartInAs ...

Preventing flex items from becoming overly tall in Firefox using flex-direction: column

Working with flex layout has presented a challenge in Firefox and IE11. I have set up a codepen to demonstrate the issue. Screenshot https://i.sstatic.net/i9KlS.png Chrome(left), Firefox(right) Description The intended behavior is for the header and f ...

Ways to convert a PHP array into a JavaScript array

I have a 3D array in php when using var_dump($arr) it looks like this array(2) { [0]=> array(4) { [0]=> array(2) { [0]=> string(7) "36.3636" [1]=> int(8) } [1]=> array(2) { [0]=> string(7) "27.2727" [1]=> int(5) } [2]=> a ...

Move the scroll bar away from the scrollable div and reposition it on a different part of the page using CSS

Is it possible to use CSS to position the scrollbar of a div that takes up the left half of my page with overflow-y: scroll to be where the page scrollbar would normally be (far right side of the page)? I've experimented with the ::-webkit-scrollbar ...

Deactivate button based on two criteria

I am currently facing an issue with jQuery where I need to disable a button based on two conditions. The first condition requires the user to select an option, and the second condition requires the user to upload a file. While I have managed to write the c ...

Having difficulty reducing the size of mui buttons on mobile devices

Currently, I am facing difficulties in reducing the size of buttons using the makeStyles hook with the latest version of Mui. Despite trying various properties to style my buttons, they do not seem to become narrower than 64px wide in mobile view (when med ...

React Native CSS Triangles not functioning anymore

I'm currently developing an application that involves triangles overlaying other containers and divs. This issue was previously resolved using CSS with the following code: .triangle:after { content: ""; display: block; position: absolu ...

Attempting to assign parameters in $routeChangeError testing is unsuccessful

Currently, I am in the process of testing the code snippet below: $rootScope.$on('$routeChangeError', function(event, current, previous, rejection) { if(rejection === 'Unauthenticated') { $location.path('/'); ...

How can I pan/move the camera in Three.js using mouse movement without the need to click?

I am currently working on panning the camera around my scene using the mouse. I have successfully implemented this feature using TrackballControls by click and dragging. function init(){ camera = new THREE.PerspectiveCamera(45,window.innerWidth / windo ...

Switch the jQuery overlay image upon clicking a button

Is there a way to dynamically change the overlay image of a jQuery-ui dialog using a button click from inside the dialog? I attempted to do so in the code snippet below, but unfortunately, the overlay image does not update. It seems that I need to manipu ...

Flask Socket-IO - Instant Messaging App with Real-Time Communication, Socket Functionality Not Operating as Expected

I've been dedicating a good amount of time to this flask project, working on incorporating Flask-SocketIO along with JavaScript SocketIO to enable real-time messaging capabilities. Here's the Python section of the code: ## Setting up Socket IO f ...

Animating text-shadow color in CSS from left to right

I need help with transitioning the text-shadow color horizontally on hover. I found a solution that demonstrates how to do this here: Although it shouldn't matter, I am working in react with styled-components. Here is an example of what I am trying ...

What is the best way to transfer information from a layout to its children within the app directory of Next.js?

One scenario I encounter frequently is the need for a button in a layout to toggle something within the components it contains. For instance, you might have a notifications button in a fixed layout that triggers a side panel to open in the main application ...

Issue: Unable to locate module '/node_modulesprogressbar/package.json'

Currently, I am facing an issue with the generation of a progress bar using Browserify and progressbar.js. When I try to var ProgressBar = require('node_modules/progressbar');, an error pops up saying Error: Cannot find module '/node_modules ...

Leveraging React Hooks to display a dynamic pie chart by fetching and mapping data from an API

I have a task where I need to fetch data from an API that returns an object containing two numbers and a list structured like this... {2, 1 , []} The project I'm currently working on utilizes 'use-global-hook' for managing state in Redux. T ...

Having trouble getting the Node.JS Express Server to function properly on Enide?

My webserver is built using Node.JS with express and has multiple route commands. While everything works fine when running Node from the command line, I encounter an issue when running it within the Enide environment. Each request triggers an error messa ...

Tips for customizing a jQuery themeHere are some strategies for

I have been working with the Smoothness theme and Jquery tabs, and I am looking to customize the appearance of a few specific tabs. I have attempted the following approach: $('#tab-id').css('background', '-moz-linear-gradient(cent ...