I need to use some Vimeo iFrames as a video background with a widescreen aspect ratio. I'm having trouble getting the iframe to stretch to the full width of the parent, ignoring it's own dimensions to stretch out. I'm working with Tailwind in a Next.js app.
This is the parent, stretching the full width of the page...
And this is how the embed is looking right now:
How can I stretch the video to the full-width of it's parent? Here's the JSX of my components:
ProjectCard.tsx
<div className="group relative flex aspect-widescreen items-center justify-center overflow-hidden">
<h3 className="absolute z-10 mx-auto text-center font-serif text-sm uppercase tracking-[15px] text-white opacity-0 transition-opacity duration-700 group-hover:opacity-100 md:text-3xl md:tracking-[1.5625rem]">
{asText(project.title)}
</h3>
<Image
src={getImageUrl(project.cover.widescreen.url)}
width={project.cover.widescreen.dimensions?.width}
height={project.cover.widescreen.dimensions?.height}
alt={asText(project.title)}
className="w-full"
placeholder="blur"
blurDataURL={getBlurUrl(project.cover.widescreen.url)}
quality={100}
/>
{preview ? <ProjectPreview preview={project.preview} /> : null}
</div>
ProjectPreview.tsx
<iframe
src="iframeSrc"
className="absolute inset-0 aspect-widescreen h-full w-full overflow-hidden object-fill"
allowFullScreen
/>
I was able to get this done by just adding a fixed height to the iframe; in my case it was h-[169%], so the iframe looks like this now:
<iframe
src="iframeSrc"
className="absolute h-[169%] min-h-full w-auto min-w-full max-w-none opacity-100 transition-opacity duration-700 group-hover:opacity-100"
allowFullScreen
/>
Here is another approach (see demo here). This causes the videos to fill their container regardless of the width. The videos retain their aspect ratio, but will overflow the container depending on the height.
Make sure that your container has an explicit height. Height auto won't cut it. Here I used grid on the list container and set each row to have a height of 25vh:
function ProjectsList({ projects }) {
return (
<div className="list | grid auto-rows-[25vh]">
{projects.map((project) => (
<ProjectCard project={project} key={project.preview} />
))}
</div>
);
}
I didn't change your project card except to add select-none to the title so the cursor doesn't change when you hover over the title.
function ProjectCard({ project }) {
return (
<div className="card | group relative flex items-center justify-center overflow-hidden">
<h3 className="title | select-none absolute z-10 mx-auto text-center font-serif text-sm uppercase tracking-[15px] text-white opacity-0 transition-opacity duration-700 group-hover:opacity-100 md:text-3xl md:tracking-[1.5625rem]">
{asText(project.title)}
</h3>
<Image
src={getImageUrl(project.cover.widescreen.url)}
width={project.cover.widescreen.dimensions?.width}
height={project.cover.widescreen.dimensions?.height}
alt={asText(project.title)}
className="w-full"
placeholder="blur"
blurDataURL={getBlurUrl(project.cover.widescreen.url)}
quality={100}
/>
{project.preview ? (
<ProjectPreview preview={project.preview} title={project.title} />
) : null}
</div>
);
}
To force the video iframe to expand outside of its containing element and fill it in both directions, you can set min-w-full min-h-full. This is the equivalent of setting min-width: 100% and min-height: 100%. Using aspect-[16/9] will make the iframe contents retain its aspect ratio of 16:9. The top-1/2 left-1/2 translate-y-[-50%] translate-x-[-50%] classes, make the video center itself within its container.
function ProjectPreview({ preview, title }) {
return (
<iframe
src={`https://player.vimeo.com/video/${preview}?background=1&autoplay=1&loop=1&byline=0&title=0`}
frameBorder="0"
title={title}
className="video | aspect-[16/9] min-w-full min-h-full absolute top-1/2 left-1/2 translate-y-[-50%] translate-x-[-50%]"
/>
);
}
Related
Here is an example
here is my code
<div className="lg:flex gap-10 mx-auto">
<div className="text-center shadow-lg p-10 rounded-xl my-10 border-4 border-teal-600 flex-auto items-center">
<Image src={figma1} width={"100%"} height={"100%"} layout="responsive" className="rounded-xl border-teal-600" />
<div className="text-center shadow-lg p-10 rounded-xl my-1 flex-auto items-center">
<h3 className="text-lg font-medium pt-8 pb-2">Less text smaller picture</h3>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Vis</button>
</div>
</div>
Im using tailwind.
I dont know how to fix this. Ive tried overflow-hidden- h-full etc, but cant seem to find a solution
You have width={"100%"} height={"100%"}, and the text is making the container bigger, so it's logical that the 100% will be bigger when the container is bigger because the text is bigger. If you don't want that, you can just change the width and height using a fixed width, and not a width with %, that follows the change in the container.
You can set margin:0 for the h3 tags. Since <h> tags have a default margin to them. You have to set them to 0 manually
.text-center h3{
margin :0
}
I was trying to style a banner for the picture and wrote the code for the same which I have attached below. I have set the width of the image to around 70% of the parent container and margin-left to auto, but on inspecting the rendered image it is showing width of 100% of the parent container. Am not able to figure out where the width is getting inherited and how can I rectify it.
Below is the CSS tailwind code for the same
import React from "react";
import Image from "next/image.js";
const EachBannerSlide = ({ bannerUrl, title }) => {
return (
<div className=" carousel-container relative w-full h-full py-5 px-0 overflow-x-hidden mt-5">
<div className="carousel flex h-full w-11/12 relative m-auto bg-black">
<div className="slider bg-black rounded flex-none h-full mr-8 overflow-hidden relative left-0 w-full">
<img src={bannerUrl} alt={title} className="block ml-auto min-h-full object-cover w-3/5" loading="lazy" />
</div>
</div>
</div>
);
};
Despite giving w-3/5, the image is appearing on the whole of the banner!!
Any help is highly appreciated!!
EDIT :
This is what I am getting
and on inspecting element it is showing
Width 100% is getting rendered.
and below is the property of the parent container
Currently, my image is getting shown like this, but I want them to be circular and each of equal width and height.
They aren't square now and I have added the below CSS tailwind code but still am not getting them all in the circular.
<div className="cursor-pointer">
<div className="w-full overflow-hidden rounded-full mb-2 border border-black">
<img src={banner} alt={title} loading="lazy" className="w-full" />
</div>
<h3 className="text-sm md:text-lg font-medium ">{title}</h3>
</div>
I have tried different iterations with hard-coded height and width, but still am not able to figure out how to get ahead of it.
Any help is highly appreciated!!
EDIT:
On w-20 and h-20, the images get circular and on doing w-25 and h-25 they become disproportionate again. Any lead on why so?
And on adding w-20 and-20 the text isn't in the middle anymore. How can I fix that?
For tailwind, you can use the CSS for rounded-full on the image tag. You need to provide a fixed height and width to the image as well like
<img class="w-50 h-50 rounded-full" src="pic.jpg" alt="Rounded avatar">
Here is Leonardo DiCaprio below for you :)
<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet" />
<img class="p-1 w-50 h-50 rounded-full ring-2 ring-gray-300 dark:ring-gray-500" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTtyS3wBrnrkDXvhSo8SEXpvUXqXajtTSi9zD33IpKxAJHlijHnaY1pFT30NpaxxnP58PM&usqp=CAU" alt="Bordered avatar">
Give your image's parent div element the same width and length and make it rounded full,
use this tailwind classes
w-10 h-10 rounded-full
I'm using tailwind. I want to create a layout with a fixed-size image. My container is 1200px and centering viewport.
The image will be overflow when the viewport is smaller, but I want to hide the overflow part of the image. That means the user can't scroll to view it. Thanks
Here is my code: https://play.tailwindcss.com/YVOEhykx1E
Here, Is the little update! I think you can do more better by exploring documentation
<div class="h-screen mx-auto bg-red-500 grid grid-cols-12 gap-8 overflow-hidden">
<div class="col-span-12 col-start-8">
<img class="object-cover h-full w-full" src="https://images.unsplash.com/photo-1640686708297-e6dc01470fb8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80" alt="" />
</div>
</div>
So I'm trying to create a card with information on the right and a purple hue on the left if it's selected. I'm using Next and TailwindCSS.
Here is my parent component which renders the cards:
{files ? (
<div className="ml-6 mt-8 absolute">
{files.map((value) => {
return <FileCard key={value.id} file={value} />
})}
</div>
) : (
<div>graphql not working :(</div>
)}
and here is the FileCard component:
return (
<div className="flex justify-content items-center mb-4 w-72 bg-white rounded-lg shadow-lg relative">
<div className="h-full w-2 bg-indigo-600 relative rounded-l-lg"></div> <- // here the h-full not working
<div className="py-4 px-4 flex justify-between items-center">
<div className="flex flex-col justify-center align-start text-sm font-bold">
<p>{file.title}</p>
<p className="text-xs">Last updated: {date.toString()}</p>
</div>
</div>
</div>
)
Expected results:
Actual results:
If I hard code the height with h-20:
As described in the official tailwindcss docs
Use h-full to set an element's height to 100% of its parent, as long as the parent has a defined height.
100% height only works if the parent element has a fixed height defined.
So one of the possible solution is to set the height of parent element like:
<div className="h-20 flex justify-content items-center mb-4 w-72 bg-white rounded-lg shadow-lg relative">
Here i've set the height to 20 which is equivalent to 5rem. You can set it according to your need and the inner div will adapt to that height if h-full is applied to it.
You can use vh. Example: h-[40vh].
Kind of late in the party but have you tried passing a percentage like this:
<div className="w-full h-[40%]">
You would notice in the web inspector, it creates a class like this on the fly:
.h-\[40\%\] {
height: 40%;
}
There is no need to extend in the tailwind config.
This works for everything in the framework, including things like background colors, font sizes, pseudo-element content, and more.
Reference: https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values