To display the images in a grid layout (for example, 4x4 or 4x2), you can add additional CSS styles to define the size of each image and maintain the aspect ratio. Additionally, you can control the size of the parent container to determine how many images are displayed per row.
Below is an example of how to modify the template to achieve a grid layout:
<template>
<Dialog v-model:visible="choosing_picture" header="Information" :style="{ width: '70%' }" :modal="true">
<div style="text-align: center;">
<label for="name" style="display: inline-block; margin-bottom: 10px; font-weight: bold;">Bilder:</label>
</div>
<!-- Image grid -->
<div class="image-grid">
<div class="image-container" v-for="(picture, index) in pictures" :key="index">
<img :src="picture" class="grid-image" alt="Bild">
</div>
</div>
<div style="text-align: center;">
<Button label="Stäng" style="display: inline-block; margin-bottom: 10px; font-weight: bold;" @click="choosing_picture = false" />
</div>
</Dialog>
</template>
<script>
export default {
data() {
return {
choosing_picture: false,
pictures: [
'data:image/jpeg;base64,...', // Base64 encoded image
// ... more images
],
};
},
};
</script>
<style>
/* Grid layout styles */
.image-grid {
display: grid;
grid-template-columns: repeat(4, 1fr); /* 4 images per row */
gap: 10px; /* Spacing between images */
max-width: 100%; /* Full width of the container */
margin: 0 auto; /* Centering the grid */
}
.image-container {
overflow: hidden; /* Hide overflowing parts to maintain aspect ratio */
}
.grid-image {
width: 100%; /* Full width of the container */
height: 100%; /* Full height of the container */
object-fit: cover; /* Maintain aspect ratio of the image */
}
</style>
In the above example:
-
.image-grid class applies a CSS grid layout with 4 columns set to equal fractions of the available space (repeat(4, 1fr)).
-
.image-container class provides a container for each image, you can control the dimensions of this container to fit your needs.
-
.grid-image class ensures that all images fill their container and maintain their aspect ratio using object-fit: cover.
Using the object-fit: cover property, images will cover the available space, and portions of images may be clipped if their aspect ratio doesn't match the aspect ratio of the container. This ensures a consistent size of the images, while maintaining their aspect ratio without stretching them.
To customize the size of the images or the number of images per row, adjust the grid-template-columns value in the .image-grid class. For instance, repeat(2, 1fr) would give you a 2-column layout, suitable for a 4x2 configuration.
To further control the sizing of images, you can adjust the height of the .image-container class. If you want to give it a fixed height, you can add for example height: 150px; in .image-container. Just make sure to maintain a consistent aspect ratio if needed.