What is the best way to create 7 or 8 column grids with Vuetify's v-row and v-col components?

I understand that vuetify's grid system is based on a 12-column flex-box layout, but I would like to customize it to have 7 or 8 columns by default instead of the usual 12. In the code snippet below, you can see my attempt:

<v-row>
  <v-col 
    v-for="(n, index) in 15" 
    :key="index"
    cols="7" 
    sm="7" 
    md="3" 
    lg="1" 
  >
    <v-card>
      <v-card-title>{{ index }}</v-card-title>
    </v-card>
  </v-col>
</v-row>

After reading through the documentation, I thought that setting cols="7" would achieve the desired outcome, but it doesn't seem to be working as expected.

Is it actually possible to create a grid with 7 or 8 columns in Vuetify?

Answer №1

  1. Vuetify's grid system uses 12 columns that are fixed and cannot be changed.
  2. The props like cols, sm, and lg determine the width of the v-col in terms of columns (i.e., how many out of the 12 columns should each column take). For instance, col refers to extra small screens and larger, while sm encompasses small screens and up. Refer to media breakpoints in the documentation for more information...
  3. The values assigned to these props must be whole numbers. This is because they are used during rendering to assign predefined CSS classes. For example, using lg="1" will result in the assignment of the ".col-lg-1" CSS class.

If you want a specific number of columns x where 12 is not evenly divisible by x (without a remainder), such as 5 or 7, you have two options (examples given for x = 7):

  1. You can define 7 columns each with a width of 1 column and distribute the white space around them (using justification and possibly offsets) ...however, this may result in excessive white space.
  2. Alternatively, you can utilize custom CSS and adjust it manually with media queries to ensure responsiveness and aesthetics across all screen sizes (source)
.custom7cols {
  width: 14%;
  max-width: 14%;
  flex-basis: 14%;
}

      <v-row>
        <v-col 
          v-for="(n, index) in 15" 
          :key="index"
          class="custom7cols" 
        >
          <v-card>
            <v-card-title>Column {{ n }}</v-card-title>
          </v-card>
        </v-col>
      </v-row> 

Demo

Personal note 1: Carefully consider whether this approach is truly necessary.

Personal note 2: I'm personally not a fan of Vuetify's grid system. While I understand how Flexbox functions, Vuetify's grid is based on Bootstrap and mapping from Vuetify to simple Flexbox can be challenging if you're not familiar with how Bootstrap operates (which I am not and prefer not to delve into).

Answer №2

Instead of using v-col, you can opt for v-flex which displays 6 columns in a row. See the code snippet below:

new Vue({
    el: '#app',
    vuetify: new Vuetify(),
})
<div id="app">
    <v-app id="inspire">
        <v-container class="grey lighten-5">
            <v-row no-gutters>
                <template>

                    <v-flex xs7 sm7 md3 lg2 v-for="(n,index) in 15" :key="n">
                        <v-card>
                            <v-card-title>{{ index }}</v-card-title>
                        </v-card>
                    </v-flex>
                </template>
            </v-row>
        </v-container>
    </v-app>
</div>

Answer №3

Hey there, I wanted to express my gratitude for this helpful post despite the time that has passed since it was posted.

It seems we were facing a similar challenge as I also needed 7 or 8 columns but couldn't figure it out until stumbling upon your post.

I am particularly thankful to Michal Levy for their code, which inspired me to make some modifications for responsiveness. I hope my updated version can benefit others as well.

CSS:

.custom4cols {
  width: 25%;
  max-width: 25%;
  flex-basis: 25%;
}
.custom7cols {
  width: 14%;
  max-width: 14%;
  flex-basis: 14%;
}

JS:

<v-row>
    <v-col 
      v-for="(n, index) in 15" 
      :key="index"
      :class="{
        custom4cols: $vuetify.breakpoint.xs,
        custom7cols: $vuetify.breakpoint.smAndUp
      }" 
    >
      <v-card>
        <v-card-title>Column {{ n }}</v-card-title>
      </v-card>
    </v-col>
  </v-row>

Warm regards.

Answer №4

Encountering the same issue, I discovered a workaround. While you can adjust col-12 to cols-3 in your print layout, this sacrifices responsiveness for small screens <600px.

My solution involves declaring a @media print and overriding col-12 with other breakpoint classes. (Print always defaults to the smallest breakpoint CSS class)

For example, if using col 12 but sm-3, use sm-3 flex/width

@media print {
  div.col-sm-3.col-12 {
      flex: 0 0 25%;
      max-width: 25%;
   }
   div.col-sm-6.col-12 {
     flex: 0 0 50%;
     max-width: 50%;
   }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered an issue while executing the next build process

Every time I run npm next build, it throws an error message. Despite my efforts to search for a solution online and installing the "extract-text-webpack-plugin," the issue persists. The error being thrown is as follows: > next build (node:8136) Depre ...

Tips for concealing the Google Chrome status bar from appearing on a webpage

I have been intrigued by the rise of Progressive Web Apps (PWAs) and I am eager to dive into understanding them better. One common feature I have noticed in PWAs is the ability to hide the browser chrome, including the URL bar, back button, search fields, ...

How can I add content to the body of a modal in Bootstrap 3?

My application has a button that, when clicked, is supposed to trigger a modal popup containing some data. I want the data in the modal to come from another application via a PHP file accessed through a URL. How can this be achieved? <?php echo '& ...

Can JavaScript be used to update/override Prototype version 1.4 to version 1.7 on a different website?

(I'm uncertain about the best way to phrase this question, feel free to make changes). I am in the process of embedding a JS widget onto a different website that is using Prototype.js Version 1.4. I have incorporated jQuery into my widget and have it ...

Loop through the data string in Ajax using a For Loop to populate it

I am currently working on a loop that inserts select tags based on the number of rows. Each select tag will have an ID like selID0, selID1, selID2, and so on. My goal is to call an AJAX function to determine which tag is not selected when the submit button ...

What is the best method for designing a custom mask for the jQuery Masked Input Plugin that accommodates alphanumeric characters, spaces, and accented

Looking for a way to make a custom mask in jQuery Masked Input Plugin that allows alphanumeric characters, spaces, and accented characters? This is what I currently have: $.mask.definitions["A"] = "[a-zA-Z0-9 ]"; $("#input").mask("A?AAAAAAA"); However, ...

I need help figuring out how to navigate between different pages in my Vue-cli app that has multiple pages configured

After following the guide on https://cli.vuejs.org/config/#pages, I successfully configured vue-cli3 to build a multiple page application. My project consists of 2 static pages, and I set up the vue.config.js file as shown below. However, when running the ...

Avoiding multiple ajax requests due to multiple clicks

I have a blog on WordPress that has a jQuery code allowing users to click a bookmark link to save the post as a bookmark. Each post displays a total bookmark counter in this format: "Bookmarked (5)". The issue is that when a user clicks multiple times on t ...

Tips for structuring classes in an Angular project

What is the best approach for handling API responses using classes in various programming languages like TypeScript, JavaScript, Java, or others? Consider an application that requires three resources: Account (API: /account/:id) Car (API: /account/:id/c ...

What is the process for linking to a backend on a distinct port in Next.js?

I am working on a project with Next.js and I am facing a challenge in connecting to a backend server that is running on a different port. The frontend of my application is on port 3000, while the backend is on port 8000. My goal is to interact with the bac ...

Selenium and PhantomJS are having trouble interpreting JavaScript code

Currently experimenting with Selenium and PhantomJS for Java search tasks, I'm facing an issue where PhantomJS fails to activate javascript. My goal is to access a webpage that utilizes javascript. Previously, this method worked well for me, but now I ...

What causes delayed state updates in React/NextJS until another state is updated or a fast refresh occurs?

UPDATE: Version 13.3.0 is coming soon! In my code, I have a state variable named localArray that I need to update at a specific index. To achieve this, I decided to create a temporary array to make modifications and then set the state with the updated val ...

Ways to ensure the CSS style remains active even after clicking the button

Is there a way to make the effect appear only when the button is clicked, then disappear after it's released? I've seen examples using addEventListener and directly changing the style with btn.style.color="" (reference) or using btnEl.c ...

Can anyone offer a straightforward method for obtaining JavaScript output in Java within the Eclipse environment?

Currently, I am attempting to retrieve the return value from a JavaScript function that I have created. Although I can achieve this using a complex method involving listeners, I am interested in utilizing Eclipse's Browser.evaluate method. For practi ...

Button to access overlay menu on my map with OpenLayers 3

I am trying to add a menu button on top of my map that, when clicked, will display a window. I have created the map div and the overlayMenu button div, but unfortunately, the button is not showing up where I want it to. I would like the button to be locate ...

Ways to retrieve the complete user object in passport

Recently, I've been diving into utilizing Express authentication with Passport and React for the frontend. While working on this project, a question came up: How can I access the entire authenticated user object? This is what my database model looks l ...

Objects that are included into an array will end up with an undefined value

Currently, I am in the process of coding a command for my Discord bot that involves adding items to an array of objects stored within a JSON file. To achieve this functionality, I have implemented the following code snippet: let rawdata = fs.readFileSync(& ...

What is the best way to create a straightforward menu for my HTML game?

I have been working on a basic HTML game and it seems to be functioning more or less. I'm wondering if it's possible to create a simple menu with an image in the background and a "click to start" button within the same file. Any suggestions or ti ...

I encountered an issue when trying to dynamically add a text field in Angular 2. The error message received was "ERROR TypeError: Cannot read property '0' of

I am currently utilizing Angular2 in my project, and I am attempting to dynamically add a text field. However, I keep encountering an error: Error Message (TS): ngOnInit() { this.myForm = this._fb.group({ myArray: this._fb.array([ ...

Restricting the fields in a $lookup operation in MongoDB

My Mongo object follows the schema provided below. { "_id" : "59fffda313d02500116e83bf::5a059c67f3ff3b001105c509", "quizzes" : [ { "topics" : [ ObjectId("5a05a1e1fc698f00118470e2") ], " ...