Why are my div margins overlapping and how can I fix it?

Solution 1:

I think it's a collapsed margin. Only the largest margin between the bottom of the first element and the top of the second is taken into account.

It is quite normal to don't have too much space between two paragraphs eg.

You cannot avoid that with two adjacent elements so you have to enlarge or reduce the larger margin.

EDIT: cf. W3C

Two margins are adjoining if and only if:

  • both belong to in-flow block-level boxes that participate in the same block formatting context
  • no line boxes, no clearance, no padding and no border separate them
  • both belong to vertically-adjacent box edges

So there is no collapsing with float which takes the element out of the flow.

Solution 2:

Margins, in contrary to padding (which pads a specific width) is a “do this as a minimum distance”.

It won’t put that distance to all elements.

As you can see, the get in touch block bottom margin is marged to the input box. That is the margin active here. The other margin, top margin from the input, is not in effect, as it’s smaller and does not reach a block-element where it would actually push back the element. The 2 margins overlap and don’t affect one another.