在 Vuetify 上垂直居中显示内容

有没有办法在 Vuetify 垂直集中内容?

使用 text-xs-center助手类,内容只能水平居中:

<v-container grid-list-md text-xs-center>
<v-layout row wrap>
<v-flex xs12>
Hello
</v-flex>
</v-layout>

空气污染指数中,我测试了一些其他的助手类,比如 align-content-center,但是没有得到结果。

239899 次浏览

Update for new vuetify version

In v.2.x.x , we can use align and justify. We have below options for setup the horizontal and vertical alignment.

  1. PROPS align : 'start','center','end','baseline','stretch'

  2. PRPS justify : 'start','center','end','space-around','space-between'

<v-container fill-height fluid>
<v-row align="center"
justify="center">
<v-col></v-col>
</v-row>
</v-container>

For more details please refer this vuetify grid-system and you could check here with working codepen demo.


Original answer

You could use align-center for layout and fill-height for container.

Demo with v1.x.x

new Vue({
el: '#app'
})
.bg{
background: gray;
color: #fff;
font-size: 18px;
}
<link href="https://cdn.jsdelivr.net/npm/vuetify@1.2.2/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@1.2.2/dist/vuetify.min.js"></script>


<div id="app">
<v-app>
<v-container bg fill-height grid-list-md text-xs-center>
<v-layout row wrap align-center>
<v-flex>
Hello I am center to vertically using "align-center".
</v-flex>
</v-layout>
</v-container>
</v-app>
</div>

Here's another approach using Vuetify grid system available in Vuetify 2.x: https://vuetifyjs.com/en/components/grids

<v-container>
<v-row align="center">
Hello I am center to vertically using "grid".
</v-row>
</v-container>

In Vuetify 2.x, v-layout and v-flex are replaced by v-row and v-col respectively. To center the content both vertically and horizontally, we have to instruct the v-row component to do it:

<v-container fill-height>
<v-row justify="center" align="center">
<v-col cols="12" sm="4">
Centered both vertically and horizontally
</v-col>
</v-row>
</v-container>
  • align="center" will center the content vertically inside the row
  • justify="center" will center the content horizontally inside the row
  • fill-height will center the whole content compared to the page.

Still surprised that no one proposed the shortest solution with align-center justify-center to center content vertically and horizontally. Check this CodeSandbox and code below:

<v-container fluid fill-height>
<v-layout align-center justify-center>
<v-flex>
<!-- Some HTML elements... -->
</v-flex>
</v-layout>
</v-container>

<v-container> has to be right after <template>, if there is a <div> in between, the vertical align will just not work.

<template>
<v-container fill-height>
<v-row class="justify-center align-center">
<v-col cols="12" sm="4">
Centered both vertically and horizontally
</v-col>
</v-row>
</v-container>
</template>

For me, align="center" was enough to center FOO vertically:

<v-row align="center">
<v-col>FOO</v-col>
</row>

Without using rows or columns... you can center your main content of your vuetify app like this:

<v-app>
<v-main>
<v-container fluid fill-height>
<v-layout class="align-center justify-center">
<router-view></router-view>
</v-layout>
</v-container>
</v-main>
</v-app>

In my exemple the first v-for will take up all of the parent element's spacing, the fill-heigh will take up all the available height. This way we can use a second v-row to position as best as possible.

<v-row class="fill-height">
<v-row align="center" justify="center">
try TOO
align="start" || align="center" ||  align="end" <br>
justify="start" || justify="center" || justify="end"
</v-row>
</v-row>

Plase check the grids docs here: https://vuetifyjs.com/en/components/grids/#justify

FOR CARDS: Concise and works in v2+. This will get you a v-card with centered content HORIZONTALLY and VERTICALLY:

<v-card class="d-flex align-center justify-center"  min-height="250">
Content here...
</v-card>

codepen

For the new Vuetify v3.0.0.0-beta this worked for me:

<v-container class="fill-height">
<v-row align="center" justify="center" class="fill-height">
<p>Center</p>
</v-row>
</v-container>

In Vuetify v2 you can achieve that with minimal markup using just the d-flex, justify-center and align-center classes on the container:

<div
style="width: 100px; height: 100px; border: 1px solid black;"
class="d-flex justify-center align-center"
>
Hello
</div>