我正在尝试制作一个包含卡片的响应网格,但是,我一辈子都不能弄清楚为什么卡片不能正确地放入列中。当使用grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
时,卡片由于不能正确地放入列中而最终溢出容器。我也尝试过grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
,它解决了容器溢出的问题,但是,卡片仍然不适合正确的列,虽然他们适合更好,我不想增加空白列时,屏幕的宽度允许它。
应用程序代码:
<script setup>
import { RouterView } from "vue-router";
import CardList from "./components/CardList.vue";
import NavBar from "./components/NavBar.vue";
</script>
<template>
<div class="container">
<NavBar />
<CardList />
<main class="main-body">
<RouterView />
</main>
</div>
</template>
<style scoped>
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main > .main-body {
align-items: center;
justify-content: center;
}
</style>
卡片列表代码:
<template>
<div class="cards-container">
<HoverLogo
v-for="item in items"
class="card-container"
:key="item"
:title="item.title"
:points="item.points"
/>
</div>
</template>
<script>
import Card from "./Card.vue";
export default {
name: "CardList",
components: { Card },
data() {
return {
items: [
{
title: "Annual Leave",
points: [
"9 days left to take",
"4 sick days taken",
"8 days of upcoming annual leave",
],
},
{
title: "Benefits",
points: ["Gym memberhip", "Health Insurance", "Wellbeing centre"],
},
{
title: "Payroll",
points: ["Payslips", "Tax", "Contact Payroll"],
},
{
title: "Pension",
points: ["Pension paid so far", "Change Pension Contribution"],
},
],
};
},
};
</script>
<style>
.cards-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
align-content: center;
justify-content: center;
padding-left: 30px;
padding-right: 30px;
width: 100%;
background: linear-gradient(#141e30, #243b55);
column-gap: 100px;
}
.cards-container:hover .card {
opacity: 0.5;
}
@media (max-width: 500px) {
.cards-container {
display: flex;
flex-direction: column;
align-items: center;
}
}
</style>
卡代码:
<template>
<div class="body">
<div class="container">
<div class="dark">
<div class="face face1">
<h3>{{ title }}</h3>
</div>
<div class="face face2">
<div class="content">
<p v-for="point in points" :key="point" class="points">
{{ point }}
</p>
<button @click="signUp">
<span></span>
<span></span>
<span></span>
<span></span>
Read More
</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Card",
props: {
title: {
type: String,
required: true,
},
points: {
type: Array,
required: true,
},
},
};
</script>
<style>
.body {
display: flex;
margin: 50px;
padding: 0;
background: transparent;
justify-content: center;
align-items: center;
font-family: arial;
}
.body > .container {
position: relative;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.dark:hover .face {
transform: translateY(0);
}
.dark:hover .face h3 {
text-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4,
0 0 100px #03e9f4;
}
.face {
width: 300px;
height: 200px;
transition: 0.7s;
}
.face1 {
font-family: Blanka;
color: #03e9f4;
position: relative;
background: #0d1520;
display: flex;
justify-content: center;
align-content: center;
align-items: center;
z-index: 1;
transform: translateY(100px);
}
.face1 > img {
width: 100%;
height: 100%;
}
.face.face1 .content {
transition: 0.5s;
text-align: center;
}
.face.face1 .content .h3 {
font-size: 1em;
text-align: center;
}
.face.face1 .content .a {
transition: 0.5s;
}
.face.face2 {
position: relative;
background: whitesmoke;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
box-sizing: border-box;
transform: translateY(-100px);
}
.face2 button {
background: #0d1520;
position: relative;
display: inline-block;
padding: 10px 20px;
color: #03e9f4;
font-size: 10px;
text-decoration: none;
text-transform: uppercase;
overflow: hidden;
transition: 0.5s;
margin-top: 40px;
letter-spacing: 1px;
border: none;
}
.face2 > .content > .points {
color: #0d1520;
font-family: Blanka;
}
.face2 button:hover {
background: #03e9f4;
color: #fff;
border-radius: 5px;
box-shadow: 0 0 2px #03e9f4, 0 0 10px #03e9f4, 0 0 20px #03e9f4,
0 0 100px #03e9f4;
cursor: pointer;
}
.face2 button span {
position: absolute;
display: block;
}
.face2 button span:nth-child(1) {
top: 0;
left: -100%;
width: 100%;
height: 2px;
background: linear-gradient(90deg, transparent, #03e9f4);
animation: btn-anim1 2s linear infinite;
}
@keyframes btn-anim1 {
0% {
left: -100%;
}
50%,
100% {
left: 100%;
}
}
.face2 button span:nth-child(2) {
top: -100%;
right: 0;
width: 2px;
height: 100%;
background: linear-gradient(180deg, transparent, #03e9f4);
animation: btn-anim2 2s linear infinite;
animation-delay: 0.5s;
}
@keyframes btn-anim2 {
0% {
top: -100%;
}
50%,
100% {
top: 100%;
}
}
.face2 button span:nth-child(3) {
bottom: 0;
right: -100%;
width: 100%;
height: 2px;
background: linear-gradient(270deg, transparent, #03e9f4);
animation: btn-anim3 2s linear infinite;
animation-delay: 1s;
}
@keyframes btn-anim3 {
0% {
right: -100%;
}
50%,
100% {
right: 100%;
}
}
.face2 button span:nth-child(4) {
bottom: -100%;
left: 0;
width: 2px;
height: 100%;
background: linear-gradient(360deg, transparent, #03e9f4);
animation: btn-anim4 2s linear infinite;
animation-delay: 1.5s;
}
@keyframes btn-anim4 {
0% {
bottom: -100%;
}
50%,
100% {
bottom: 100%;
}
}
</style>
显示自动调整问题的图片:
- 编辑:**
代码笔链接
1条答案
按热度按时间wbrvyc0a1#
根据@yoduh的评论,我弄明白了这个问题。因为Card.Vue上的固定宽度为300 px:
结合Card-List.vue上的最小值200 px minmax:
这是造成的问题,因此,我改变了最小值的最小值为300 px,并修复了这个问题。