What is the best way to create separate positions for items within a single div?

https://i.sstatic.net/FQRY4.jpgAfter looping through various names and accessing them from a single div using {{item}}, I encountered an issue. All the names are within one parent element.

This is the code snippet:

<div 
  :style="image"
  class="main-browse">
  <div
    id="first"
    class="names"
    v-for="(user, index) in getUsers" 
    :key="user.id"
  >
    <div
      id='user'
      :style="{backgroundColor: getColors[index]}">
      <div>{{user.name[0]}}</div>
      
      
    </div>
  </div>

Here's the CSS snippet:

#user{
    padding: 10px; 
    margin: 50px;
    width: 200px;
    height: 200px;  
    border-radius: 50%;
  }
  .names{
    text-align: center;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    align-items: center;
    position: unset;
    font-size: 80px;
    line-height: 2.5;
  }

Currently, all the names move together as a single entity, but I aim to apply individual flex rules so that each name falls at different positions.

Answer №1

It seems like there are a few issues here, and I'm not entirely clear on your intended outcome. Providing an image of the desired design would be beneficial in clarifying the goal. That being said, I'll do my best to address what I can.

One problem is that your v-for directive is placed on the #first div, resulting in multiple instances of #first for each user in the getUsers array. As IDs should be unique, having multiple #first IDs will cause the names to stack instead of flexing properly within each .names element. The generated markup looks like this:

<div class="main-browser">
  <div id="first" class="names">
    <div id="user">H</div>
  </div>
  <div id="first" class="names">
    <div id="user">L</div>
  </div>
  <div id="first" class="names">
    <div id="user">N</div>
  </div>
  <div id="first" class="names">
    <div id="user">K</div>
  </div>
</div>

To resolve this issue, it's recommended to move the v-for onto the #user element rather than #first, and use classes instead of IDs. A revised markup example could look like this:

<div :style="image" class="main-browser">
  <div class="names">
    <div
      v-for="(user, index) in getUsers" 
      :key="user.id"
      class="user"
      :style="{backgroundColor: getColors[index]}">
      <div>{{user.name[0]}}</div>
    </div>
  </div>
</div>

This updated structure would render as follows:

<div class="main-browser">
  <div class="names">
    <div class="user">H</div>
    <div class="user">L</div>
    <div class="user">N</div>
    <div class="user">K</div>
  </div>
</div>

By making these adjustments, you can iterate through the users correctly and have more flexibility in rendering their information.

Additionally, don't forget to apply display: flex; to the .names class to ensure the initials display next to each other in a row.


In regards to your question about having items in one div display separately in different positions, the approach will vary based on your specific requirements. If you simply want them to display side by side, the changes suggested above should suffice, though you may need to adjust margins for proper alignment.

If you need more precise positioning, consider using position: relative; on .names and then applying position: absolute; to its child elements. You can then customize the positioning using properties like top, right, bottom, and left.

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

Accessing an external webpage within a React.js application while maintaining visibility of the navbar

Seeking assistance on how to open an external link within a React app below the navbar when navigating to "/WebSite". Currently using react router for page navigation. Here's the code snippet: const App = () => { return( <BrowserRouter&g ...

Is it possible for the content of the HTML Select to vary from the options displayed in the 'drop-down' menu that the user chooses from?

When a user clicks on a dropdown menu, I want them to see the full state names in the options like this. <select name="address_region" id="address_region"> <option value=1>Alabama</option> <option value=2&g ...

Node.js Express API returns an error status with an empty array response in a RestFul manner

Currently, I am in the process of developing a RestFull API using Node.JS to validate if a specific license plate is registered in my database (which happens to be MySQL). The endpoint that I have set up for this task is as follows: http://localhost:3009/_ ...

What methods can I utilize to increase the speed of my JavaScript animation?

Recently, I implemented a Vue component designed to loop a div over the X axis. While it functions correctly, I can't help but notice that it is consuming an excessive amount of CPU. I am interested in optimizing this animation for better performance. ...

Monitoring the progress of file uploads within itemView

In the process of developing an application using Marionette/Backbone, I have successfully implemented file uploads over an AJAX call. Now, I am looking for a way to provide users with feedback on when they can select the uploaded file and proceed with mod ...

What is the best method to invoke a function recursively with a delay of 1 second following the completion of an ajax

I am facing a situation where I need to implement a delay of 1 second after an ajax request is completed, regardless of the outcome. Instead of calling a method every second, I specifically want to call a function 1 second after the ajax request finishes. ...

Retrieve the parent object within a constructor function

Is there a way to access the parent object when calling a function contained inside that object as a constructor without explicitly referring to it? Take a look at this scenario: var customers = { // Number of customers count: 0, // Naturally ...

Navigating the translation URL in a Node.js Express app

Can someone please guide me on how to handle routing for translated URLs in a Node.js Express application? In my app.js file, I currently have the following routes. How can I improve this setup for handling URLs that change based on language, while still ...

What is the proper way to detach an event listener from a class?

I am facing a challenge when trying to remove an event listener. After running the script, I receive an error message stating that changeGirl.off("click") is not a function. Despite this issue, everything else within the code is working perfectly fine. Any ...

Flexbox's bootstrap columns display issue on smaller screens with no wrapping

I am currently working on a section of my website where I have applied some CSS to 2 divs and an anchor tag in order to center the content vertically. However, I am facing an issue with the flex style properties when the window size is less than 768px. In ...

Tips for extracting the chosen text during a 'change' event

I need to access the text of the currently selected object. When I use $(this).text(), I end up getting all available selections I have attempted the following methods without achieving the desired result: $(this).text() $(this).selected() $(this).se ...

Is Amazon altering the names of their CSS selectors and HTML elements on the fly?

For my Amazon.es web scraper built with Selenium, I am using a CSS selector to determine the total number of pages it will iterate through. However, the selector name seems to change dynamically and I must update it daily. As someone not well-versed in H ...

Utilize various CSS styles for text wrapping

I'm struggling to figure out where to begin with this problem. My goal is to create a three-column row that can wrap below each other if they cannot fit horizontally. The height of the row should adjust to accommodate the items while maintaining a fix ...

Can the Twitter Bootstrap typeahead feature be used with an external data source?

Can the typeahead feature in version 2.0.3 of twitter-bootstrap work with a remote data source? You can check out the link for more information on the typeahead functionality: ...

What is the best way to replicate a VueJS project?

While attempting to replicate an entire VueJS project using cp -r project clone_project, I encountered this error upon executing npm run serve from the clone_project directory: > [email protected] serve /Users/path_to_clone_project/clone_project > v ...

Utilize Puppeteer for Web Scraping to Extract Products with an Array of Images

I am currently developing my portfolio by working on a variety of small projects, with my current focus on web scraping. Using Puppeteer, I have successfully scraped basic test websites. However, my goal now is to tackle more advanced challenges, such as ...

The issue of the useRef object returning undefined in React/React-Native is causing confusion during

Every time I attempt to access the ref object, I keep receiving an error stating that it is 'undefined'. My goal is to map through an array of images and use useRef to access each individual one. To start off, I initialized the useRef like this: ...

Interactive Django table using AJAX

This marks the second question in a series regarding my Django project. The code utilized here contains sections borrowed from this post: Stack Overflow The goal is to implement a dynamic table that cycles through objects in a list (currently set at an in ...

Certain public files in Express cannot be accessed locally

While running my Node.js application on localhost, I am able to access http://localhost:3000/css/dashboard.css without any issues. However, when attempting to access http://localhost:3000/css/logo.png for a logo image in the same directory, all I receive i ...

text box with an immobile header

As the browser window size decreases, the layout changes. However, when scrolling down, the search text box moves up and is no longer visible due to its lack of fixation. How can I make the search text box stay fixed as I scroll down? I tried implementing ...