I am trying to make cards in a FlatList the same height, although they each contain different amounts of content.
I have tried wrapping the FlatList in a container using the flex flex-row items-stretch, but I am unable to get this to work.
How can I achieve this?
// Card within FlatList
<View className="mr-4">
<View className="flex-1">
<View className="relative inline-block w-[260px]">
<View className="relative flex h-[144px] w-full items-center justify-center overflow-hidden rounded-xl">
<Image
className="object-cover h-full w-full"
alt={title}
source={{
uri: image,
}}
/>
<View className="">
<View className="pt-2 pb-2 mx-1">
<Text className="text-lg font-semibold text-gray-900">
{title}
</Text>
</View>
<View className="pb-2">
<Text className="text-gray-500">{description}</Text>
</View>
<View className="">
<Button title="Submit" />
</View>
</View>
</View>
</View>
</View>
I would try something along the lines of this:
<View className="mr-4">
<View className="flex-1">
<View className="relative inline-block w-[260px]">
<View style={{height: 144, alignItems: 'stretch', justifyContent: 'center'}} className="relative flex w-full items-center overflow-hidden rounded-xl">
<Image
className="object-cover h-full w-full"
alt={title}
source={{
uri: image,
}}
/>
<View className="">
<View className="pt-2 pb-2 mx-1">
<Text className="text-lg font-semibold text-gray-900">
{title}
</Text>
</View>
<View className="pb-2">
<Text className="text-gray-500">{description}</Text>
</View>
<View className="">
<Button title="Submit" />
</View>
</View>
</View>
</View>
</View>
Instead of using 'className' you should use style.
<View style={{height: 144, alignItems: 'stretch', justifyContent: 'center'}} className="relative flex w-full items-center overflow-hidden rounded-xl">
This should make the card 144px height, also you can stretch the items so they are the same height for all the items in the FlatList, and this should center the card in view container.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 days ago.
Improve this question
I have a container that is two sections left side and holds multiple divs that consist of a title and description and the right side of their pics but the container overflow is hidden and sticky.
I want to make it similar to the pics the right side container stays fixed or sticky with scrolling.
I know how to create the right side container and images appear at scrolling
but I cannot make it sticky and be in the center.
This is what I'm trying to achieve,
1st img
2nd img
<div className="flex flex-row w-full bg-red-900 ">
<div className="bg-red-300 w-full justify-center place-items-center">
<div className="h-screen justify-center flex flex-col space-y-3 bg-green-900 sticky top-0">
<span className="text-xl">Slemani Nwe Company</span>
<h1 className="font-semibold w-full text-5xl">
FMCG Distributer
</h1>
{/* map the tags and indexes seperate them with , */}
<div className="text-xl">Design, Development, Branding</div>
</div>
<div className="h-screen justify-center flex flex-col space-y-3 bg-green-900 sticky top-0">
<span className="text-xl">Slemani Nwe Company</span>
<h1 className="font-semibold w-full text-5xl">
FMCG Distributer
</h1>
{/* map the tags and indexes seperate them with , */}
<div className="text-xl">Design, Development, Branding</div>
</div>
<div className="h-screen justify-center flex flex-col space-y-3 bg-green-900 sticky top-0">
<span className="text-xl">Slemani Nwe Company</span>
<h1 className="font-semibold w-full text-5xl">
FMCG Distributer
</h1>
{/* map the tags and indexes seperate them with , */}
<div className="text-xl">Design, Development, Branding</div>
</div>
</div>
<div className="max-h-screen overflow-y-auto no-scrollbar bg-yellow-600 top-0 right-0 fixed" data-scroll-sticky>
<div className="imgs-container bg-pink-700 flex flex-col place-items-center ">
<Image
className="h-900 lg:w-10/12 xl:w-9/12 object-cover border-0 rounded-0"
src={"/projects/slemani-nwe.png"}
height={270}
width={350}
quality={100}
alt={"aa"}
priority
unoptimized
style={{
borderRadius: "0px !important",
overflow: "hidden",
}}
></Image>
<Image
className="h-900 lg:w-10/12 xl:w-9/12 object-cover border-0 rounded-0"
src={"/projects/slemani-nwe.png"}
height={270}
width={350}
quality={100}
alt={"aa"}
priority
unoptimized
style={{
borderRadius: "0px !important",
overflow: "hidden",
}}
></Image>
<Image
className="h-900 lg:w-10/12 xl:w-9/12 object-cover border-0 rounded-0"
src={"/projects/slemani-nwe.png"}
height={270}
width={350}
quality={100}
alt={"aa"}
priority
unoptimized
style={{
borderRadius: "0px !important",
overflow: "hidden",
}}
></Image>
</div>
</div>
</div>
I am using React-bootstrap for my app, specifically the Card& CardGroup components. I want help please with making the cards all the same height.
JSX component:
<Card style={{flex: 1}} className="card">
<Card.Img variant="top" src={Weather} />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>
This is a wider card with supporting text below as a natural lead-in
to additional content. This content is a little bit longer.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
<Card style={{flex: 1}} className="card">
<Card.Img variant="top" src={Scheduler} />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>
This card has supporting text below as a natural lead-in to
additional content.{' '}
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
<Card style={{flex: 1}} className="card">
<Card.Img variant="top" src={Quiz} />
<Card.Body>
<Card.Title>Card title</Card.Title>
<Card.Text>
This is a wider card with supporting text below as a natural lead-in
to additional content. This card has even longer content than the
first to show that equal height action.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</CardGroup>
)
Related CSS:
.card {
padding: 30px;
margin-top:59px;
}
Here's what it currently looks like
enter image description here
I have tried using max-height and min-height, but it didnt work. I learned react a month ago and thus are trying to get acclimated with it. I would truly appreciate help on how to make these cards all the same height/size, regardless of the height of the photos. Or how to make the photo sizes the same as well.
Thank you so much
Not sure if I understand the desired result, but if the goal is to:
make these cards all the same height/size, regardless of the height of the photos
It seems that Card.Body is already taking the remaining space of Card, and it could be styled to align the text and button to the bottom, making the whole CardGroup aligned vertically.
The below example adds some bootstrap classes for this as a basic solution.
Live demo of the simplified example on: stackblitz
Example:
<CardGroup>
<Card className="card d-flex flex-column">
<Card.Img src={Weather} />
<Card.Body className="d-flex flex-column justify-content-end align-items-center">
<Card.Title>Card title</Card.Title>
<Card.Text>
This is a wider card with supporting text below as a natural lead-in to
additional content. This content is a little bit longer.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
<Card className="card d-flex flex-column">
<Card.Img src={Scheduler} />
<Card.Body className="d-flex flex-column justify-content-end align-items-center">
<Card.Title>Card title</Card.Title>
<Card.Text>
This card has supporting text below as a natural lead-in to additional
content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
<Card className="card d-flex flex-column">
<Card.Img src={Quiz} />
<Card.Body className="d-flex flex-column justify-content-end align-items-center">
<Card.Title>Card title</Card.Title>
<Card.Text>
This is a wider card with supporting text below as a natural lead-in to
additional content. This card has even longer content than the first to
show that equal height action.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</CardGroup>
'Header cannot be used as a JSX component. Its return return type 'Promise' is not a valid JSX element. Type 'Promise' is missing the following properties from type 'ReactElement<any, any>': type, props, key
import Image from "next/image";
import Link from "next/link";
import LogoutButton from "./LogoutButton";
import { unstable_getServerSession } from "next-auth/next";
async function Header() {
const session = await unstable_getServerSession();
if (session)
return (
<header className="sticky top-0 z-50 bg-white flex justify-between items-center p-10 shadow-sm">
<div className="flex space-x-2">
<Image
className="rounded-full mx-2 object-contain"
height={10}
width={50}
src={session.user?.image!}
alt="Profile Picture"
/>
<div>
<p className="text-blue-400">Logged in as:</p>
<p className="font-bold text-lg">{session.user?.name}</p>
</div>
</div>
<LogoutButton />
</header>
);
return (
<header className="sticky top-0 z-50 bg-white flex justify-center items-center p-10 shadow-sm">
<div className="flex flex-col items-center space-y-5">
<div className="flex space-x-2 items-center">
<Image
src="https://links.papareact.com/jne"
height={10}
width={50}
alt="Logo"
/>
<p className="text-blue-400">Welcome to Meta Messenger</p>
</div>
<Link
href="/auth/signin"
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
Sign In
</Link>
</div>
</header>
);
}
export default Header;
I tried using an async function
I am trying to display list images that have a fixed width and use the original height of the image that is 100%.
Similar question here with no answer.
I have read these answers here and tried them out. The images are there in the dom but out of the screen and does not show up.
The size of images varies eg. 1280x15141, 1280x14333.
This is what I am trying to do from the docs example.
<main className="mx-auto max-w-7xl ">
<section className="flex flex-col">
{chapter.images.map((img) => (
<div key={img} style={{ position: 'relative', width: '768px', height: '100%' }}>
<Image alt="Mountains" src={img} layout="fill" objectFit="contain" />
</div>
))}
</section>
</main>
This ends up displaying no image but they are present in the dom.
Update:
As per suggestion I have added height.
<section className="flex flex-col">
{chapter.images.map((img) => (
<div key={img} style={{ position: 'relative', width: '768px', height: '100px' }}>
<Image alt="Mountains" src={img} layout="fill" objectFit="contain" />
</div>
))}
</section>
Plain img tag approach.
But if i use the images in <img> instead.
I am able to see the image and apply width to it with the height as the default.
<section className="flex flex-col gap-2">
{chapter.images.map((img) => (
<div key={img} className="mx-auto md:w-4/5">
<img alt="Mountains" src={img} />
</div>
))}
</section>
But I want to use NextJs Image.
How can I do that?
I believe you're import path is incorrect
import mountains from '../../public/mountains.jpg'
I have been trying to make a twitter clone, while making the sidebar I am not able to align the text of "Home" and "Explore". in img you can see "Home" is little bit towards the right. while inspecting I found that faHome icon is of 18x14 size, while faHashtag is of 14x14.
I need to know how I can I keep all button's text and Icon in same alignment.
Following the code for complete screen as shown in image. I am using Tailwind CSS
<div className="flex flex-row">
{--------- Div for Nav Bar ---------}
<div class="px-4 w-80 bg-white flex flex-col justify-start align-center top-0 bottom-0 left-0 fixed">
<div>
<button className="flex p-4 bg-red-500 mb-2 rounded-full mx-auto 2xl:mx-0 2xl:space-x-4 focus:outline-none hover:text-blue hover:bg-blue hover:bg-opacity-20">
<FontAwesomeIcon icon={faHome} className="mr-4 text-xl" />
<span>Home</span>
</button>
<button className="flex p-4 bg-red-500 rounded-full mx-auto 2xl:mx-0 2xl:space-x-4 focus:outline-none hover:text-blue hover:bg-blue hover:bg-opacity-20">
<FontAwesomeIcon icon={faHashtag} className="mr-4 text-xl" />
<span>Explore</span>
</button>
<button className="flex p-4 rounded-full mx-auto 2xl:mx-0 2xl:space-x-4 focus:outline-none hover:text-blue hover:bg-blue hover:bg-opacity-20">
<FontAwesomeIcon icon={faHashtag} size="lg" className="mr-4" />
<span>Explore</span>
</button>
</div>
</div>
{---------------------}
<div className="flex-auto h-screen bg-blue-400">Hello World</div>
<div className="w-96 h-screen bg-gray-400">Hello World</div>
</div>
I want Home and explore to look something like this.
The issue was there because of icons size coming from FontAwesome Icons. though I tried to wrap the size into a div, but that didn't work.
I found out that we can use following to get the desired size of icon.
<FontAwesomeIcon icon={faHome} style={{ width: "20px", height: "230px" }} />