tailwind-cssradixshadcnui

Shadcn Dialog component margin


Trying to add some simple margin to left and right of a dialog box when viewing on a mobile screen. No matter where I seem to add mx-5 it doesn't actually work. Any ideas on how to add some margin to a shadcn dialog when viewing on a phone like this?

Example of Dialog on mobile without margin

Just about every place I can find on this component I added an mx-5 to add left and right margin but it doesn't seem to have an effect. Also tried sm:mx-5 hoping it would apply on mobile screens but no luck.


Solution

  • One way around it is first to note that DialogContent has a max-w-lg around 32rem 512px for large screens and :max-w-md for smaller screens. To fix this, add a tailwind class w-11/12 - making the width not 100%.

     <DialogContent className="w-11/12 sm:max-w-md">
    
    

    Below is an example from the docs - I've added the class to show the example.

    import { Copy } from "lucide-react"
    
    import { Button } from "@/components/ui/button"
    import {
      Dialog,
      DialogClose,
      DialogContent,
      DialogDescription,
      DialogFooter,
      DialogHeader,
      DialogTitle,
      DialogTrigger,
    } from "@/components/ui/dialog"
    import { Input } from "@/components/ui/input"
    import { Label } from "@/components/ui/label"
    
    export function DialogCloseButton() {
      return (
        <Dialog>
          <DialogTrigger asChild>
            <Button variant="outline">Share</Button>
          </DialogTrigger>
          <DialogContent className="w-11/12 sm:max-w-md">
            <DialogHeader>
              <DialogTitle>Share link</DialogTitle>
              <DialogDescription>
                Anyone who has this link will be able to view this.
              </DialogDescription>
            </DialogHeader>
            <div className="flex items-center space-x-2">
              <div className="grid flex-1 gap-2">
                <Label htmlFor="link" className="sr-only">
                  Link
                </Label>
                <Input
                  id="link"
                  defaultValue="https://ui.shadcn.com/docs/installation"
                  readOnly
                />
              </div>
              <Button type="submit" size="sm" className="px-3">
                <span className="sr-only">Copy</span>
                <Copy className="h-4 w-4" />
              </Button>
            </div>
            <DialogFooter className="sm:justify-start">
              <DialogClose asChild>
                <Button type="button" variant="secondary">
                  Close
                </Button>
              </DialogClose>
            </DialogFooter>
          </DialogContent>
        </Dialog>
      )
    }