- React Material:UI Cookbook
- Adam Boduch
- 163字
- 2021-06-24 15:49:33
How to do it...
Let's say that you have three basic tabs using the following code:
import React, { useState } from 'react';
import { withStyles } from '@material-ui/core/styles';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
const styles = theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper
}
});
function TabAlignment({ classes }) {
const [value, setValue] = useState(0);
const onChange = (e, value) => {
setValue(value);
};
return (
<div className={classes.root}>
<Tabs value={value} onChange={onChange}>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</div>
);
}
export default withStyles(styles)(TabAlignment);
Here's what you should see when the screen first loads:
![](https://epubservercos.yuewen.com/E00FB0/19470380001496806/epubprivate/OEBPS/Images/39fbb81c-8587-4e7d-baef-56929e5508ce.png?sign=1739553649-5KM68xcR0qX0Ozalem35jdGGCadJ9Lj8-0-254607beb4faf95254fc1449bf8b8f20)
By default, tabs are aligned to the left. You can center your tabs by setting the centered property, as follows:
<Tabs value={value} onChange={onChange} centered>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
Here's what centered tabs look like:
![](https://epubservercos.yuewen.com/E00FB0/19470380001496806/epubprivate/OEBPS/Images/d8af98bd-1853-465b-9ba1-86100051bcbf.png?sign=1739553649-hkdHb6tCezOhRqHHJVvyjlWt9xJsRMjz-0-543e4e8c8d790158537f4afb422ca049)
When your tabs are centered, all of the empty space goes to the left and right of the tabs. The alternative is setting the variant property to fullWidth:
<Tabs value={value} onChange={onChange} variant="fullWidth">
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
Here's what full width tabs look like:
![](https://epubservercos.yuewen.com/E00FB0/19470380001496806/epubprivate/OEBPS/Images/e9bf778b-ce3f-4224-b2a3-36a7f5cb8fa5.png?sign=1739553649-53QcBAW6Y1An5OeldXAKsJ2lvqfRmm9k-0-085e3886fd20bf6f267959076fcda4f2)
The tabs are centered, but they're spaced evenly to cover the width of the screen.