Sew a flat button back onto a shirt
Use the original stitch position and leave enough clearance for the buttonhole to close.

Find the original position
Match the loose button to the others in size and hole pattern. Check the inside seam or care label for a spare if it went missing. Lay the shirt flat and use the marks left by the original stitches, or line up the placket and mark the point through the buttonhole.
Sew through the original marks
Choose a matching thread and start on the inside. Pass the needle through the fabric and button holes several times, following the stitch pattern of the neighbouring buttons. Keep the stitches firm with enough space for the buttonhole layer underneath. A toothpick laid across a flat button gives you a small, consistent gap while stitching.
Secure the thread and test the closure
Remove the spacer, wrap the thread under the button a few times to support the gap, then pass the needle back through to the inside. Tie it off securely and trim the tail. Fasten and unfasten the button twice to check it sits level and passes through without straining the fabric.