I have an image gallery in Tailwind CSS with React. It looks like this:
Each image component is defined life this:
<div className="flex items-center">
<div className="rounded overflow-hidden shadow-lg">
<img
className="w-full h-48 object-cover"
src={img}
loading="lazy"
alt={""}
/>
<div className="p-6">
<div className="flex items-baseline">
<div className="">{name}</div>
</div>
</div>
</div>
</div>
and it's loaded on this container:
<div className="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
{images.map(img => <ImageComponent ..../>)}
</div>
As you can see the descriptions make the cards longer than others. How can I have the cards in the same row the same size without cutting the word?
I'm using "tailwindcss": "1.4.6"
I've solved this before by putting the card contents in a flex-column and stretching the content area to fill the flex by giving it flex-1. For example:
<div className="rounded overflow-hidden shadow-lg">
<div className="flex flex-column">
<img
className="w-full h-48 object-cover"
src={img}
loading="lazy"
alt={""}
/>
<div className="flex-1 p-6">
<div className="">{name}</div>
</div>
</div>
</div>
Related
The problem is that I have a title a subtitle and a footer. I would like the subtitle's div to span across the empty space between the title and the footer.
I tried setting h-full to the div but it does not seem to do anything.
export default function ASD() {
return (
<div className="flex flex-col justify-start mx-auto w-9/12 min-h-screen bg-red-300 text-center">
<div className="bg-yellow-200">
<div className="bg-blue-200 text-6xl">
<h1>Title</h1>
</div>
<div className="bg-indigo-500 text-3xl">
<h1>Subtitle</h1>
</div>
</div>
<footer className="mt-auto bg-green-200">Footer</footer>
</div>
);
}
Also, I would like the whole page to cover the entire screen so that the footer is at the bottom of the page.
Your header and subtitle is wrapped inside a div, you need to remove that wrapper div ,and then use flex-1 so that the subtitle div spans accross the entire height of the screen.
<div class="mx-auto flex min-h-screen w-9/12 flex-col justify-start bg-red-300 text-center">
<div class="bg-blue-200 text-6xl">
<h1>Title</h1>
</div>
<div class="flex-1 bg-indigo-500 text-3xl"> //👈 use flex-1 here
<h1>Subtitle</h1>
</div>
<footer class="mt-auto bg-green-200">Footer</footer>
</div>
Output:
Tailwind play link
Try this.
<div className="flex flex-col justify-start mx-auto w-9/12 h-full bg-red-300 text-center">
<div className="bg-yellow-200 flex-1 flex flex-col">
<div className="bg-blue-200 text-6xl">
<h1>Title</h1>
</div>
<div className="bg-indigo-500 text-3xl flex-1">
<h1>Subtitle</h1>
</div>
</div>
<footer className="bg-green-200">Footer</footer>
</div>
I am struggling to make my images fill or fit their containers. I want them to take all available of the view and not beyond the view.
<div class="flex h-screen max-h-screen border-2">
<div class="w-1/2">
<div class="h-12 bg-red-100">here</div>
<div class="flex-1 border-2 border-red-200">
<img class="h-full object-contain" src="https://picsum.photos/2000/4000" alt="first image" />
</div>
</div>
<div class="w-1/2">
<div class="h-12 bg-red-100">here</div>
<div class="flex-1 h-full border-2 border-red-200">
<img class="object-fill" src="https://picsum.photos/200/300" alt="test" />
</div>
</div>
</div>
https://play.tailwindcss.com/6GMm4k4vxU
THX
You can remove the images' containers and apply w-full and h-full to both. Then, apply overflow-hidden to hide the images' overflowing parts.
Of course, the second image (https://picsum.photos/200/300) will always be blurry because we're scaling it up quite a bit.
You can also remove the max-h-screen because we already set a constant height to the screen, which is 100vh (h-screen).
<div class="flex h-screen border-2 border-red-600">
<div class="flex w-1/2 flex-col">
<div class="min-h-12 bg-red-100">here</div>
<img class="h-full w-full overflow-hidden" src="https://picsum.photos/2000/4000" alt="test" />
</div>
<div class="flex w-1/2 flex-col">
<div class="min-h-12 bg-red-100">here</div>
<img class="h-full w-full overflow-hidden" src="https://picsum.photos/200/300" alt="test" />
</div>
</div>
Tailwind-play
The above method might stretch your images. Here's another solution that should take care of it:
Remove the images thoroughly. We're going to show them as a background image.
We'll start by adding the background's images to tailwind.config:
module.exports = {
theme: {
extend: {
backgroundImage: {
'1000': "url('https://picsum.photos/1000/1000')",
'1200': "url('https://picsum.photos/1200/1200')",
}
}
}
}
You can read about this method here. You can also read about configuring the default theme here.
We'll then change the structure of the images by splitting our view into two containers (w-1/2 each). Each container will have an image as a background applied using the custom background we created (bg-1000 and bg-1200).
Each container also has a div inside it. This div is entirely optional.
<div class="flex h-screen border-2 border-red-600">
<div class="w-1/2 bg-1200">
<div class="h-12 bg-red-100">here</div>
</div>
<div class="w-1/2 bg-1000">
<div class="h-12 bg-red-100">here</div>
</div>
</div>
Tailwind-play
So I have an Image card that I want to place an element to the bottom right of the image.
I'm using the TailwindCss aspect ratio plugin.
That Close X needs to be moved to the bottom-right side of the Image.
<div class="mt-5 grid grid-cols-2 gap-3 px-3">
<div class="aspect-w-3 aspect-h-4">
<image
src="http://unsplash.it/500/500?random&gravity=center"
alt="Profile Image"
class="rounded-md"
layout="fill"
/>
<div class="absolute bottom-2 right-0 text-white">X</div>
</div>
</div>
This is a Codesandbox demonstrating the problem
Since the TailwindCss aspect ratio plugin uses padding and 0 height, position absolute will not work as intended. So we can use flex to our advantage.
<div class="mt-5 grid grid-cols-2 gap-3 px-3">
<div class="flex aspect-w-3 aspect-h-4">
<image
src="http://unsplash.it/500/500?random&gravity=center"
alt="Profile Image"
class="rounded-md"
layout="fill"
/>
<div class="flex items-end justify-end p-5 text-white">X</div>
</div>
</div>
Working Solution:
https://codesandbox.io/s/morning-morning-ggr841?file=/public/index.html
you just need add relative class in div parent of image and add w-full class in image element, because width of image will be fit with width of div parent.
<div class="mt-5 grid grid-cols-2 gap-3 px-3">
<div class="aspect-w-3 aspect-h-4 relative">
<image
src="http://unsplash.it/500/500?random&gravity=center"
alt="Profile Image"
class="rounded-md w-full"
layout="fill"
/>
<div class="absolute bottom-2 right-0 text-white">X</div>
</div>
</div>
You just need to add a relative class to the div with the aspect ration styles.
<div class="mt-5 grid grid-cols-2 gap-3 px-3">
<div class="aspect-w-3 aspect-h-4 relative">
<image src="https://unsplash.it/500/500?random&gravity=center" alt="Profile Image" class="rounded-md" layout="fill" />
<div class="absolute bottom-2 right-0 text-white">X</div>
</div>
</div>
https://play.tailwindcss.com/7yFSnphcyq
EDIT
Updated answer that does not require the aspect ratio plugin and uses arbitrary values instead aspect-[3/4]. It also replaces the deprecated <image> tag which I would not recommend you use.
<div class="mt-5 grid grid-cols-2 gap-3 px-3">
<div class="relative aspect-[3/4]">
<img src="https://unsplash.it/500/500?random&gravity=center" alt="Profile Image" class="rounded-md w-full h-full" />
<div class="absolute bottom-2 right-0 text-white">X</div>
</div>
</div>
https://play.tailwindcss.com/TLAPFmRbZr
I am using Flex in tailwind in one my main div container to align the items inside of it horizontal
but somehow the items are of different sizes in the row the image link is below any fixes for it?
i have tried using height utility class but it just messes up the entire page.
[1]: https://i.stack.imgur.com/Yjs51.jpg
HOME
ORDERS
MENU CARD
FAQs
ABOUT
Butter Chicken
If you haven't tried butter chicken, you don't know what you'r missing.
</div>
</div>
</a>
<a href="butterchicken.html">
<div class="max-w-sm rounded overflow-hidden shadow-lg justify-center">
<img class= "w-full" src="images/download (1).jpg" alt="malai-chicken">
<div class="px-6 py-8">
<div class="font-extrabold text-xl pb-3">Malai Chicken</div>
<div>
<p class=" font-serif text-base mb-3">Clean as white and appetizing.</p>
</div>
</div>
</div>
</a>
</a>
<a href="butterchicken.html">
<div class="max-w-sm rounded overflow-hidden shadow-lg justify-center">
<img class= "w-full" src="images/download (2).jpg" alt="Shahi Paneer">
<div class="px-6 py-8">
<div class="font-extrabold text-xl pb-3">Shahi Paneer</div>
<div>
<p class=" font-serif text-base mb-3">First Pick of Royales.</p>
</div>
</div>
</div>
</a>
</div>
I'm trying to learn tailwind-css or I would say learning css where I'm struggling with the position of elements. Working on Vue js components.
I achieved the designing few of the elements so far:
I want to add some shapes or designs inside the banner/dark-blue area and then would add some widget-box inside of it. My code look something like this:
<div class="bg-white block">
<nav-bar></nav-bar>
<div class="hidden md:block w-2/5 top-0 left-0">
<img src="/nits-assets/images/body_shape.png" alt="shape" align="left">
</div>
<div class="hidden md:block">
<img src="/nits-assets/images/body_shape_2.png" alt="shape" align="right">
</div>
<div class="block">
<div class="absolute w-full top-0 pl-12 pr-12 pt-40">
<slider></slider>
<div class="flex justify-around">
<card></card>
<card></card>
<card></card>
<card></card>
</div>
</div>
</div>
<div class="block">
<div class="bg-white overlflow-hidden">
<div class="relative">
<img src="/nits-assets/images/screenshot_banner.png" alt="screenshot_banner" align="center">
<img class="absolute top-0 left-0" src="/nits-assets/images/pattern_1.png" alt="banner" align="left">
</div>
</div>
</div>
<feature></feature>
</div>
For reference to the components code: https://github.com/nitish1986/sample_website
My approach was to fix position of the parent element or make it relative then position the shapes with absolute positioning respective to its parent element but whenever I try to put absolute position the shapes reaches to the top of the website. It is not taking respective positioning
<div class="bg-white overlflow-hidden">
<div class="relative">
<img src="/nits-assets/images/screenshot_banner.png" alt="screenshot_banner" align="center">
<img class="absolute top-0 left-0" src="/nits-assets/images/pattern_1.png" alt="banner" align="left">
</div>
</div>
How can achieve this positioning? I want to achieve something like this:
Any better approach into it are most welcome. Thanks.
I did a lot of research work on my issue, I found out that my absolute element is taking all the space needed for me. I came to know that I have to define an empty space or define the height of the div which is exactly consumed by my absolute elements. Since in my code I have a very long absolute element, so the only thing can cover is empty div space.
<div class="bg-white block">
<nav-bar></nav-bar>
<div class="hidden md:block w-2/5 top-0 h-auto">
<img src="/nits-assets/images/body_shape.png" alt="shape" align="left">
</div>
<div class="hidden md:block h-auto">
<img src="/nits-assets/images/body_shape_2.png" alt="shape" align="right">
</div>
<div class="block bg-white h-screen">
<div class="relative">
<div class="absolute w-full top-0 pl-12 pr-12 pt-40">
<slider></slider>
<div class="flex justify-around">
<card></card>
<card></card>
<card></card>
<card></card>
</div>
</div>
</div>
</div>
<div class="block mt-48 p-56">
<div class="p-2 w-full h-100"></div>
</div>
<feature></feature>
<preview></preview>
<about-us></about-us>
</div>
So in my code I kept empty space with:
<div class="block mt-48 p-56">
<div class="p-2 w-full h-100"></div>
</div>
Hope this helps someone. Thanks