Box Model for Inline Elements

前端 未结 2 1110
故里飘歌
故里飘歌 2021-01-23 06:00

W3C\'s CSS2.1 specification, chapter 8.6 The box model for inline elements in bidirectional context, states:

For each line box, UAs must take the inline b

相关标签:
2条回答
  • 2021-01-23 07:00

    For the second question you may refer to this part of the specification:

    The 'height' property does not apply. The height of the content area should be based on the font, but this specification does not specify how. A UA may, e.g., use the em-box or the maximum ascender and descender of the font. (The latter would ensure that glyphs with parts above or below the em-box still fall within the content area, but leads to differently sized boxes for different fonts; the former would ensure authors can control background styling relative to the 'line-height', but leads to glyphs painting outside their content area.)

    The vertical padding, border and margin of an inline, non-replaced box start at the top and bottom of the content area, and has nothing to do with the 'line-height'. But only the 'line-height' is used when calculating the height of the line box.

    .test {
      margin: 0 10px;
      padding: 20px;
      border: 5px solid blue;
    }
    
    div {
     border:1px solid red;
     margin:50px 0;
    }
    <div>
    <span class="test">test2test2test2test2test2test2test2 test</span>
    </div>
    
    <div>
    <span class="test">test2test2test2test2test2test2test2 test st2test2test2test2 test st2test2test2test2 test st2test2test2test2 test</span>
    </div>
    
    
    
    <div>
    <span class="test" style="line-height:50px;">test2test2test2test2test2test2test2 test2test2</span>
    </div>
    
    <div>
    <span class="test" style="line-height:50px;">test2test2test2test2test2test2test2 test2test2 test2test2test2test2test2test2test2 test2test2</span>
    </div>

    0 讨论(0)
  • 2021-01-23 07:02

    To answer the first question:

    It says "left-most generated box", which indicates that the inline-level element creates more than one inline-level box. Is that because at each line break it creates a new anonymous inline-level box?

    Yes, but that's only one reason. An inline-level element can create zero, one or many inline-level boxes. So an inline element with no content or horizontal padding, border, or margin will create zero inline-level boxes. A different way in which an element can create multiple inline-level boxes is if the element contains child elements.

    So if we have <span>foo <b>bar</b> baz</span>, then even if that all sits on one line, the span will create one inline-level box for foo, which will get the left margin, left border and left padding, and a separate inline-level box for baz, which will get the right margin, right border and right padding.

    The b element creates the inline-level box for bar, which may have its own margins, borders, and paddings.

    See Temani's answer for your second question.

    0 讨论(0)
提交回复
热议问题