React JS responsive height and width - reactjs

How can I make the height and width of a div grow and shrink with their aspect ratio while resizing the window?. I'm using tailwind css with next JS and I want to make my div responsive while resizing the window, and in doing that I need the height to shrink as well. how is that possible?
<div className="relative flex -z-10 h-auto w-72 object-scale-down">
<Image src={"/profile.webp"} fill objectFit="cover" />
</div>
I'm using an image as a content for the div.

Related

How can I open the tooltip in an overflow hidden sidebar?

my sidebar
<div className="w-96 h-screen overflow-x-visible overflow-y-auto" >
<SidebarContent/>
</div>
in sidebar content
menuname1 ...
menuname2 ...
these "..." my tooltip.tooltip content will open when hovering. but my sidebar overflow y hidden. if sidebar content relative , tooltip does not export content. if sidebar content not relative , tooltip export content but It does not open at the menu level, as if there is no scroll, it opens in hard places.
<div className="relative z-auto">
<div className="absolute bg-pink-200 left-72 z-50">tooltipcontent</div>
</div>
Both my sidebar will be fixed and overflow y hidden, and when the tooltip is opened in the sidebar, it will also appear outside the sidebar. How can I do that? in React

Can't contain video using flexgrid Tailwind CSS

I am having difficulty containing a video element inside my flexgrid. I am simply trying to organize my page to have the navbar at the top of the screen, with a background video playing below it, filling the rest of the screen. Currently I am able to center my navbar over the video, but the video is causing scrolldown functionality. When I replace the video with a simple text object, everything appears to work fine.
// Navbar is a component
<div className='flex flex-col min-h-screen min-w-full'>
<div><Navbar/></div>
<div className='flex-1'> // Should expand the remainder of the screen height
<video src="videos/Demo-Video.mp4"
autoPlay
loop
muted
className='object-cover' // Is supposed to shrink or expand the video the length of the parent div
>Your browser does not support the video tag.</video>
</div>
</div>
not tested, but removing the div wrapping the video might help, or scaling the video to fit that div.
// Navbar is a component
<div className='flex flex-col min-h-screen min-w-full'>
<div><Navbar/></div>
//<div className=''> // Should expand the remainder of the screen height
<video src="videos/Demo-Video.mp4"
autoPlay
loop
muted
className='flex-1 object-cover' // Is supposed to shrink or expand the video the length of the parent div
>Your browser does not support the video tag.</video>
//</div>
</div>

Flexbox - how to align items vertically with flex-col?

const Product = ({product}) => {
return (
<div className="rounded shadow flex-col justify-center overflow-hidden">
<img className="" src={product.image} />
<span className="block h-16">{product.name}</span>
<span className="block">{product.price}</span>
<button className="px-4 py-2 my-2 bg-green-200 rounded block">Add to Cart</button>
</div>
)
}
This is a card component in React using Tailwind CSS.
I cannot get the items to be centered in the card.
I used flex-col so the direction is now vertical and tried justify-center as well as items-center with tailwind but it doesn't seem to be moving anything.
If you use flex-col doesn't the axis get swapped so you need to use align-items: center to make it vertically centered?
The justify-content CSS property aligns on the current flex-direction axis, while the align-items CSS property aligns on the axis perpendicular to the current flex-direction.
Therefore, if the container has flex-direction set to column (.flex-col), you need to use .items-center (in CSS: align-items: center) to center its children horizontally.
However, if the centered child is block level, its text might still be left-aligned even though the element, (being a 100% width box) is centered in the column, in which case you'll need to use text-align: center (.text-center) on the child to horizontally align its contents.
For an in-depth article on flexbox, I recommend A complete guide to flexbox.

How to center conent using React bootstrap

I'm trying to do the infamous center operation both vertical and horizontal and have sorta succeeded in doing so.
I'm trying to render a simple react component on my page that will show a 404 message. I want this message to be centered. The way I managed to do this some whitespace is leftover resulting in vertical scroll bars showing up. Ofc I could get rid of them using something like overflow-hidden, but surely there must be a way to center this message perfectly just using a better structure or certain bootstrap classes.
Here is my component:
const NotFoundPage = () => {
return (
<div>
<NavbarComp />
<div className="d-flex align-items-center justify-content-center text-center min-vh-100">
<div>
<h3 className="m-4">
<Badge variant="primary">404</Badge> Page not found...
</h3>
<Link to="/">
<Button variant="secondary">Go to main page</Button>
</Link>
</div>
</div>
</div>
);
};
In the end, I don't care that the scrollbars appear but it bothers me that this positional issue keeps occurring for me in some form or another and I wanna learn to put an end to this :)
You have already figured out how to center your "Not Found" message. What's messing it up for you is the Navbar, which you can test by taking it out. You will notice that the scroll bars disappear.
This class min-vh-100 gives the flex box below the Navbar the height of 100% of the viewport, which avoids adding any scrolls bars. The issue is that Navbar barges in and throw the page off blalance by attaching itself to the top. So the amount of srolling is exactly the height of the Navbar and since it's empty, there is very little scrolling.
An intutitive solution is to put your Navbar INSIDE the flex box. But that won't work in your case, because of how your flex box adjusts items. So I will give you a simpler solution, which should be viewed as more of a work-around.
Bootstrap does not come with classes such as min-vh-95 or min-vh-80, but luckily they are super easy to create. You can add this class to your custom CSS:
.not-found-container {
min-height: 95vh !important;
}
(or you could just stick to the naming convention and name it min-vh-95 for example)
And then change the flex box classes from:
<div className="d-flex align-items-center justify-content-center text-center min-vh-100">
to
<div className="d-flex align-items-center justify-content-center text-center not-found-container">
Here is a Sandbox to illustrate the idea: https://codesandbox.io/s/peaceful-sanne-e3m9w?file=/src/App.js
Obviously the min-height value would need to be adjusted based on the height if your Navbar.

n3-chart width not set according to parent width

In template file
<div class="chart-container">
<linechart data="data" options="options" mode=""></linechart>
</div>
In css file:
.chart-container > .chart {
width:800px;
height:500px;
}
The svg element is always taking the width of 1333px. Are there any options to set height and width? Or am I doing something wrong?
Here is the image:
Even in the full view I have set width to 800px but the svg element is taking 1333px.
When I resize it then it works.
Change your css definition to below. Your current css will set size of chart-container that has children of chart but not setting chart width and height itself. So that the chart div will growth to the size of svg.
.chart-container, .chart {
width:800px;
height:500px;
}

Resources