The centering and overflow issues with shadcn dialogs on mobile are common problems. Here's how to fix them:
CSS Solution
Add these styles to your global CSS or component styles:
css Copy code
/* Fix dialog positioning and overflow on mobile */
@media (max-width: 768px) {
[data-radix-dialog-content] {
position: fixed !important;
top: 50% !important;
left: 50% !important;
transform: translate(-50%, -50%) !important;
width: calc(100vw - 2rem) !important;
max-width: none !important;
max-height: calc(100vh - 2rem) !important;
margin: 0 !important;
}
/* Ensure the dialog overlay covers the full viewport */
[data-radix-dialog-overlay] {
position: fixed !important;
inset: 0 !important;
}
}
Component-Level Solution
If you prefer to handle it at the component level, modify your Dialog component:
tsx Copy code
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { cn } from "@/lib/utils"
export function ResponsiveDialog({ children, className, ...props }) {
return (
<Dialog {...props}>
<DialogContent
className={cn(
// Desktop styles
"sm:max-w-md sm:top-1/2 sm:left-1/2 sm:translate-x-[-50%] sm:translate-y-[-50%]",
// Mobile styles
"max-sm:fixed max-sm:top-1/2 max-sm:left-1/2 max-sm:translate-x-[-50%] max-sm:translate-y-[-50%]",
"max-sm:w-[calc(100vw-2rem)] max-sm:max-w-none max-sm:max-h-[calc(100vh-2rem)]",
"max-sm:m-0",
className
)}
>
{children}
</DialogContent>
</Dialog>
)
}
Alternative Tailwind-Only Solution
You can also use Tailwind classes directly on your DialogContent:
tsx Copy code
<DialogContent className="
fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2
w-[calc(100vw-2rem)] max-w-md
max-h-[calc(100vh-2rem)]
sm:w-full sm:max-w-md
">
{/* Your dialog content */}
</DialogContent>
Key Points
The solution addresses:
Centering : Uses top: 50%, left: 50% with translate(-50%, -50%)
Width overflow : Sets width to calc(100vw - 2rem) on mobile
Height overflow : Limits max-height to calc(100vh - 2rem)
Responsive behavior : Different styles for mobile vs desktop
This should give you a properly centered dialog that doesn't overflow on mobile while maintaining the desktop behavior you want.