Why does break outside
when margin and padding is applied?

前端 未结 3 1622
礼貌的吻别
礼貌的吻别 2021-02-19 09:03

I know this is very basic CSS. How do I keep the span contained within the div? At the moment, the span extends outside the top and bottom of the div.

相关标签:
3条回答
  • 2021-02-19 09:22

    The vertical padding, border and margin of an inline, non-replaced box (e.g. span) 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. Therefore you see an overlapping here: http://jsfiddle.net/Q9AED/

    If you want to use a straightforward solution, you can use line-height rather than display: inline-block: using line-height.

    display: inline-block works in Safari >= 2, Opera >= 9, IE >= 8, Firefox > 3. But you can imitate an inline-block in IE < 8: display: inline; zoom: 1.

    0 讨论(0)
  • 2021-02-19 09:23

    To answer your question, this isn't just an issue with padding or margin, but also with width, display, and the box model.

    jsFiddle

    span {
        display: inline-block;
    }
    

    This will honor any padding, margins, or widths you apply to the span.

    0 讨论(0)
  • 2021-02-19 09:34

    Inline elements will not consume vertical padding and margin space. You can make the span display: block, but without more detail I don't know if that will achieve your goal.

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