【问题标题】:How to make all cards same size and aligned in a row?如何使所有卡片大小相同并排成一行?
【发布时间】:2023-01-30 18:59:24
【问题描述】:

如何使所有卡片大小相同并排成一行? 我尝试使用不同的样式,但卡片看起来很乱。我不知道我的 .css 代码需要更改什么。 我希望它看起来与页面和其他卡片对齐

.card {
width: 90%;
margin: 1rem;
padding-bottom: 1rem;

text-align: center;
border: solid 2px #fcad02;
border-radius: 10px;
box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.5);
}

.card:hover {
box-shadow: 0 8px 16px 0 rgba(217, 218, 182, 0.2);
}

.card a {
color: white;
font-size: 1.2rem;
text-decoration: none;
background-color: #1c36fc;
padding: 0.5rem;
border-radius: 7px;
font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}

 /*     Display two cards per row at 576px breakpoint */
@media (min-width: 576px) {
.cards-container {
 /*     Set the display for the outer container to flex */
display: flex;
display: inline-block;

 justify-content: space-around;
  }

.card {
/*  Set the flex-basis so that two cards are displayed in each row. */
flex-basis: calc(50% - 3rem);
}
}

/*  Display four cards across one row at 992px breakpoint */
@media (min-width: 992px) {
.card {
flex-basis: calc(25% - 3rem);
padding-bottom: 1.5rem;
}
}

.cardHeader {
text-align: center;
  }

【问题讨论】:

  • 对于可以同时控制两个方向的 CSS Grid,这将是一个更好的任务。
  • 尝试为卡片提供 flex: 1 大小。这将使它们在 flexbox 中具有相同的大小,浏览器默认为 flex: auto

标签: css flexbox


【解决方案1】:

这是因为第一张卡片的标题既宽又占 2 行。所以你需要将卡片的字幕宽度设置为固定长度(或最大宽度),并考虑字幕可能的高度。例如,如果最多可以占用 4 行,则应在所有卡上预留空间。

【讨论】:

  • 答案太宽泛了。并且很难找到正确的宽度。这就是为什么你应该完全改变方法。
猜你喜欢
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
相关资源
最近更新 更多