Introduction
It is commonly understood that placing a block element within an inline element is incorrect. However, what happens when the reverse occurs? Specifically, if a div containing a paragraph is transformed into inline by CSS, does it still adhere to HTML validation rules?
CSS Specification
According to the CSS 2.1 specification, an inline box containing a block-level box will be split into two anonymous block boxes, one before and one after the block-level box. These boxes become siblings of the block-level box. This behaviour applies even if either anonymous block box is empty.
HTML Validation
The HTML validation status depends on whether it is assessed prior to or after CSS application. Before CSS is applied, the HTML is valid. However, after CSS transformation, the document may no longer be valid according to the HTML specification.
Implementation and Browser Support
The CSS specification provides guidelines for how browsers should handle this scenario. However, browser implementations may vary. Some browsers have implemented borders on inline elements containing blocks by creating "anonymous line boxes" around the nested blocks. This behaviour is not explicitly defined in CSS1 or CSS2 and may not be consistent across all browsers.
HTML5 Considerations
In HTML5, it is permissible to place block-level elements inside inline elements, such as links. This can be useful when creating large blocks of clickable HTML content.
Conclusion
While the behaviour of block elements contained within inline elements after CSS transformation is specified in the CSS specification, the interpretation and implementation of this rule may vary across browsers. The impact on HTML validation depends on when the validation is performed, before or after CSS application.
The above is the detailed content of Is Converting Block Elements to Inline via CSS, When Containing Other Block Elements, Valid HTML?. For more information, please follow other related articles on the PHP Chinese website!