How can I align a mapped button to appear on the opposite side of the data using Tailwind CSS?

I've been struggling to find a straightforward solution to this issue for a while now.

Let me break it down - I need to display some data on the left side of a post card component with a button located right next to it. Here's an illustration:

https://i.sstatic.net/1dlRf.png

The ellipsis icon represents the button, and my goal is to position it on the right side. However, if I use 'absolute' positioning, all buttons are fixed in place even when scrolling.

Below is the code for the two components:

import * as React from 'react';

import { Button } from '@/components/ui/button';
import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from '@/components/ui/card';

import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Ellipsis } from 'lucide-react';
import { PostDialog } from './PostDialog';

// Props interface

export default async function Post({
  user_id,
  avatar_url,
  first_name,
  last_name,
  connections,
  timestamp,
  content,
}: Props) {
  // Rest of the code here...

Here is a snippet of the button component:

'use client';

import * as React from 'react';
import { Ellipsis, Moon, Sun } from 'lucide-react';
import { useTheme } from 'next-themes';

import { Button } from '@/components/ui/button';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { deletePost } from '@/lib/actions/post.actions';

// Function for handling the dialog

export function PostDialog() {
  // handleDeletePost function
  return (
    // Return statement here...
  );
}

Your attention is greatly appreciated, thank you!

Answer №1

If the parent element has a class of CardDescription with flex properties,

you can easily insert

<div className="flex-1"></div>
in between.

        <CardDescription className="flex w-full flex-row items-center">
          <Avatar className="mr-2">
            <AvatarImage src={avatar_url} />
            <AvatarFallback>NX</AvatarFallback>
          </Avatar>
          {`${first_name} ${last_name} · ${formattedConnects} · ${formattedDate}`}

          <div className="flex-1"></div> // Insert this line here

            <PostDialog /> // This represents the ellipsis button
        </CardDescription>

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

Bone structure template - optimizing list spacing with CSS

Currently, I am attempting to float my navigation bar further towards the left by at least 200px, closer to the end of the line visible below. However, every time I add a margin or padding, it causes the elements within the navigation bar to stack on top o ...

Having trouble getting the jquery tabify plugin to work properly

I've put in a lot of effort and double-checked everything, but for some reason this tabify function just won't work. I have gone through my code 100 times but still can't seem to pinpoint the error. Here is the code I am working with: <! ...

The line directly following the first line within the <li> element is located in the same position

I need help with customizing the bullet background-image for an <li> element. I want the text in the bullet to be displayed outside the marker using the CSS property list-item-position: outside. However, my current implementation doesn't seem to ...

Is there a way to automatically disable other options in a MUI Select once one option has been selected?

Can the options in the select menu be dynamically changed based on the value selected in another menu? For example: if option A is selected show options C and D in the other menu but if option B is selected show options E and F Here is a visual represent ...

The government fails to compute the updated amount

Can anyone assist me with this problem? I am facing an issue where the second state is not updating based on the changes in the first state. Specifically, in the code snippet below, the setTotalPrice function does not receive the correct value of ingredie ...

What's the best way to adjust the card column for mobile view resizing?

I am trying to modify the card view for mobile devices to display as a 2-column layout. I have adjusted the flex length and grid size in my code, but I still can't achieve the desired result. Can someone guide me on how to make this change? Current d ...

Removing nested divs using JavaScript

My website has a nested div structure which contains multiple child divs. Here is an example of the div structure: <div id="outside-one"> <div class="inside" id="1"></div> <div class="inside" id="2"></div> <div ...

Is it possible to replace text on click using CSS instead of jQuery?

My new project involves creating a flashcard system. The idea is that when a question is clicked, it reveals the answer and hides the question. To achieve this, I am currently using the following code: jsFiddle <p id="shown">What is stackoverflow?&l ...

Leveraging UseParams for Detailed Product Pages

I am in the process of developing a product detail page using the useParams Hook in my ReactJS application. Unfortunately, I have encountered some issues as I am able to retrieve the ID from the URL but unable to fetch the item's title, image, and pri ...

How to resolve hydration error in Next.js: Issue - Server-rendered HTML content does not match the text content

What should be done when the client has additional or different information than the server? For example, reading data from localStorage and displaying it. The content is different, so why does this hydration error occur? Error: Text content does not matc ...

What is the best way to choose all initial elements within a single row using CSS?

Is it possible to target all the first elements in a single row? I know this is an unusual request, but I have a row that contains block elements. I want to apply CSS styling to each of the first elements. Here is the row: <ul class="slides"> &l ...

Tips on fixing server/client content mismatch caused by browser auto translate

While constructing a website with next14, storyblok, and i18n, I encountered an issue in the browser related to the automatic Google translate feature for Georgian and English languages. Despite setting Georgian as the default language in my app, when swit ...

Expanding the last row to ensure its proper width with flex-grow

I have a flexbox with columns that each take up one third of the width. However, due to using flex-grow, the last element does not stick to its column size. I understand why this is happening, but I don't want to use max-width as it's not always ...

Develop a server-side component using Next.js that dynamically updates

I need to create a Next.js component that displays different greetings based on whether the current time's minutes are even or odd. The greeting should be "Hi" for even minutes and "Hello" for odd minutes, but this needs to be implemented in a server- ...

Having trouble getting the background image to display within the div element

I'm having trouble getting my background image to display in the specific div I want it in. It works fine when I place it in other elements, like the body element, but for some reason, it's not showing up where I need it to. Any chance you could ...

What is the best way to dynamically update the mailto: subject and body tags as they are being typed in React?

I am in the process of creating a contact page for my portfolio, and I'm looking to incorporate a simple email form using an anchor tag with a mailto attribute to send emails. The form includes fields for a title and body, with the anchor tag at the b ...

Guide on how to address the problem of the @tawk.to/tawk-messenger-react module's absence of TypeScript definitions

Is there a way to fix the issue of missing TypeScript definitions for the @tawk.to/tawk-messenger-react module? The module '@tawk.to/tawk-messenger-react' does not have a declaration file. 'c:/develop/eachblock/aquatrack/management-tool-app ...

Center and justify image inside flexbox using MUI

Can someone explain why my image appears centered but not justified? I'm feeling confused about this. <Box sx={{ display: 'flex', minHeight: '100vh', alignItems: 'center', flexGrow ...

Tips for altering the color of an activated Bootstrap dropdown toggle

When the Dropdown menu is open, I would like to customize its default colors. Specifically, I want to change the border color and background using CSS. https://i.sstatic.net/vKwLE.png Below is the HTML code snippet: <div class="row menu"> <ul ...

Steps for changing the direction of a dropdown menu to the left instead of the right

I'm having a bit of an issue with my inbox setup. I have a dropdown for it, but the dropdown is appearing off to the right and since it's at the end of my top navbar, it's not very visible. Here is the CSS code for the inbox: .notification ...