Center content vertically on Vuetify

后端 未结 6 1598
生来不讨喜
生来不讨喜 2021-01-31 07:46

Is there a way to center content vertically in Vuetify?

With the text-xs-center helper class, the content gets centered horizontally only:

&         


        
相关标签:
6条回答
  • 2021-01-31 07:49

    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>

    0 讨论(0)
  • 2021-01-31 07:50

    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>
    
    0 讨论(0)
  • 2021-01-31 07:54

    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.
    0 讨论(0)
  • 2021-01-31 07:59

    <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>
    
    0 讨论(0)
  • 2021-01-31 08:10

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

    <v-row align="center">
      <v-col>FOO</v-col>
    </row>
    
    0 讨论(0)
  • 2021-01-31 08:11

    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>
    
    0 讨论(0)
提交回复
热议问题