Skip to content

[css-sizing-4] Add margin-box for box-sizing? #5715

Open

Description

@fantasai

There exist some use cases for margin-box sizing. Do we want to add this as a keyword to box-sizing and/or allow the sizing properties to take a *-box keyword to indicate which box to apply to? That would avoid authors having to manually "back out" their margins for e.g. image+margin = 100vh or float+margin = 50% of containing block.

Activity

  1. Loirooriol commented on Nov 11, 2020

    @Loirooriol
    Contributor

    I think margin collapse can make this confusing. Like if I have

    <div style="height: 100px; border-left: 10px solid">
      <p style="box-sizing: margin-box; height: 50%; margin: 20px 0; background: cyan"></p>
      <p style="box-sizing: margin-box; height: 50%; margin: 20px 0; background: cyan"></p>
    </div>

    then I would probably expect the content-area height of the <p> to be 40px, so that 0px+40px+20px+40px+0px = 100px. But if I have

    <div style="height: 100px; border-left: 10px solid">
      <div style="display: flow-root"></div>
      <p style="box-sizing: margin-box; height: 50%; margin: 20px 0; background: cyan"></p>
      <div style="display: flow-root"></div>
      <p style="box-sizing: margin-box; height: 50%; margin: 20px 0; background: cyan"></p>
      <div style="display: flow-root"></div>
    </div>

    then I would probably expect the content-area height of the <p> to be 10px, so that 20px+10px+20px+20px+10px+20px = 100px.

  2. fantasai commented on Nov 11, 2020

    @fantasai
    CollaboratorAuthor

    I don't think the main use cases are percentage heights in the margin-collapsing dimension of block layout, though. :) It think it's fine that in the first example, the content height doesn't fill the container. Margin collapsing should be ignored for this purpose.

  3. Loirooriol commented on Nov 11, 2020

    @Loirooriol
    Contributor

    Well, even if not the main usecase, authors may start using *, ::before, ::after { box-sizing: margin-box } because it works as they want in simple cases, but then with margin collapsing, the content areas will shrink too much.

  4. fantasai commented on Aug 5, 2026

    @fantasai
    CollaboratorAuthor

    If we resolve to add this, we should apply it to (at least) ::picker() in the UA stylesheet. See #14172

  5. Loirooriol commented on Aug 5, 2026

    @Loirooriol
    Contributor

    We handled margin collapse for stretch sizes. So, I guess it's fine if we say that stretch is like 100% with box-sizing: margin-box.

    Then my examples in #5715 (comment) would treat the margins as 0, i.e. box-sizing: margin-box would behave as border-box.

    But if the containing block had block-axis border/padding, or established an independent FC, then margin-box would be different.

  6. fantasai commented on Aug 13, 2026

    @fantasai
    CollaboratorAuthor

    @Loirooriol I'm not sure that we should treat margins as 0 in these cases. The idea of stretch is to fill the containing block. But you might set, for example, max-height: 100vh; box-sizing: margin-box; margin: 1em on some images. That should account for the margins you set regardless of whether the image's containing block (which might be an unstyled <section> or the styled <article> above it) has borders or padding.

    So I think the definition here should keep it simple: we compute the margin, and we subtract it always, just like padding. Auto margins are treated as zero, and margin collapsing is not taken into account.

  7. Loirooriol commented on Aug 13, 2026

    @Loirooriol
    Contributor

    I think it should happen for percentages. But maybe not for lengths. But then what about calc() mixing both?

  8. fantasai commented on Aug 13, 2026

    @fantasai
    CollaboratorAuthor

    Percentages are resolved against the containing block, and are otherwise exactly equivalent to the equivalent length. I don't think we want to change that.

  9. css-meeting-bot commented on Aug 26, 2026

    @css-meeting-bot
    Member

    The CSS Working Group just discussed [css-sizing-4] Add `margin-box` for box-sizing?.

    The full IRC log of that discussion <emilio> fantasai: we discussed the need for this when discussing the select popup sizing
    <emilio> ... but this has come up before in more general context
    <emilio> ... would be useful like for inflow-content and image, the image should fit in the viewport
    <emilio> ... you give it height 100vh
    <emilio> ... that's fine until you have a margin
    <oriol> q+
    <emilio> ... proposal is to make it work just like border-box
    <emilio> ... would ignore margin collapsing
    <emilio> ... auto would be treated as 0
    <emilio> q+
    <iank_> q+
    <astearns> ack oriol
    <emilio> q+ later
    <lea> +1 for adding this
    <emilio> oriol: concerned about margin-collapsed
    <astearns> ack emilio
    <emilio> ... e.g. height: 100% the expected behavior would be the same as stretch
    <emilio> ... but it'd be different with the proposed behavior
    <emilio> ... stretch special-cases some collapsing margins
    <emilio> ... instead of applying the margin to the element we move it to the CB
    <emilio> ... if we subtract this margin which is not taking any spacing in the containing block
    <emilio> ... specially when specifying sizes with percentages we want to be consistent with stretch
    <emilio> ... less sure for lengths
    <emilio> ... if we do different things for percents vs. lengths
    <astearns> ack fantasai
    <Zakim> fantasai, you wanted to respond
    <emilio> ... so I think I'm considering being consistent with percentages with lengths
    <emilio> fantasai: I don't think think we want to copy stretch
    <emilio> ... you have stretch if you want
    <emilio> ... we want to allow the author to set something to an explicit size
    <emilio> ... don't want to treat percentages and lengths inconsistent because they often mix
    <emilio> oriol: so if you want to specify size against the viewport or whatever, if your CB is ??
    <emilio> ... you already get the behavior you're proposing right?
    <emilio> fantasai: but for inflow content that wouldn't work
    <emilio> ... e.g. an image inside a <section>
    <emilio> ... you want the image to have height: 100% of the viewport plus some margin
    <emilio> ... the margin might collapse
    <emilio> ... I have a margin of 1em
    <emilio> ... I want that to be subtracted
    <emilio> oriol: but you can set flow-root on the container for that right?
    <emilio> fantasai: but the margins I want to continue to collapse
    <emilio> ... I get at least 1em of margin
    <emilio> ... the parent might not be the containing block, you have a ton of div / section / etc...
    <emilio> ... If I set a margin on the element I actually want it to be honored
    <astearns> ack iank_
    <astearns> q+
    <emilio> iank_: so I took a look at decisions based on box-sizing... what happens with aspect-ratio?
    <emilio> fantasai: what happens with aspect-ratio currently?
    <emilio> iank_: depends on whether you set aspect-ratio
    <emilio> ... it'll do the aspect ratio on the content-box / border-box
    <emilio> ... is that what we want here?
    <emilio> fantasai: yes, that'd be consistent
    <emilio> iank_: seems weird to have an aspect ratio based off something invisible
    <emilio> fantasai: that might be weird yes
    <emilio> iank_: that might be a separate issue
    <emilio> iank_: we want the invariant that the border-box size isn't smaller than the border + padding cases
    <emilio> fantasai: that should still hold, we floor the content-box size
    <emilio> iank_: there's a weird case for scrollbar
    <oriol> q+
    <emilio> iank_: the other thing is that tables support specifying margins but don't have margins, so if you implement that add tests for it
    <emilio> iank_: there's a few other things for tables but they are not particularly difficult
    <CameronPsychpsyo> q+
    <kbabbitt> emilio: my gut reaction is that this introduces a lot of subtle complexities for use cases that ... image case can get away with a calc
    <kbabbitt> ... won't object to adding it but it feels like the amount of complexity for benefit might not be great
    <kbabbitt> ... as an author it feels pretty weird ... sizing to border-box makes sense because that's what you see
    <lea> q+
    <kbabbitt> ... understand that's what you want for select popup but don't see this as super broadly useful, may be wrong there
    <emilio> ack emilio
    <kbabbitt> astearns: if we don't add it, does the calc solution work for select picker?
    <emilio> astearns: if we don't add it, is the calc() solution fine for select?
    <astearns> ack astearns
    <emilio> fantasai: it won't work in the UA sheet
    <iank_> why doesn't stretch work?
    <emilio> ... because you don't know the margin before-hand
    <emilio> ... authors use variables to track this stuff
    <emilio> fantasai: stretch doesn't stretch to the right size, for the popover it stretches to the position-area not the viewport
    <emilio> ... the example I gave where it's an in-flow element also wouldn't work
    <emilio> astearns: historical question, I expect we added border-box because that solved the issue we were trying to solve, but we were open to add more values to box-sizing later, but I don't know if we explicitly asked for others
    <iank_> I believe padding-box got kicked due to lack of use cases.
    <emilio> fantasai: could dig it out but guess is we didn't have the use cases
    <emilio> dbaron: I suspect margin-box was kicked for concerns around collapsing
    <iank_> stretch(100%) ?
    <emilio> fantasai: we could make stretch() take a length
    <astearns> ack oriol
    <emilio> ... but stretch kw on its own won't
    <emilio> oriol: if for margin-collapsing we do it in the way fantasai proposes, I also wonder about margin-trim
    <emilio> ... we could be subtracting some margins that could end up trimmed
    <emilio> ... not sure about margin-trim
    <emilio> fantasai: I'd ignore it
    <iank_> (we already have fit-content(<length>) so maybe stretch(<length>) isn't bad)
    <emilio> ... the idea is to trim margins / whatever where the spacing is provided
    <emilio> ... in a number of cases like collapsing / trim the layout engine is removing it from its calculation but there's spacing there
    <emilio> ... so you still probably want to subtract that margin so the space is allocated for sizing purposes
    <emilio> oriol: over all I agree with emilio that it will add a bit of confusion
    <fantasai> iank_, yeah ... I think I might agree, except for the special cases we have with margin-collapsing for stretch, we don't want them here.
    <emilio> ... not totally convinced it's great
    <astearns> ack CameronPsychpsyo
    <emilio> q+
    <fantasai> I think.
    <emilio> CameronPsychpsyo: this got answered but there's also fragmentation, but there's also "collapsing" in fragmentation
    <emilio> ... but yeah the space is there is just not on the element
    <emilio> lea:
    <astearns> ack lea
    <emilio> lea: I think there are valid use cases for this, so +1 from me
    <iank_> fantasai: it would solve the popover usecase immediately
    <emilio> ... use cases for overlays etc
    <emilio> ... I'd disagree with emilio that it doesn't make sense
    <emilio> ... that's how you'd expect it to work
    <emilio> ... don't use it if you don't think it's useful
    <emilio> ... any weirdness is already there with box-sizing
    <astearns> ack emilio
    <CameronPsychpsyo> Although box-sizing currently does not need to worry about margin collapsing, trimming, etc.
    <kbabbitt> emilio: agree the use case is not specific to select but most of the use cases about abspos... that's a constrained layout mode
    <kbabbitt> ... don't need to deal with intrinsic sizing etc.
    <kbabbitt> ... other question is how do percentages behave?
    <kbabbitt> ... I guess % padding already hits this so not a big issue but wondering if it might be a bit of a footgun
    <emilio> astearns: iank_ suggested stretch(<length>) rather than this?
    <emilio> ... should we take it back?
    <emilio> ... and discuss the pros / cons of each approach?
    <emilio> ... should we do both?
    <emilio> fantasai: I think we should only do one of them
    <emilio> ... not 100% sure which one's a better plan
    <emilio> ... the advantage of stretch() is you can do it per-axis
    <emilio> ... you could use stretch(100vh) on your max size but still have your min-size be border-box sizing
    <emilio> ... so I think that's one advantage of stretch()
    <emilio> ... not 100% sure we want the percents to do this weird thing
    <miriam> (I think that's a pretty good advantage for stretch())
    <emilio> ... with the margin's joining top / bottom but I could be convinced
    <emilio> ... so stretch(100%) would be equivalent to stretch()
    <emilio> astearns: since you're not sure we should probably take it back to the issue and discuss both
    <emilio> ... and get a bit more discussion in the issue
    <emilio> ... does that work?
    <oriol> I prefer stretch() too
    <emilio> iank_: marginally prefer stretch
    <emilio> fantasai: I think the stretch margin behavior we might not want that
    <emilio> ... would be weird to make stretch and stretch() inconsistent
    <emilio> ... but there's definitely advantages to the functional notation
  10. JoshTumath commented on Aug 27, 2026

    @JoshTumath
    Contributor

    @bfgeek, so for the stretch(<length>) function you are suggesting that would be similar to fit-content(), would it basically use this formula: max(min-content, min(the-author-specified-limit, stretch)), where 'stretch' is the existing stretch keyword?

  11. Loirooriol commented on Aug 27, 2026

    @Loirooriol
    Contributor

    @JoshTumath My understanding is that prop: stretch(<length-percentage>) would be like:

    1. Resolve the <length-percentage>. Cyclic percentages might need some special consideration, maybe resolve like https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution would for prop: <length-percentage>, and if that would produce initial, treat the entire stretch(<length-percentage>) as stretch and finish.
    2. Then, treat stretch() as stretch, but pretending that the size of the containing block is the result from (1)

    So stretch would be equivalent to stretch(100%).

    And instead of width: 100px; box-sizing: margin-box you would use width: stretch(100px).

  12. 19 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      Sponsor
      SponsoredKunjungi sekarang
      Promo