Creating an adaptable grid system in Vue Material

I am working on a project where I am using Vue-Material to display user-inputted cards in a grid layout. While the cards are rendering correctly, I want to optimize the grid to make it flexible, justify the cards, and stagger them in a way that eliminates any gaps between them. This is what I aim to achieve:

https://i.sstatic.net/Ou7Ec.jpg

The code snippet below corresponds to the grid layout shown above:

<template>
  <div class="content">
    <div class="md-layout">
      <div
        v-for="post in posts.slice().reverse()" :key="post.id"
        class="md-layout-item md-size-20 md-xsmall-size-100"
      >
        <md-card>
          <md-card-content v-if="post.downloadUrl">
            <md-card-media>
              <img :src="post.downloadUrl" style="width: 100%"/>
            </md-card-media><br>
            <p>{{ post.content }}</p>
            <p>{{ post.timestamp | moment }}</p>
          </md-card-content>

          <md-card-content v-else>
            <p>{{ post.content }}</p>
            <p>{{ post.timestamp | moment }}</p>
          </md-card-content>
          <md-card-actions>
            <md-button class="md-icon-button md-info">
              <md-icon>favorite</md-icon>
            </md-button>
            <md-button class="md-icon-button md-info">
              <md-icon>share</md-icon>
            </md-button>
          </md-card-actions>
        </md-card>
      </div>
    </div>
  </div>
</template>

Any suggestions on how I can tweak my Vue-Material setup to achieve a gap-free arrangement of the cards? Your input is greatly appreciated!

https://i.sstatic.net/m4NvW.jpg

Example #3

https://i.sstatic.net/zuJPY.jpg

Answer №1

If you're looking to create a dynamic Masonry layout in Vue, consider using the vue-masonry plugin.

To install, run: npm i vue-masonry

Check it out in action here. Try out the code in this sandbox.

Answer №2

If you want to create a dynamic layout, consider using the grid system:

Vue.use(VueMaterial.default)

new Vue({
  el: '#app'
})
.grid-container {
  display: grid;
  max-width: 100%;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(10px, auto);
  grid-gap: 1rem;
  padding: 1rem;
}
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1,minimal-ui" name="viewport">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic|Material+Icons">
<link rel="stylesheet" href="https://unpkg.com/vue-material/dist/vue-material.min.css">
<link rel="stylesheet" href="https://unpkg.com/vue-material/dist/theme/default.css">

<body>
  <div id="app">
    <div class="grid-container">
      <md-card>
        <md-card-media>
          <img src="https://vuematerial.io/assets/examples/card-image-1.jpg" alt="People">
        </md-card-media>
        <md-card-header>
          <div class="md-title">Card without hover effect</div>
        </md-card-header>

        <md-card-content>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
        </md-card-content>

        <md-card-actions>
          <md-button>Action</md-button>
          <md-button>Action</md-button>
        </md-card-actions>
      </md-card>

      <md-card md-with-hover>
        <md-ripple>
          <md-card-header>
            <div class="md-title">Card with hover effect</div>
            <div class="md-subhead">It also have a ripple</div>
          </md-card-header>

          <md-card-content>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio itaque ea, nostrum odio. Dolores, sed accusantium quasi non.
          </md-card-content>

          <md-card-actions>
            <md-button>Action</md-button>
            <md-button>Action</md-button>
          </md-card-actions>
        </md-ripple>
      </md-card>
      <!-- More cards here -->
    </div>
    &copy Image linked from https://vuematerial.io/
  </div>

  <script src="https://unpkg.com/vue"></script>
  <script src="https://unpkg.com/vue-material"></script>

To learn more about organizing your layout effectively, explore the possibilities of CSS grid: https://developer.mozilla.org/en-US/docs/Web/CSS/grid

A DIFFERENT APPROACH

An alternative method is to implement

Andy Barefoot's approach: https://medium.com/@andybarefoot/a-masonry-style-layout-using-css-grid-8c663d355ebb (Codepen available at https://codepen.io/andybarefoot/pen/QMeZda),

or refer to other options listed here: https://css-tricks.com/piecing-together-approaches-for-a-css-masonry-layout/

Due to length restrictions, I am unable to include the entire code snippet in this response.

Answer №3

Thank you to everyone who provided feedback today. However, after some research, I found a plugin that perfectly fits my needs: https://www.npmjs.com/package/vue-masonry-css

All I had to do was insert the v-for loop container and cards within the code snippet below:

<masonry
  :cols="3"
  :gutter="30"
  >
  <div v-for="(item, index) in items" :key="index">Item: {{index + 1}}</div>
</masonry>

This is exactly what I wanted to achieve:

https://i.sstatic.net/ivWM0.jpg

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

Modifying text size using JavaScript string manipulation

I'm currently experimenting with a countdown script, but I'm struggling to change the size of the numbers displayed. Can anyone help me find where I can adjust the font and font size in this script? var eventdate = new Date("February 20, 2014 11 ...

The controller's AngularJS function seems to be unresponsive

Issue with AngularJs ng-click Event I'm attempting to utilize the GitHub Search-API. When a user clicks the button, my Controller should trigger a search query on GitHub. Here is my code: HTML: <head> <script src="js/AngularJS/angula ...

Creating a form generator with multi-column field layout in Vue.js

Vuejs vue-form-generator typically creates form layouts with a single column view, displaying one field per row. <vue-form-generator :schema="schema" :model="model" :options="formOptions"></vue-form-generator> For reference, here is a sample ...

Introduce an additional parameter to the Prestashop Cart entity

After setting up a radiobox "stock_action" in the Cart, I need to send its value to the Cart object for additional order costs. In the Cart Override, the $stock_action variable has been added: public $stock_action; /** * @see ObjectModel::$defi ...

Add a variable to the configuration

Here is my configuration setup angular.module('moduleApp.config') .config(['$translateProvider', '$languageSupportProvider', function($translateProvider, $languageSupportProvider) { // Need to access data from myS ...

Having trouble navigating to the link using Selenium WebDriver

I encountered an issue when trying to click on a link as it displayed an "Element Not Found" message. Below is the HTML code I used: <a id="expTo" class="formblue_link padRight10 exportLinkActive" style="display: block; margin-left: -50px; margin-b ...

Tips on transferring dynamically generated results to a user-friendly print window

After users complete a quiz, they receive their results. The client now wants to implement a "Print Results" feature that opens in a new window with customized CSS. I'm trying to figure out how to transfer the results to the new window using JavaScri ...

Preparing the format/serialization of a JQuery JSON array prior to submitting it

Looking at the JSON structure I have: { "firstArrayOfJSON": [ { "something" : "something" }, { "another" : "another" }, { "secondArrayOfJson" : [ ...

Personalized message on Vuetify slider

Is there a way to customize the message displayed on a Vuetify slider? https://i.stack.imgur.com/6Xdsm.png I want to include text like 'The number is: 8' <v-slider class='slider' step="1" thumb- ...

Validate fields by iterating through an object and considering three data points for each field

The struggle is real when it comes to coming up with a title for this question. Suggestions are welcomed! When it comes to field validation, I require three data elements per field: variable name, element ID, and whether it is required or not. Although I ...

The behavior of having two submit buttons within the $document.ready(function) in Jquery

In my code, I have implemented the behavior of two buttons, button1 and button2, within a $(document).ready(function). Whenever either button is clicked, an alert() function should be triggered. However, it seems that only button2 is functioning properly w ...

After reloading the page, Nuxt dynamic routes are displaying a 404 error

Hey there! I'm currently diving into a project that involves using nuxt js, and it's all new to me. I've set it up in spa mode without any modifications in the nuxt config file, just sticking with the default settings. Here's how I&apos ...

Ways to create a smooth transition in element height without a known fixed target height

Is it possible to create a smooth height transition for an element when the target height is unknown? Replacing height: unset with height: 100px allows the animation to work, but this presents a problem as the height needs to be based on content and may v ...

Issues encountered when implementing server-sent events in a project built with Node.js and React

I've been working on implementing server-sent-events into my Node.js and React application. After doing some research and following tutorials online, I found this particular site to be very helpful and straightforward. The main objective is to have a ...

Angular promise did not assign a value to a variable

Can someone assist me with a problem I'm facing? After the first callback, the variable doesn't seem to change as expected. Below is my code snippet: this.handlerLocalDef = function(defer) { var hash = {}; defer.then( ...

`Struggling with managing callbacks, handling errors, and working with MongoDB`

I recently revamped my code for an application that handles adding companies to a database. In the past, my code was messy and unorganized, so I decided to structure it properly by introducing routes, a controller, and a data access object. Here is how my ...

Sending back an array within the onOpen function

I am currently in the process of developing a chat application. At this stage, users are able to send messages to each other and I have successfully stored these messages in my database. Now, my objective is to display the stored messages from the database ...

Ways to block past dates on bootstrap date picker starting from the current date and how to prevent dates beyond 90 days in the future from being selected on the

I am currently facing an issue with disabling previous dates from the current date in my code. While the functionality works well for the "From Date" date picker, I am having trouble implementing the same restriction for the "To Date" date picker after 90 ...

JavaScript only collapsible navigation bar

I have implemented a collapsible navbar using Bootstrap 4 framework according to the documentation provided at this link. The navbar and its contents collapse on small screens, including smartphones, by adding the class .collapse.navbar-collapse. You can ...

displaying and activating element using jQuery

I'm currently working on setting up a notification system for my website but seem to be encountering some issues that I can't quite pinpoint. Essentially, I have a link that triggers a JavaScript function upon being clicked. This function is mean ...