Do elements with “position: absolute;” behave as block-level elements?

前端 未结 2 482
深忆病人
深忆病人 2020-12-07 03:44

Are elements with position: absolute; able to have a height, width, padding, etc. without changing it to display: block;

相关标签:
2条回答
  • 2020-12-07 04:18

    The spec says yes:

    Otherwise, if 'position' has the value 'absolute' or 'fixed', the box is absolutely positioned, the computed value of 'float' is 'none', and display is set according to the table below. The position of the box will be determined by the 'top', 'right', 'bottom' and 'left' properties and the box's containing block.

    The table says that any inline display values (inline, table-row-group, table-column, table-column-group, table-header-group, table-footer-group, table-row, table-cell, table-caption, inline-block) become block.

    0 讨论(0)
  • 2020-12-07 04:19

    Short answer: yes.

    Long answer: see http://www.w3.org/TR/CSS21/visuren.html#dis-pos-flo (or SLaks's answer).

    Hovewer, this change refers only to the visual display model of the element. The CSS can't affect the content model of the HTML element since CSS is applied only after the document is parsed into DOM tree. So span element can never have p or div children, no matter which styles are applied to it.

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