Pau*_*bro 6 css flexbox reactjs tailwind-css
我有这个反应组件:
const Status = () => {
return (
<div className='h-screen bg-gray-800 flex justify-center items-center p-16'>
<div className='bg-gray-700 px-8 py-4 rounded w-full'>
<h2 className='text-2xl text-gray-100 font-medium'>Plex Server</h2>
<h3 className='text-base text-gray-400'>Added 22/10/2021</h3>
<div className='flex flex-row w-full space-x-1 my-4'>
{[...Array(30)].map((i, idx) => (
<div key={idx} className='h-8 w-1 bg-green-400 rounded'></div>
))}
</div>
</div>
</div>
);
};
export default Status;
Run Code Online (Sandbox Code Playgroud)
我用它来渲染 30 个硬编码宽度为 4px(w-1类)的 Flex 子项。结果如下:
有没有办法自动设置子宽度,以便它平等地填充父空间?
例如:父宽度为 100px,这次我只想渲染 10 个元素。使用当前代码,只需 76px(子宽度 40px + 它们之间 4px 空间 36px)。有没有办法将子项宽度自动设置为~6px?
您可以通过使用flex-1来代替它w-1,它会自动占用宽度,例如:
const Status = () => {
return (
<div className='h-screen bg-gray-800 flex justify-center items-center p-16'>
<div className='bg-gray-700 px-8 py-4 rounded w-full'>
<h2 className='text-2xl text-gray-100 font-medium'>Plex Server</h2>
<h3 className='text-base text-gray-400'>Added 22/10/2021</h3>
<div className='flex flex-row w-full space-x-1 my-4'>
{[...Array(30)].map((i, idx) => (
<div key={idx} className='h-8 flex-1 bg-green-400 rounded'></div>
))}
</div>
</div>
</div>
);
};
export default Status;
Run Code Online (Sandbox Code Playgroud)
这是它的一个工作示例:https://play.tailwindcss.com/rYFniYTQe6
其外观如下: