Anchor Tag's Dimensions
Anchor tags, by default, do not assume the dimensions of their containing elements. This is explained by the CSS 2.1 specification, which governs how elements are laid out on a web page.
Non-Replaced Inline Element
Height Calculation
For non-replaced inline elements, the 'height' property is not applicable. The height is typically determined by the font metrics of the element's content. In the provided example, the height is 18px, which is determined by the size of the text contained within the anchor.
Width Calculation
The 'width' property also does not apply directly to non-replaced inline elements. The width is calculated based on the element's paddings, borders, margins, and contents.
Image Content
The images within the anchor tags do not contribute to the width calculations. This is because the width of inline non-replaced elements is not influenced by their content size.
Margin Collapse
Spaces between anchor tags in an inline context follow margin collapse rules. In this case, consecutive spaces between anchor tags collapse into a single space. The space at the end of the first anchor contributes to its width, while the space at the start of the second anchor does not.
Example Calculations
The first anchor tag has a width of 114px (image space) 20px (left margin) 10px (left and right borders) = 144px.
The second anchor tag has a width of 280px (image) 20px (left margin) 10px (left and right borders) = 310px.
Conclusion
Anchor tags, being non-replaced inline elements, do not inherit the height or width of their containing elements. Their dimensions are primarily determined by their own content (text), paddings, borders, margins, and the application of margin collapse rules in an inline context.
The above is the detailed content of Why Don't Anchor Tags Inherit the Dimensions of Their Containing Elements?. For more information, please follow other related articles on the PHP Chinese website!