It's all about the CSS properties
or their combinations.
The bunch of related concepts you need to understand is called CSS box model
You can read how they work here:
You can probably get a good idea how it all works at once, if you simply look at the picture shown in the second page referenced above.