javascript - 我应该如何设置卡片的样式,以便每行可以有 3 张卡片?

标签 javascript css reactjs jsx

我正在使用数组的 map 方法创建卡片组件以迭代容器中的状态。我想垂直排列卡片,每行只有 3 张卡片。

这是 ContentCard 功能组件:

import React from "react";
import CardLogo from "./../../../Assets/CardLogo.PNG";
import { Card, CardImg, CardText, CardBody, CardTitle, CardSubtitle, Button } from "reactstrap";
import classes from "./ContentCard.module.css";

const contentCard = props => (
  <Card className={classes.ContentCard}>
    <CardImg src={CardLogo} />
    <CardBody>
      <CardTitle>
        <b>{props.title}</b>
      </CardTitle>
      <CardSubtitle>from ₹{props.price}</CardSubtitle>
      <CardText>
        Typically 450-400 words, an e-book is perfect for your target audience ranging from
        prospective customers to users
      </CardText>
      <Button>Order</Button>
    </CardBody>
  </Card>
);
export default contentCard;

这是重用 ContentCard 组件的 ContentCards 父组件。在迭代此组件中的状态后,我应该如何设置卡片的样式以按照我上面所说的方式排列它们。

import React, { Component } from "react";
import classes from "./ContentCards.module.css";
import ContentCard from "./ContentCard/ContentCard";

class ContentCards extends Component {
  state = {
    content: [
      { title: "Blog / Article", price: "500" },
      { title: "Newsletter / Emailer", price: "1000" },
      { title: "Whitepaper", price: "2000" },
      { title: "e-book", price: "1000" },
      { title: "Report-Guide", price: "1000" },
      { title: "Product Description", price: "500" },
      { title: "Website Content", price: "1000" },
      { title: "Video Script", price: "1000" },
      { title: "Company Profile / Brochure", price: "2000" },
      { title: "Press Realise", price: "2000" }
    ]
  };

  render() {
    let card = this.state.content.map(cnt => (
      <ContentCard key={cnt.title} title={cnt.title} price={cnt.price} />
    ));

    return <div className={classes.ContentCards}>{card}</div>;
  }
}

export default ContentCards;

最佳答案

您可以为此使用 reactstrap 的 Col 和 Row 组件:

由于 bootstrap 使用 12 列系统,要在行中显示 3 列,我将 sm 设置为 4。

我制作了这个沙盒,它在整页大小下连续显示 3 张卡片。

https://codesandbox.io/s/bold-heisenberg-o9s2k

因为我没有类和 Logo ,所以我排除了它们。

关于javascript - 我应该如何设置卡片的样式,以便每行可以有 3 张卡片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58346760/

相关文章:

javascript - JS替换返回原字符串

javascript - 如何使用 CreateJS 将图像在 Canvas 上居中

javascript - 无法使用 webpack-dev-server 调用 React.Component.setState()

javascript - 类型错误 : Cannot read property 'then' of undefined on webservice

javascript - ObjectAssign 将 bool 转换为 object

javascript - 查找嵌套的 <li> 并修改它

javascript - 我的 ajax 跨域请求被 Chrome 取消

javascript - 如何使用一个 png 图像创建多个网站按钮(及其悬停)

CSS 仅在我手动将我的样式表拖到浏览器中的检查元素时显示

css - 从父类以外的类继承属性