Positioning an absolute div inside a relative div within a v-for loop while using VueJS and Element UI

I am experimenting with rendering a list of <el-card> items using a transition-group. Each card has a front and back side that flip when a button is clicked.

To achieve a smooth flip transition, I need to apply the style:

position: absolute; top: 0; left: 0
to the cards. However, this causes all the cards to be stacked on top of each other.

If I remove this style, the cards are displayed side by side but the flip effect shows the back item under the front item.

You can view an example here.

I would greatly appreciate any assistance in resolving this issue.

Thank you.

Michael

Answer №1

The reason for this issue is that the front and back elements of the card are positioned absolutely in relation to the <body> element. Since none of the parent elements have position: relative, they will naturally all display at coordinates (0,0) on the page.

To fix this, you should actually use position: relative on the parent element, preferably on each individual card. Give the parent element a class for easier selection:

<div class="card" v-for="card in cards" v-bind:key="card">

Then, set a fixed width and height (which is 180px, as it's defined for the front/back faces of the card already), and add position: relative to it:

.card {
  position: relative;
  width: 180px;
  height: 180px;
}

Check out a proof-of-concept fix here: https://codesandbox.io/s/xjq08j5n1p


Additionally, you may notice that your example still has a flaw where flipping one card flips all the cards. This is because the flip state is stored on the parent app instance, rather than on each individual card. It's recommended to create a child component for each card to store their flipped states individually.

Answer №2

Terry's response was right on target. I thought I would provide a comprehensive example to ensure functionality.

Given that you had defined a .flipper class, I utilized it to incorporate the position:relative block:

.flipper {
  position: relative;
  width: 180px;
  height: 180px;
}

Remember that in order for position:relative to be effective, a size must be specified. Otherwise, if there is no element within the .flipper in flow, its size will be zero.

It is also important to establish the appropriate width of the child elements

.front,
.back {
  width: 100%;
  height: 100%;
  position: absolute;
}

We utilize 100% as we have already set the width on the parent.

I transformed flipCards into an array of Boolean values and made adjustments to the flipCardSwitch function.

flipCardSwitch: function(index) {
  // necessary because of https://vuejs.org/v2/guide/list.html#Caveats
  this.$set(this.flipCard, index, !this.flipCard[index]);
}

Don't forget to update the on-click.

@click="flipCardSwitch(i)"

Operational code: https://codesandbox.io/s/71512v9qvx

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

Guide to positioning text so the baseline aligns with a specific point in HTML/CSS

After developing an algorithm that converts text from SVG to HTML, I encountered a discrepancy in the positioning of the text. The issue stems from the difference in coordinate systems between SVG and HTML; SVG's origin is at the bottom left while HTM ...

Improved efficiency in CSS for left transition animations

Here is the current code I am using: .s2 { top: 150px; left: 20px; position: absolute; transition: left 300ms linear; } I am currently updating the left position dynamically using JavaScript based on scroll events. However, I have noticed that th ...

Styling triangles within a CSS triangle

I'm attempting to design a webpage with a fixed triangle navigation element. The issue I am encountering is that I am unable to position smaller triangles inside the larger one, as shown in the image below. https://i.stack.imgur.com/1bTj8.png As th ...

What is causing the align content property to not function as expected in this situation?

Is it expected for the flex items of the .center-section-container to be closer together when using this property? They do not seem to be affected at all. I have applied flex wrap so the items are now in different lines but too far away from each other, I ...

What is the process for implementing custom CSS styles in Cognos?

I've been working on creating Cognos reports and I'm looking to change the overall design of some specific ones (prompt pages and report pages). Is there a way for me to use different custom CSS files for each individual report? If that's ...

Choose the label by utilizing the CSS selector

I am currently using CSS to create tabs with radio buttons. However, I am struggling to figure out how to select the corresponding <label> for the radio button. To keep the labels separate from the content and display them as tabs, I have structured ...

Error in Laraval Breeze: Vue component is missing page or props definition

Hey there, I'm currently working on developing a web application using the Laravel Breeze framework which incorporates Laravel, Vue.js, and Inertia.js. I've made significant progress so far, but I'm encountering an issue with utilizing the ...

Applying the active state to the link will cause it to inherit the default styles of the

I am facing a challenge with overriding the active style of links in a universal manner, to restore them to their original state when they cannot be interacted with (such as during scrolling). In my current code, I implemented this: .scrolling a:active { ...

Dynamic JavaScript tool

Does anyone know which library is being used on the website linked here? I am working on a project similar to this and would appreciate if anyone can identify this library for me. Thank you in advance. ...

Trouble displaying data table using Vue JS (v-for)

My goal is to display a table of data fetched from Firebase Firestore. I have successfully stored all the data in an array, but when I try to display it, the entire array appears instead of individual items. You can see the issue in the image below: Here& ...

Tips for creating a textfield with height that responds dynamically in Material UI

I am trying to create a textfield with a responsive height that adjusts itself based on the size of its parent grid when the browser window is resized. I have been searching for a solution and came across this helpful post on Stack Overflow about creating ...

how to change the class of a div when clicking on a <p> element

Check out this code snippet: <p class="faq" onclick="toggleVisibility('answer1');"> How can I restrict email sending on my website? </p> <div id="answer1" class="hide"><sometext></div> these are my CSS styles! ...

Ways to implement CSS with javascript

I'm using PHP to retrieve data from my database and then leveraging Javascript to enable users to add it. However, I am facing challenges in making a div change its background color and apply borders. <div class="displayedItems"></div> &l ...

Tips for monitoring multiple values in a Vue 3 script setup

Within my code, I currently have a watcher set up like this (inside <script setup>): const form = reactive({ body: '', image: '' }) watch(() => form.image, () => { console.log(form.image) }) I am looking to enh ...

Encountering a 404 Error on Vite-Vue Page Hosted on Azure Blob Storage

My website was successfully deployed on Azure blob storage using the Azure Storage Extension for VS code. I navigated to my project folder, clicked on the Azure Section, Storage Accounts, Blob Containers, and then Deployed to Static Website via Azure Stora ...

When an error message is displayed in the textField, the Material UI button shifts downward

I have incorporated the material UI error message display into my text field. When I click on the button without entering any text in the field, an error message pops up which pushes down the button. I am looking for a way to prevent this behavior but have ...

Tips for implementing a fallback image in v-img using Vuetify

Within my Vuetify application, I am utilizing a v-img component and I am looking to implement a fallback image in case the primary one fails to load. <v-img :src="cPicture" contain v-on:error="onImgError"></v-img> cPicture ...

Can theme changes be carried over between different pages using Material UI?

I've encountered an issue with MUI 5.14.1 where I'm getting an error every time I attempt to save themes across pages using localStorage. Any suggestions on how to resolve this problem or recommendations on a different approach would be greatly a ...

Issue with TypeScript in Vue3: Unable to access computed property from another computed property

In my Vue3 project with TypeScript, I am encountering an issue where I am unable to access the properties of the returned JavaScript object from one computed property in another computed property using dot notation or named indexing. For instance, when tr ...

Incorrect font display occurring exclusively on Windows Chrome

There seems to be an issue with the Google served Open Sans font on my website, specifically with Win Chrome where the font appears thick and ugly. However, when I tested it on Win FF and Safari, everything seemed fine. The font also displays correctly on ...