【问题标题】:How to get card group inline instead one on top of each other如何让卡组内联而不是一个在彼此之上
【发布时间】:2022-01-12 02:21:15
【问题描述】:

我试图让一个卡片组内联,而不是一个在另一个之上。文档说“card-group”类应该这样做,但似乎没有任何效果。我将在下面附上示例代码和结果图片。

<div class="card-group">       
          {candidateData.map((candidate) => (
            <div class="card">
              <Card sx={{ maxWidth: 145 }}>
                <CardActionArea>
                  <CardMedia
                    component="img"
                    height="140"
                    src={candidate.img}
                    alt="uncle ruckus"
                  />
                  <CardContent>
                    <Typography gutterBottom variant="h6" component="div">
                      {candidate.name}
                    </Typography>
                    <Typography variant="body5" color="text.secondary">
                      {candidate.politics}
                    </Typography>
                  </CardContent>
                </CardActionArea>
              </Card> 
            </div>
          ))}
      </div>

这里是组件生成的候选数据 json

{
    "main": {
        "candidates":[
            {
              "name":"Uncle Ruckus",
              "politics":"R-District 21",
              "img":"./Candidates/uncleruckus.jpg"
            },
            {
              "name":"Uncle Ruckus",
              "politics":"R-District 21",
              "img":"./Candidates/uncleruckus.jpg"
            }
          ]
    }
}

这是卡片在另一个之上而不是内联(并排)的结果

【问题讨论】:

    标签: javascript html css reactjs card


    【解决方案1】:

    解决这个问题有两种可能的方法:

    1。使用 Flex 样式

    您可以通过将此样式赋予卡片的父/包装器/容器来修复它

    display: flex;
    flex-direction: row;
    

    所以它可以是这样的:

    .card {
      /* Add shadows to create the "card" effect */
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
      transition: 0.3s;
      margin: 0px 12px;
    }
    
    /* On mouse-over, add a deeper shadow */
    .card:hover {
      box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
    }
    
    /* Add some padding inside the card container */
    .container {
      padding: 2px 16px;
    }
    
    .card-group {
      display: flex;
      flex-direction: row;
    }
    
    .card {
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
      transition: 0.3s;
      border-radius: 5px; /* 5px rounded corners */
    }
    
    /* Add rounded corners to the top left and the top right corner of the image */
    img {
      border-radius: 5px 5px 0 0;
    }
    <div class="card-group">
      <div class="card">
        <img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt="Avatar" style="width:100%">
        <div class="container">
          <h4><b>Card 1</b></h4>
        </div>
      </div>
      <div class="card">
        <img src="https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8dmlld3xlbnwwfHwwfHw%3D&w=1000&q=80" alt="Avatar" style="width:100%">
        <div class="container">
          <h4><b>Card 2</b></h4>
        </div>
      </div>
    </div>

    2。使用 css 网格(像表格一样放置)

    把这个样式放到card-group:

    display: grid;
    grid-template-columns: auto auto;
    grid-column-gap: 12px;
    

    上面的grid-template-columns 可以帮助你决定一排有多少张牌,而且是固定的。

    .card {
      /* Add shadows to create the "card" effect */
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
      transition: 0.3s;
      margin: 0px 12px;
    }
    
    /* On mouse-over, add a deeper shadow */
    .card:hover {
      box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
    }
    
    /* Add some padding inside the card container */
    .container {
      padding: 2px 16px;
    }
    
    .card-group {
      display: grid;
      grid-template-columns: auto auto;
      grid-column-gap: 12px;
    }
    
    .card {
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
      transition: 0.3s;
      border-radius: 5px; /* 5px rounded corners */
    }
    
    /* Add rounded corners to the top left and the top right corner of the image */
    img {
      border-radius: 5px 5px 0 0;
    }
    <div class="card-group">
      <div class="card">
        <img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt="Avatar" style="width:100%">
        <div class="container">
          <h4><b>Card 1</b></h4>
        </div>
      </div>
      <div class="card">
        <img src="https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8dmlld3xlbnwwfHwwfHw%3D&w=1000&q=80" alt="Avatar" style="width:100%">
        <div class="container">
          <h4><b>Card 2</b></h4>
        </div>
      </div>
    </div>

    更多关于网格的参考:https://www.w3schools.com/css/css_grid.asp

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 2020-01-16
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多