Tips for aligning the router-link perfectly with an image in Vue.js 3

I recently set up a flex grid gallery and now I'm trying to figure out how to turn only the images into router-links rather than the entire gallery-item. Here's an example of my Vue component .vue code:

<template>
    <div class="gallery">
        <div class="gallery-item">
            <router-link to="/cat"><img src="../assets/cat.jpg" alt="cat"></router-link>
        </div>
    </div>
</template> 

Below is the CSS styling for the grid gallery:

<style scoped>
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-gap: 20px;
  margin: 20px;
}

.gallery-item {
  background-color: aquamarine;
  max-height: 300px;
}

img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
</style>

Answer №1

Ensure router-link is styled with a class that includes display: block, or you can consolidate the styles like so:

<template>
    <div class="gallery">
        <router-link to="/cat" class="gallery-item">
            <img src="../assets/cat.jpg" alt="cat">
        </router-link>
    </div>
</template> 

<style scoped>
    .gallery {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
      grid-gap: 20px;
      margin: 20px;
    }
    .gallery-item {
        background-color: aquamarine;
        max-height: 300px;
        display: block;
    }

    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
    }
</style>

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

Displaying local PDF files with Vue.js - A comprehensive guide

I've been experimenting with different PDF viewers like pdf.js and vue-pdf, but I'm encountering some issues. With vue-pdf, I am able to make it work, but I can't seem to render local files. And when trying out pdf.js, I attempted to downlo ...

Experiencing challenges accessing information from the useEffect hook in React

I'm facing some issues with my useEffect hook and I can't seem to figure out why it's not working. I've been trying to make a call to an endpoint, but it seems like I'm not getting any response back. Any help would be greatly appr ...

Angular Datatables unable to locate DataTables with Webpack Encore configuration

As I've been updating a Symfony application from version 3.4 to 4.2.2, everything has been going smoothly until I encountered an issue with getting DataTables to work through yarn installation and webpack encore using angular-datatables. The Setup Pr ...

Load Angular modules dynamically

Is there a way to dynamically load a module (js, css and html) using a single directive at any point during the app's lifecycle? <my-module id="contacts"></my-module> The template for this directive looks like this <my-module id="con ...

Tips on implementing VueStripe in a vue2 Project

I recently integrated Vue Stripe into my vue2 Project and encountered 2 similar errors in my code : Property 'redirectToCheckout' does not exist on type 'Vue | Element | (Vue | Element)[]'. Property 'publishableKey' does ...

How can we efficiently load and display all images from a directory using Node.js and JavaScript?

My strategy involves reading all image file names from a specific directory and passing them as an array to the front-end JavaScript. The front-end script then iterates through the array to dynamically create image elements. Step 1: node const path = requ ...

Rendering Single File Components with Render Functions in Vue 2: A Step-by-Step Guide

I am facing an issue with rendering a Single File Vue Component using a render function. I have tried different variations like the one below: // simple.vue <template> ... </template> <script> ... </script> // parent.vue <scr ...

What causes ng-options to return an empty array in AngularJS when using $element.find('option')?

For my project in Angular 1.4, I am utilizing the ngOptions directive to dynamically fill a <select> element with <option> elements based on an object structure like this: {black: '#000000', red: '#FF0000', ...} The implem ...

When should CSS compression and combining / JS minification be performed - at runtime or during build time?

I need to find a way to compress, version (for caching reasons), and possibly combine our JS and CSS files. I've come across two main approaches so far: 1) During the build process: Using an MSBuild task or similar. 2) Dynamically at runtime: Through ...

Utilizing jQuery/Cheerio to retrieve data from a script tag variable

Currently, my web scraping process involves node.js and cheerio. Upon making a request to the website, I am presented with the following content: <html> <head> ... </head> <body> <script> ...

What is the best way to retain checkbox states after closing a modal?

I have a modal that includes multiple checkboxes, similar to a filter... When I check a checkbox, close the modal, and reopen it, the checkbox I clicked on should remain checked. (I am unsure how to achieve this :/) If I check a checkbox and then click t ...

Utilizing environment variables in a Rails-AngularJS (1.5) project

Currently working on a Rails-AngularJS (1.5) project and encountering difficulties accessing my ENV variables in the JavaScript components such as services and console. I've implemented dotenv, stored my SECRET_KEY in a .env file, and included the fo ...

Internet Explorer: JQuery deselects radio button upon its selection

One issue I have encountered is with a listener for a group of radio buttons in IE. When a radio button is selected, it triggers a database call to populate a select element. However, in IE, after the code runs successfully, the selected radio button becom ...

Transforming unknown JSON data into a fresh JSON structure

An undefined JSON object is being returned to a Node.js/Express.js application from a URL API endpoint http://someserver:someport/some_api_url?_var1=1. This particular JSON input always follows the same format and must be transformed into a new JSON object ...

What are the best practices for utilizing *ngIf?

In my Angular project, I am facing a challenge with using *ngIf. My app.component.html file handles both the login page and the dashboard. I want to hide the dashboard until the user logs in. To achieve this, I decided to use *ngIf. Here is how I implement ...

Having trouble installing express and socket.io for my nodejs application

CONFG.JSON file { "name" : "realtimechatapp", "version" : "1.0.0", "private" : "false", "dependencies" : { "socket.io" : "2.3.4", "express" : "4.17.1" }, "author" : "coder123", } ERROR DETAILS 0 info it worked if it ends ...

Step-by-step guide on starting Edge with Open package on Windows 10

I am utilizing the Open package to launch URLs across different browsers. Unfortunately, there is a lack of comprehensive documentation on how to specifically launch with different browsers on various operating systems. After some experimentation, I have ...

Is there a way to determine if a container is overflowing at the top?

Currently, I am using Puppeteer to scrape Slack. My goal is to confirm whether I have scrolled to the very top of the channel feed. The issue arises because the channel feed does not actually scroll, making it impossible for me to utilize the method outli ...

Adding event listeners for elements inside slots in Vue.js: A step-by-step guide

I need to create a unique vue component called "characters-counter" that can accurately count the characters in input elements, as shown below: <characters-counter v-slot =" {charactersCount} "> <div>{{ charactersCount }} Chars< ...

How can I delete the onmouseover function in HTML?

Is there a way to remove the (onmouseover="mouseoversound.playclip()") function from the following HTML code? <a href="roster.html" onmouseover="mouseoversound.playclip()">Roster</a> Can we instead implement this functionality in a JavaScript ...