Achieving Center Alignment for Material-UI's <Table> within a <div> using ReactJS

Currently, I am working with a Material-UI's <Table> embedded within a <div>. My goal is to center the <Table> while maintaining a fixed width. I want the table to remain centered in the browser, but if the browser window is minimized, I would like scroll bars to appear for viewing.

How can I achieve this - centering the <Table> with a fixed width inside a <div>, and ensuring that scrollbars appear when needed to maintain the fixed width of the <Table>? Thank you!

Below is my current setup:

   <div>
    <Table>
      <TableBody style={{width: 1000}}>
        <TableRow>
          <TableRowColumn>Row 1</TableRowColumn>
          <TableRowColumn>Content 1</TableRowColumn>
        </TableRow>
        <TableRow>
          <TableRowColumn>Row 2</TableRowColumn>
          <TableRowColumn>Content 2</TableRowColumn>
        </TableRow>
      </TableBody>
    </Table>
   <div/>

I tried applying the following styling to the <div>:

style={{display: 'flex', alignItems: 'center', justifyContent: 'center}}
. This approach somewhat centered the elements, but when the browser was resized too small, there was some right-side padding covering up the table.

Answer №1

When you set the width to 100% in your style, the layout remains consistent even after minimizing the browser window.

  <div>
<Table>
  <TableBody style={{width: 100%}}>
    <TableRow>
      <TableRowColumn>Row 1</TableRowColumn>
      <TableRowColumn>Content 1</TableRowColumn>
    </TableRow>
    <TableRow>
      <TableRowColumn>Row 2</TableRowColumn>
      <TableRowColumn>Content 2</TableRowColumn>
    </TableRow>
  </TableBody>
</Table>

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

Having trouble getting Javascript to reveal hidden elements based on their class

I am having some trouble making specific fields in a form appear based on the selection made from a dropdown menu. Below is a simplified snippet of my code, which should change the display from 'none' to 'block'. Can you help me figure ...

The error message thrown is: "Unable to assign headers after they have already been sent to the client."

I've been attempting to make a GET request, but it keeps failing at the app.js res.json line. app.js app.use(function(err, req, res, next) { res.locals.message = err.message; res.locals.error = req.app.get("env") === "development" ? err : {}; ...

Issue: "Exported functions in a 'use server' file must be async"

I'm currently working on implementing layout.tsx in the app directory of Next.js 13 to create a navigation layout that appears on all pages. I've successfully configured it so that the navbar updates when a user logs out or signs in, but there&ap ...

React filter continues to display only the values that have been filtered

I need help filtering an array of objects to exclude items with a blank or null value for the name property. Even though I have applied a filter, names that are empty strings still appear on the browser. Can anyone spot what is causing this issue in my c ...

Dropdown element with PrimeNG adorned with a span

I am trying to create a simple form with text inputs and dropdowns. I have successfully implemented the textInput, but I am struggling with the dropdowns. Below is the code that I have so far: <div class="p-grid p-dir-col p-offset-2"> ...

Issue with selecting a value in React MUI and default value not being defined

Currently, I am working on creating a form in React using MUI and Formik. While implementing the select feature with default values fetched from an API object, I encountered issues where the select function was not working as expected. Strangely, I couldn& ...

Issues with Angular 2 loading properly on Internet Explorer 11

We are currently running an Asp.net MVC 5 web application integrated with Angular 2. The application functions smoothly on Chrome, Firefox, and Edge browsers, but encounters loading issues on IE 11, displaying the error illustrated in the image below: ht ...

The Ajax PHP function only works on the initial call

The function below calls a PHP file that returns results in JSON format, which are assigned to JavaScript values. The PHP function has been thoroughly tested and works as expected. The results are stored in variables until the market variable is changed wi ...

Encountering an error with Material-ui's withTheme() function: "TypeError: Object(...) is not a function"

UPDATE: Issue resolved with v1.0.0.36 beta version Trying to access Material-UI's theme in a component using withTheme. Followed the sample in the docs, packaging through create-react-app without errors but encountering TypeError in the browser. The ...

In what scenario should I respond with true or false to AJAX, and when is it more appropriate to use the echo function to output "true" or "false

It seems I have managed to confuse myself. I mistakenly believed that when using AJAX to communicate with PHP (like $.post), one had to echo back a "true" or "false" instead of simply returning true or false. I now realize this is not the case, but could ...

Show content to the right side of the division element

I would like to display the bold text up here, and this is my code: img{ margin-left:10px; } html <div id="wrap"> <img src="bg.jpg" /> <b class="name">WaqasTahir</b> </div> ...

Sporadic UnhandledPromiseRejectionWarning surfacing while utilizing sinon

Upon inspection, it appears that the objects failApiClient and explicitFailApiClient should be of the same type. When logging them, they seem to have identical outputs: console.log(failApiClient) // { getObjects: [Function: getObjects] } console.log(expli ...

Adjust the ListItem's dimensions to match the ListItemText in order to resemble chat bubbles

Here is where I currently stand: and here is the code I am working with: <ListItem sx={{ textAlign: "left", bgcolor: "#f6f6f6", borderRadius: "10px", marginBottom: "1rem", }} > <ListIt ...

Ensuring Code Execution Order in NODE.JS

I've encountered an issue with my code that involves parsing a pcap file, storing the data in an array data = [], and then writing it to a JSON file: var fs = require("fs"); var pcapp = require('pcap-parser'); var hex = ""; var data = []; v ...

Utilizing the predefined color palette of Bootstrap for styling text in a

When creating a form using Bootstrap, I want to add some text in a color that matches the brand color used for buttons. While I am aware of the text colors like text-primary available in Bootstrap, I prefer to use button colors for my text. I attempted th ...

Utilizing useStyles/jss to customize theme in Material Form Field and TextFieldRoot

Is there a way to customize Material themes for FormControl in MUI version 4? Specifically, I need the margin bottom of the parent HTML element containing a Textfield to be 8px instead of the default 4px. The code snippet provided below is not achieving th ...

Issue with Bootstrap 4 Navbar collapsing and not expanding again

Help needed! My navigation bar collapses when the window is resized, but clicking on the hamburger icon does not open it back up. I have included my code below. Can someone please provide guidance on how to expand the collapsed navbar? <html lang=&quo ...

What is the process for making an Ajax request in Rails?

I'm attempting to send a variable through jQuery using the POST method, saving it in a controller, and then utilizing that same variable in Rails HTML to query a table. It seems like the variable isn't being passed to the controller. jQuery: v ...

Is there a secure way to prevent user input from causing issues in a BigQuery node insert? Can the BigQuery.insert node library support parameterized queries for added security

I am seeking advice on how to securely insert user form data into BigQuery using the Google Cloud BigQuery library. Specifically, I am curious about the most effective methods for sanitizing, escaping, and cleaning the input data. Is it feasible to implem ...

Create a list item that includes an image with a corresponding label positioned beneath it

Currently attempting to convert my design into HTML and CSS for the first time, I've hit a roadblock with this particular task: I'm trying to create a series of white boxes, with each item having a white border. However, I'm struggling to c ...