Guide to centering a Material UI Table on a webpage

Is it possible to center the <Table> within a fixed width <div>, and have a scrollbar appear when the browser is resized, while maintaining the fixed width of the <Table>? Thanks in advance.

This 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/>

EDIT

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

Answer №1

Check out this demonstration: https://jsfiddle.net/yb57vrhp/1/

Here is the HTML code snippet:

<div id="box"></div>

JavaScript code:

const { Box, BoxHeader, BoxContent, BoxItem, Button, BoxItemColumn, BoxHeaderRow, getMuiTheme } = MaterialUI;

 class Demo extends React.Component {
  render() {
    return (
        <div style={{ width: '100%' }}>
        <Box style={{ width: 400, margin: 'auto' }}>
          <BoxHeader>
            <BoxHeaderRow>
              <BoxItemColumn>ID</BoxItemColumn>
              <BoxItemColumn>Name</BoxItemColumn>
              <BoxItemColumn>Status</BoxItemColumn>
            </BoxHeaderRow>
          </BoxHeader>
          <BoxBody>
            <BoxItem>
              <BoxItemColumn>1</BoxItemColumn>
              <BoxItemColumn>John Smith</BoxItemColumn>
              <BoxItemColumn>Employed</BoxItemColumn>
            </BoxItem>
            <BoxItem>
              <BoxItemColumn>2</BoxItemColumn>
              <BoxItemColumn>Randal White</BoxItemColumn>
              <BoxItemColumn>Unemployed</BoxItemColumn>
            </BoxItem>
            <BoxItem>
              <BoxItemColumn>3</BoxItemColumn>
              <BoxItemColumn>Stephanie Sanders</BoxItemColumn>
              <BoxItemColumn>Employed</BoxItemColumn>
            </BoxItem>
            <BoxItem>
              <BoxItemColumn>4</BoxItemColumn>
              <BoxItemColumn>Steve Brown</BoxItemColumn>
              <BoxItemColumn>Employed</BoxItemColumn>
            </BoxItem>
          </BoxBody>
        </Box>      
      </div>
    );
  }

}

const Application = () => (
  <MuiThemeProvider muiTheme={getMuiTheme()}>
    <Demo />
  </MuiThemeProvider>
);

ReactDOM.render(
  <Application />,
  document.getElementById('box')
);

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

The error message "Cannot read property '$scope' of undefined" indicates that there is an issue

After receiving HTML content in an Angular app, I inject it into the HTML document but struggle to retrieve it. For example, you can see a minimized code on plunker here and in JavaScript here Below is my controller: class ReadCtrl constructor: (@$sco ...

Tips for retrieving HTML file content as a string using JavaScript

I'm looking to retrieve the contents of an HTML file using JavaScript and store it as a string. I tried writing some code for this purpose, but unfortunately, I encountered an error: Error: Cannot find module 'xmlhttprequest' Could someone ...

Having issues with ReactJs rendering - why does my signout button only appear after a refresh?

Can anyone assist me? I have a question about retrieving user data from local storage. The issue is that the sign out button only appears after refreshing the page. Can someone provide guidance on this, please? Thank you in advance. Here is the code snipp ...

Specifying Size of Icons in HTML and CSS

Displayed in the image below are 3 icons - 2 of them are the same size, while one (on the left) appears larger and uneven compared to the other two. https://i.sstatic.net/JtIov.png This snippet demonstrates the HTML code related to this section: < ...

What is the best way to incorporate a @types module into a TypeScript file that is not already a module?

Setting the Stage: In the process of shifting a hefty ~3,000 line inline <script> from a web-page to a TypeScript file (PageScripts.ts) to be utilized by the page through <script src="PageScripts.js" defer></script>. This script entails ...

How can a Link be used to open a component in Material-UI V 5.0 when using a MenuItem with onClick={popupState.close}?

Hey everyone, I spent the entire Sunday trying to create a dropdown menu in the AppBar but encountered various errors. The only menu that worked without any issues was using the "PopupState helper" which can be found at https://mui.com/material-ui/react-me ...

Cloning a file input does not retain the uploaded file in the cloned input. Only the original input retains the uploaded file

Whenever I duplicate an input type file, I encounter an issue where the uploaded file from the duplicated input is also linked to the original input. It seems like the duplicate input is somehow connected to and taking files for the original one. This is ...

Encountering 'no overload matches this call' while developing a useReducer using React with Typescript

import { useCallback, useReducer } from "react"; const ARTIST_REDUCER_TYPES = { ADD: "ADD", }; const artistArray = [...Object.values(ARTIST_REDUCER_TYPES)] as const; type ActionReturnedTypes = (typeof artistArray)[number]; type Re ...

Acquire Category Permissions when making a channel in discord.js v14

I am in the process of setting up a channel that will grant specific roles access while automatically blocking out @everyone. I also want this setup to be compatible with categories, allowing for other roles to have permissions within them. let customPermi ...

Different ways to analyze elements from 2 arrays to hone in on specific values

I'm really struggling to solve this issue. I have two arrays - one for cars and the other for active filters. The cars array consists of HTML li tags with attributes like car type, number of seats, price, etc. On the other hand, the active_filters arr ...

Basic contact form with modal feedback

Hey there, I'm looking for a way to have a regular contact form on my webpage and once it's submitted, display the PHP action in a pop-up modal that says "Thanks for contacting us" before allowing users to close it. I've worked with HTML con ...

Encountering problems with `npm run build` when using `con

Running a react app with express as the backend has been quite the adventure. After following this particular tutorial on how to connect the two and deploy to heroku, I found myself in a bit of a conundrum. Express is running smoothly on port 5000 as instr ...

Incorporating a CSS file within a JSP page

I've been struggling to get my '.jsp' file to utilize a '.css' file that I created. Initially, I stored the '.css' file in the 'WEB-INF' folder but discovered that this folder is not accessible publicly. So, I m ...

My goal is to create a stylish form using bootstrap and CSS3

I am aiming to replicate the layout of this particular page: https://i.sstatic.net/HxOhC.png Struggling to utilize css3 for designing, how can I achieve a form similar to this? Facing difficulty in creating the outer red border and inserting spacing betw ...

How to showcase numerous PDF documents within a ReactJS application

As a newcomer to the world of React, I am facing an issue with displaying multiple PDFs based on the selected link. Although I have been successful in displaying a PDF from a list of links pointing to my stored PDFs within the src directory, I encounter th ...

Using useEffect with promises causing TypeScript errors

useEffect(login, []) In this case, the login function returns a promise and the useEffect hook is triggered without expecting a return value. However, TypeScript shows errors like: Argument of type '() => Promise<void>' is not assi ...

Troubleshooting textarea resize events in Angular 6

In the development of my Angular 6 application, I have encountered an issue with a textarea element. When an error occurs, an asterisk should be displayed next to the textarea in the top-right corner. However, there is a gap between the textarea and the as ...

Stop any accidental double clicks on the <input type="submit" /> to avoid duplicate entries

Is it possible to prevent a user from clicking twice on an input tag button with type submit? I attempted using ondblclick="javascript:void(0)", but it did not work. My code is within an Html.BeginForm, not a form element tag. Thank you in advance for al ...

What is the best way to bundle a node module with the choice of adding submodules?

I am in the process of developing a JavaScript library that consists of a central core module and multiple optional submodules that enhance the functionalities of the core module. This library is specifically designed for the browser environment, utilizing ...

Using React.js to Switch Images When Links are Clicked

Currently, I am in the process of learning React.js for a school project and I'm facing challenges with toggling images alongside active links. Despite extensive searching, I have not been able to find a tutorial that addresses this specific issue. My ...