The border attribute (border) is used to set the edge of an element.
The margin attribute (margin) is used to set the distance between the edge of the space occupied by an element and adjacent elements.
The gap attribute (padding) is used to set the distance from the element content to the element boundary.
These three attributes all belong to the box type attributes in CSS.
The diagram below vividly reflects the differences between the three.
Here is a 3D schematic diagram of the box model, I hope it will be easier for you to understand and remember.