The grid collapses whenever I add any margin or padding

My current setup involves using the bootstrap-grid system alongside Material UI, which has been working well overall. However, I am facing a specific issue where the grid system does not maintain its slot space after applying padding or margin. This causes items to move down to the next row or line when there is insufficient space.
Here is an example
In the provided example, each button is set to have a grid size of 3, indicating that there should be 4 buttons in each row. Due to slight padding and margin adjustments, only 3 buttons are displayed in each line.
I am looking for a solution where the items automatically resize their width when margin and padding are added, rather than moving to the next row or line. How can this be achieved?
Thank you.

Answer №1

Avoid using the bootstrap col- classes directly on your button. Instead, encapsulate your button within a div element as shown below:

for (var i = 0; i < 10; i++) {
    buttonArray.push(
      <div className="col-sm-3 col-md-3 col-lg-3">
      <Button
        variant="contained"
        className={classes.button + " "}
      >
        Default
      </Button>
      </div>
    );
  }

Now you only need to adjust the button styling by modifying or removing this:

const useStyles = makeStyles(theme => ({
  button: {
    padding: theme.spacing(1),
    margin: theme.spacing(1)
  }
}));

If desired, you can add padding or margin to your button. But be cautious not to apply padding or margin directly to elements like .row or .col-*.

Answer №2

If you want to enclose it in a Div tag, here's how:

for (var i = 0; i < 10; i++) {
    buttonArray.push(
      <Div className="col-sm-3 col-md-3 col-lg-3">
      <Button
        variant="contained"
        className={classes.button}
      >
        Default
      </Button></Div>
    );
  }

Remember to import Div before using it:

import Div from "@material-ui/core/Grid";

Check out the example at: https://codesandbox.io/s/bootstrap-grid-testcase-efi4u

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

Are there any better methods for creating div backgrounds and borders using CSS?

Within my application, there exists a multitude of div elements that share identical backgrounds and borders but vary in size. Having to utilize the same background image for each individual div proves to be highly inefficient, particularly in terms of ba ...

it results in an error when attempting to deconstruct an object

Using a style object in a component <Temp styles={{fontWeight: 'bold', fontSize: '1.6'}} ...otherprops /> Encountering an error while deconstructing the style object Cannot read property 'fontSize' of undefined. The d ...

How can we implement conditional mandatory fields in a form using Yup and react-hook-form?

AMMENDE I am working on a form that utilizes Material UI, react-hook-form, and yup for validation purposes. This form includes three groups of radio buttons (for example: radio-g1, radio-g2, radio-g3), where the user is required to choose at least two op ...

Constructing and delivering React/Next.js applications through AWS S3

I'm currently in the process of creating a React/Nextjs application that I plan to deploy to AWS S3 and serve it through AWS CloudFront (using the S3 bucket as a distribution origin). I will use Route53 and DNS configurations to map my domain (for exa ...

Exploring the Selection State of Rows in Ant Design Table and Passing it to expandRowRender Component

Currently, I am utilizing nested tables within the Ant Design framework. My goal is to ensure that when a row is selected in the main table, all corresponding values in the nested tables for that particular row are also selected. Conversely, if I deselect ...

Managing file uploads in Django Rest Framework

I'm working on incorporating a filefield into my Django models, but I'm unsure of how to manage the files coming from the React Front-end. Models.py class Product(models.Model): name = models.CharField(max_length=500, default=0) sho ...

What is the best way to conceal the mobile navigation menu at the bottom of the

I'm currently utilizing Bootstrap classes to hide elements on different screen sizes, specifically trying to hide it on desktop and tablets. Here is the code snippet: return ( <div className="container-fluid bottom-navbar .d-xxl-none . ...

Linking text to specific spot on image

I am seeking a solution to anchor a text input box to a specific spot on an image. Although I can achieve this using CSS, the problem arises when I resize my window and the alignment of the input box is lost. While I want to allow some resizing of the win ...

"Utilizing the power of React Redux toolkit to toggle the visibility of a

Just starting out with redux toolkit and I have a question about updating my modal within the reducer. I attempted to update state.modal to be action.payload but it doesn't appear to be working as expected. export const uiSlice = createSlice({ name: ...

Utilize SVG background with transparent text for a clear view of surrounding content

I'm looking for a way to overlay a video with a semi-transparent black div that includes text and a button, while still allowing users to see the video playing behind it. I previously achieved this using a PNG image but now need to apply the effect to ...

CSS for when the mouse hovers over an element

Two php files and a js file are involved in this scenario. The issue at hand is that when the div is clicked, it navigates to a new page with an appended # to the URL, causing it to scroll to the specified comment ID. However, instead of scrolling to the c ...

How can I create the effect of text changing color automatically after a specified time period has elapsed

I am currently dealing with a timer that is functioning properly, but I have a specific CSS inquiry. Essentially, what I would like to achieve is when the timer hits 30 seconds, I want the numbers to change to red color. $(function () { var $startTimer = ...

Data manipulation with Next.js

_APP.JS function MyApp({ Component, pageProps }) { let primary = 'darkMode_Primary'; let secondary = 'darkMode_Secondary' return ( <Layout primary_super={primary} secondary_super={secondary}> <Component {...page ...

Is it feasible to have a set number of character lines per <p> tag without relying on monospaced fonts?

I am facing the challenge of breaking a large text into paragraphs of equal size, with the same number of string lines in order to maintain uniformity. Currently, I am using PHP and the following code to achieve this: function arrangeText(){ if (have_ ...

Unexpected issue encountered for identifiers beginning with a numerical digit

Is it possible to select an element from a page with an id that begins with a number? $('#3|assets_main|ast_module|start-iso-date') Upon attempting to do so, the following error occurs: Uncaught Error: Syntax error, unrecognized expression: ...

"Step-by-step guide on assigning a class to a Component that has been

When attempting to pass a component as a prop of another component, it functions correctly. However, when I try to pass a Component and manage its CSS classes within the children, I find myself stuck. I am envisioning something like this: import Navbar fr ...

Modify the icon for the action menu in Material-UI data grids

I am currently utilizing the free version of @mui/x-data-grid. Below is the column definition I am working with: const columns = [ { field: "actions", type: "actions", width: 100, getActions: (cell) => [ <GridActionsCell ...

The text for the Google chart is nowhere to be found

After creating a test project using the Google Chart example, I noticed that some text from the chart is missing after adding CSS. Here is the CSS code I used: html, body { font-size: 100%; height: 100%; width: 100%; } body { background: ...

When viewed on a mobile device, the page defaults to displaying the NumPad instead of the Text Keyboard in Angular

The email field in my angular app is opening a Key Pad in mobile view and I'm not sure why. <form (ngSubmit)="onSubmit()" #emailForm="ngForm"> <div class="emailaddress" style="text-align:center;" *ngIf="!showEmail"& ...

The fusion of Material UI with React

Struggling to make the UI from material-ui function properly. Despite my efforts of researching online and on stackoverflow, I have not been successful in resolving the issues. Can someone provide assistance in making this work? The official guide seems in ...