【问题标题】:Map data to tailwind grid specific layout将数据映射到顺风网格特定布局
【发布时间】:2022-09-29 06:28:19
【问题描述】:

我一直在尝试使用网格来达到这种类型的layout,因为我发现它在这种情况下更容易处理。但似乎我当前的代码在第一列中实现了将两个盒子堆叠在一起,而在第二列中只有一个盒子,这不是所需的布局。如果数组的index=0 中的原色是占据整个第一列的 div 并且数组的两个剩余元素在第二列中堆叠在一起,我会很喜欢它。

这是它目前的样子:Link

这是我当前的组件代码以及顺风类:

const ColorPreview = ({ colors }: any) => {
  console.log(\'colors inside colorPreview\', colors);

  return (
    <div className=\'grid grid-cols-2 gap-6\'>
      {Object.keys(colors).map((key, index) => (
        <div key={uuidv4()} className=\' overflow-hidden rounded-lg border-2\'>
          <div
            className={clsx(
              \'h-[6rem] w-20 p-4\',
              index === 0 && \'h-60 w-48 p-4\',
            )}
            style={{
              backgroundColor: colors[key],
            }}
          >
            {\' \'}
          </div>
          <p className=\'justify-center p-2 text-base\'>{colors[key]}</p>
          {/* primary or secondary or tertiary*/}
        </div>
      ))}
    </div>
  );
};

非常感谢任何帮助或指导。

  • 我对 React 一无所知,但是可以以这种方式创建这种类型的布局 - 创建不是两列而是三列(甚至更多),其中第一个元素跨越 2 列,这给你 2:1 的比例。行也是一样。见demo

标签: css reactjs css-grid tailwind-css


【解决方案1】:

我写了一个CSS类。如果您只有 3 个项目,这将起作用。

.parentClass {
  /* targeting only those children 3n+1 */
  /* n=0=>1st, n=1=>4th and so on */
  & > *:nth-child(3n + 1),
    // in large screen make the first item col span 2 units and row spans 2 units
    @screen lg {
      @apply col-span-2 row-span-2;
    }
  }

如果您有更多项目,请确定要定位的项目。这是关于第 n 个孩子的链接: https://css-tricks.com/how-nth-child-works/

根据您的布局决定,您可能会编写 2 个单独的逻辑。例如

& > *:nth-child(3n + 1),
& > *:nth-child(6n + 1) {
   /* apply this css to target children. you could add more css here */
   @screen lg {
        @apply col-span-2 row-span-2;
    }

然后将parentClass 类名应用到顶级div。

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 2017-05-16
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多