You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vuetify中为同行卡片添加间距并实现响应式n×3布局

解决Vuetify卡片矩阵的间距与响应式问题

嘿,我来帮你搞定这个问题!其实不用自己写额外的CSS,Vuetify本身就提供了内置的间距工具类,既能轻松添加卡片间的间距,还能完美保持响应式效果。咱们来一步步调整你的代码:

核心改动点

  1. 用单个带wrap属性的v-layout包裹所有卡片:这样Vuetify会自动帮你处理换行,不用手动拆分多个v-layout,行间距也更容易统一控制。
  2. 添加Vuetify内置间距类:给每个卡片容器(v-flex或v-card)加上ma-2(margin all 2)或者pa-2(padding all 2)这类类名,就能实现均匀的间距,而且响应式场景下会自动适配。
  3. 可选:给v-container加fluid属性:让布局更贴合视口宽度,优化响应式体验。

修改后的完整代码

<template lang="html">
  <v-container fluid>
    <!-- 用单个v-layout wrap来自动换行 -->
    <v-layout wrap>
      <!-- 每个v-flex添加ma-2类,给卡片添加左右上下间距 -->
      <v-flex xs12 md4 ma-2>
        <v-card>
          <v-img class="white--text" height="200px" src="https://cdn.vuetifyjs.com/images/cards/docks.jpg">
            <v-layout>
              <v-flex xs12 align-end flexbox>
                <span class="headline">Top 10 Australian beaches</span>
              </v-flex>
            </v-layout>
          </v-img>
          <v-card-title>
            <div>
              <span class="grey--text">Number 10</span><br>
              <span>Whitehaven Beach</span><br>
              <span>Whitsunday Island, Whitsunday Islands</span>
            </div>
          </v-card-title>
          <v-card-actions>
            <v-btn flat color="orange">Share</v-btn>
            <v-btn flat color="orange">Explore</v-btn>
          </v-card-actions>
        </v-card>
      </v-flex>

      <v-flex xs12 md4 ma-2>
        <v-card>
          <v-img class="white--text" height="200px" src="https://cdn.vuetifyjs.com/images/cards/docks.jpg">
            <v-layout>
              <v-flex xs12 align-end flexbox>
                <span class="headline">Top 10 Australian beaches</span>
              </v-flex>
            </v-layout>
          </v-img>
          <v-card-title>
            <div>
              <span class="grey--text">Number 10</span><br>
              <span>Whitehaven Beach</span><br>
              <span>Whitsunday Island, Whitsunday Islands</span>
            </div>
          </v-card-title>
          <v-card-actions>
            <v-btn flat color="orange">Share</v-btn>
            <v-btn flat color="orange">Explore</v-btn>
          </v-card-actions>
        </v-card>
      </v-flex>

      <v-flex xs12 md4 ma-2>
        <v-card>
          <v-img class="white--text" height="200px" src="https://cdn.vuetifyjs.com/images/cards/docks.jpg">
            <v-layout>
              <v-flex xs12 align-end flexbox>
                <span class="headline">Top 10 Australian beaches</span>
              </v-flex>
            </v-layout>
          </v-img>
          <v-card-title>
            <div>
              <span class="grey--text">Number 10</span><br>
              <span>Whitehaven Beach</span><br>
              <span>Whitsunday Island, Whitsunday Islands</span>
            </div>
          </v-card-title>
          <v-card-actions>
            <v-btn flat color="orange">Share</v-btn>
            <v-btn flat color="orange">Explore</v-btn>
          </v-card-actions>
        </v-card>
      </v-flex>

      <v-flex xs12 md4 ma-2>
        <v-card>
          <v-img class="white--text" height="200px" src="https://cdn.vuetifyjs.com/images/cards/docks.jpg">
            <v-layout>
              <v-flex xs12 align-end flexbox>
                <span class="headline">Top 10 Australian beaches</span>
              </v-flex>
            </v-layout>
          </v-img>
          <v-card-title>
            <div>
              <span class="grey--text">Number 10</span><br>
              <span>Whitehaven Beach</span><br>
              <span>Whitsunday Island, Whitsunday Islands</span>
            </div>
          </v-card-title>
          <v-card-actions>
            <v-btn flat color="orange">Share</v-btn>
            <v-btn flat color="orange">Explore</v-btn>
          </v-card-actions>
        </v-card>
      </v-flex>
    </v-layout>
  </v-container>
</template>

额外说明

  • 如果你想调整间距大小,可以把ma-2改成ma-1(更小)或者ma-3(更大),Vuetify的间距类支持0-5的尺寸等级。
  • 如果你只想给卡片添加水平间距,可以用mx-2(x轴左右margin),垂直间距用my-2(y轴上下margin),灵活度很高。
  • 单个v-layout wrap会自动根据屏幕宽度换行,在移动端(xs12)会自动变成单列,完全符合响应式要求。

内容的提问来源于stack exchange,提问作者abhigyan nayak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:50:31