【问题标题】:Vuetify - make square v-card with centered text (both axis)Vuetify - 制作带有居中文本的方形 v-card(两个轴)
【发布时间】:2021-02-11 04:20:52
【问题描述】:

我正在尝试制作一张卡片文字在正方形中间居中的 v-card。我尝试使用 v-spacer 和 vuetify 附带的其他一些 CSS 类,但不幸的是,虽然文本保持水平居中,但我无法使其也垂直居中。

除了中间文本的垂直居中之外,这段代码几乎可以正常工作

  <v-container fluid>
    <v-row>
      <v-col cols="6" sm="4">
        <v-card rounded color="info">
          <v-responsive aspect-ratio="1">
            <v-card-title>
              Top left - correct
            </v-card-title>
            <v-card-text class="text-center white--text">
              middle center
            </v-card-text>
            <v-card-actions class="justify-center white--text">
              bottom center
            </v-card-actions>
          </v-responsive>
        </v-card>
      </v-col>
    </v-row>
  </v-container>

这是一个带有示例的 JS 小提琴:https://jsfiddle.net/mrpquke4/3/

尝试使用上面的示例调整浏览器窗口的大小,您将看到 v-card 在增长或缩小时保持方形(根据需要),文本保持水平居中(根据需要),文本未垂直居中(问题)。

JSFiddle:

期望的结果:

【问题讨论】:

    标签: vue.js vuetify.js


    【解决方案1】:

    使用这个css:

    .v-responsive__content {
        display: flex;
        flex-direction: column;
        align-items: center;
        // justify-content: center; // this will make everything vertical center
        justify-content: space-between
    }
    
    
    .v-responsive__content > div {
        width: 100%;
    }
    

    您可以为任何 css 冲突命名空间类:

    .my-card .v-responsive__content { ... }
    

    【讨论】:

    • 这移动了中心的所有内容。我需要 v-card-title (第一行)和 v-card-actions (最后一行)来保持顶部和底部并仅居中 v-card-text。这是您的建议:jsfiddle.net/8wr1xjzf
    • @luriG 看看这是否有效justify-content: space-between?如果这对您没有帮助,请在 Skype 上给我打电话,让我们解决它。 Skype ID:syed_haroon
    • 做到了@Syed!如果您可以使用justify-content: space-between 为其他任何搜索此内容的人更新答案,那就太好了。谢谢!
    【解决方案2】:

    您可以在v-col 上使用align-self(如果您想放在单独的行中,则可以在v-row 上使用align)用于水平方向,在v-col 上使用offset 用于垂直方向

      <v-row>
        <v-col align-self="center" cols="6">
          <v-card class="pa-3">align : start</v-card>
        </v-col>
    
        <v-col align-self="center" cols="6" offset="3">
          <v-card class="pa-3 text-center">align : center</v-card>
        </v-col>
    
        <v-col align-self="end" cols="6" offset="6">
          <v-card class="pa-3 text-end">align : end</v-card>
        </v-col>
      </v-row>
    

    这里是更新的 jsfiddle:https://jsfiddle.net/tunzLcw5/

    【讨论】:

    • 这不起作用:文本顶部居中对齐,而不是在卡片中间。给卡片添加一个高度,你会看到文本停留在顶部附近,而不是在卡片的中心。这是示例:jsfiddle.net/p0zuv756
    • 将 v-row v-col 放入您的卡片中,或者将 v-card-title 和 v-card-actions 与 class="justify-center" jsfiddle.net/f7bck1go 一起使用
    • 我看到你添加了style="height:200px",没有它,文本不居中。由于 vcard 的纵横比在我的示例(正方形)中为 1,因此它的高度会根据页面的宽度而变化,因此我无法对 style="height:200px" 进行硬编码。您知道如何在高度动态时使其始终居中吗?
    • 应该是&lt;v-card class="d-flex justify-center"&gt;,而不是内文项fiddle
    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2021-12-10
    • 2018-12-01
    • 2021-05-01
    • 2020-08-21
    • 1970-01-01
    相关资源
    最近更新 更多