Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a percentage margin cause a new line?

Tags:

html

css

margin

<div style = "float : left; background-color: #dd3fb8;">
    <a style = "margin-left : 10%;">a</a> 
    <a>b</a> 
    <a>c</a> 
</div>

In the example above, the letter "c" would be on new line, but if I set "margin-left" to px unit, "c" would be on the same line as "a" and "b". Why does this happen?

like image 989
user3720957 Avatar asked Jun 09 '14 02:06

user3720957


People also ask

What will happen if the margin is set to auto?

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.

Why is margin left?

The margin-left CSS property sets the margin area on the left side of an element. A positive value places it farther from its neighbors, while a negative value places it closer.

What is margin in CSS?

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.

What is margin in HTML?

The margin property sets or returns the margins of an element. This property can take from one to four values: One value, like: div {margin: 50px} - all four margins will be 50px. Two values, like: div {margin: 50px 10px} - the top and bottom margins will be 50px, left and right margins will be 10px.


3 Answers

Unfortunately, the CSS2.1 spec doesn't appear to have a clear answer to this. In fact, I would say this is well within the realm of undefined behavior.

Here are the relevant points I can find:

  • Floats without a specified width will shrink to fit their contents. In the case of floats with only inline content, the float needs to be made just wide enough to fit its contents on a single line (notwithstanding explicit line breaks) and no more.

  • Percentage margins are calculated based on the width of the containing block.

    Note that it says:

    If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.

    ... but as far as I can see, the behavior is consistent across all browsers.

    That being said, the reason this statement applies is because since the margin of the inline element falls within the content bounds of the float, it can be said that the width of the float (the containing block of the inline elements) depends on the this element (the element having the margin).

Here's what I can deduce based on the points above:

  • When the margin is specified as a percentage, the width of the float is calculated without taking the margin into account, because it's not possible to calculate the margin until the width of the float has been determined.

    The margin is then calculated based on the used width of the float, and the letter "c" wraps to a new line as a result of being pushed forward by the margin on "a". The width of the float does not change.

    Again, none of this behavior is specified at all, and so technically it's not in violation of the spec. However, it seems sensible.

  • When the margin is specified as a pixel value, the margin is calculated first. The width of the float is then calculated taking this margin into account (remember that horizontal margins do apply to inline elements as normal). Per the shrink-to-fit algorithm, this is the preferred width: just wide enough to contain all the inline elements on a single line.

    Unlike with percentage margins, this is very clear-cut, as implementations should have no trouble calculating computing absolute values for margins first.

I would be hard-pressed to call this a bug in any of the browsers, especially since they all behave consistently.

Lastly, of course, you can avoid this undefined behavior entirely simply by giving your floats explicit widths where possible. It does help to understand why you should do so, however.

like image 93
BoltClock Avatar answered Oct 15 '22 09:10

BoltClock


Since your div is floated, and its width is auto (implicitly), http://www.w3.org/TR/CSS21/visudet.html#float-width applies:

If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.

“shrink-to-fit” width basically means, let the element be as wide as its content requires it to be.

Now without the margin-left, that is no problem: All three of your a elements are inline elements that contain a specific character each – easy enough to determine their individual widths, and add them up.

But now you want a margin-left in percent, and here things get complicated – if we look at the definition for margin-left, it says:

Percentages: refer to width of containing block

Now, that leaves us in a bit of a pickle, since the width of the containing block (which is established by the floated div element), is computed based on its content – but now this margin-left would change the overall width of that content, but is in itself dependent on the width of the containing block, which it itself influences …

That’s a classical problem of two measurements that are dependent on each other … and that is therefor basically unsolveable.

http://www.w3.org/TR/CSS21/box.html#margin-properties says,

 The percentage is calculated with respect to the width of the generated box's containing block. […]
If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.


Edit: Basically the same as what BoltClock said in his answer, just took me a little longer …

like image 34
CBroe Avatar answered Oct 15 '22 10:10

CBroe


The link has a left margin of 10%, 10% of how much? The parent element is floated left which means it does not have a width of its own, instead it expands as much as its contents. If you try to imitate how the browser would compute the resulting box and you will find yourself in a fix:

  • Let the width of the content (and therefore the container) be 30px
  • Add 10% of 30px = 3px left margin to the link
  • The resulting width of the container is 30 + 3 = 33px

This creates a loop where margin increases as outer width is increased and outer width increases as the margin is increased (10% of 33px = 3.3px means container width changes from 33px to 33.3px and so forth). For such computations the resulting behavior is undefined (as pointed out by CBroe).

The browser seems to avoid the loop and sticks with the 30px width. The 3px margin introduced after calculation causes the third link to flow into second row. The browser again avoids the loop by sticking with 30px width.

like image 26
Salman A Avatar answered Oct 15 '22 10:10

Salman A