How do you fix a collapse margin in CSS?

just add a 1px negative top margin! display:inline-block also works by the way. it still causes a 1px difference in IE5 if you have the height set on the maincontainer.

Do margins collapse in CSS?

The top and bottom margins of blocks are sometimes combined (collapsed) into a single margin whose size is the largest of the individual margins (or just one of them, if they are equal), a behavior known as margin collapsing. Note that the margins of floating and absolutely positioned elements never collapse.

How do you resolve margin collapse?

The basic stuff to know:

  1. Margin collapsing only happens in the block-direction.
  2. The largest margin “wins”
  3. Any element in between will nix the collapsing (if we’re talking within-parent collapsing, even a bit of padding or border will be the in-between thing and prevent the collapsing, as Geoff noted when he covered it).

Why are my margins collapsing?

Collapsing margins happen when two vertical margins come in contact with one another. If one margin is greater than the other, then that margin overrides the other, leaving one margin. It’s like the bigger margin straight up ate the other one and left nothing behind.

How do you prevent margin collapse in CSS?

Using a padding or border will prevent collapse only in the latter case. Also, any value of overflow different from its default ( visible ) applied to the parent will prevent collapse. Thus, both overflow: auto and overflow: hidden will have the same effect.

Will padding collapse?

You can think of padding/border as a sort of wall; if it sits between two margins, they can’t collapse, because there’s a wall in the way. The width doesn’t matter, either; even 1px of padding will interfere with margin collapse.

How does margin work CSS?

The CSS margin properties are used to create space around elements, outside of any defined borders. With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

What is padding vs margin?

In CSS, a margin is the space around an element’s border, while padding is the space between an element’s border and the element’s content. Put another way, the margin property controls the space outside an element, and the padding property controls the space inside an element.

Do margins and padding collapse?

The top or bottom margin of an element collapses when it touches the margin of its first child element. If an element has no height, padding, or border and all of its children elements’ margins collapse, it will take up no (vertical) space.

When two vertical margins meet what happens?

A collapsed margin is what occurs when two block-level elements with meeting vertical margins combine. When this happens, the larger of the two margins (or any if they are equal) is assumed as the single collapsed margin.

What does margin auto do in CSS?

The auto Value You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.

What is a good margin for website?

Brick-and-mortar retailers tend to have profit margins between . 5 and 4.5%. Web-based retailers generally have higher profit margins, while building supply and distribution retailers have the best margins⁠—reaching as high as 6.5%.