I'm working with a Next.js project and have the following layout setup. My issue is that a div in my Page.tsx extends beyond the screen, causing unwanted overflow. I would like to make sure that the content is fully contained within the viewport.
Here are the relevant parts of my code:
Layout.tsx code
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar";
import { AppSidebar } from "@/components/app-sidebar";
import { AuthProvider } from "@/AuthContext";
import { ThemeProvider } from "@/components/theme-provider"
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: "Lumos",
description: "SNU CGPA Calculator",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en" suppressHydrationWarning>
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
<ThemeProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
>
<SidebarProvider>
<AuthProvider>
<AppSidebar />
</AuthProvider>
<main>
{children}
</main>
</SidebarProvider>
</ThemeProvider>
</body>
</html>
);
}
Page.tsx Code
import { ModeToggle } from "@/components/dark-mode-toggle";
export default function Home() {
return (
<>
<div className=" border border-red-200 w-screen">
<div className=" absolute right-0 m-4 outline outline-1 dark:outline-white outline-black rounded-sm">
<ModeToggle />
</div>
<div className="flex items justify-center h-full w-full">
Hello
</div>
</div>
</>
);
}
Issue: As you can see in the screenshot below, the content (div) extends beyond the screen, causing an unwanted horizontal scroll. I want to ensure that all content remains within the viewport without causing overflow.
What I've tried:
I used w-screen for width, but it didn't solve the issue. The h-full on the child div seems to make it extend beyond the screen as well.
it is because the w-screen in the Home div that wraps the full component instead you can do
calc(w-screen - sidebarWidth)
in tailwind will be like this
<div className=" border border-red-200 w-[calc(100vw_-_sidebarWidth)]">
<div className=" absolute right-0 m-4 outline outline-1 dark:outline-white outline-black rounded-sm">
<ModeToggle />
</div>
<div className="flex items justify-center h-full w-full">
Hello
</div>
</div>