CSS Child Selector: Why Can't I Select Descendants Directly?
In CSS, the child selector (>) denotes a direct parent-child relationship between elements. However, applying the child selector to descendants of descendants can fail. Consider the following example:
table > tr > td
This selector will correctly select the td elements, as a td element is a direct child of a tr element, and tr is a direct child of a table element. However, the following selector fails:
table > tr > td
Understanding the Missing tbody Element
HTML documents implicitly create a tbody element to contain tr elements unless explicitly defined. As such, tr is never a direct child of table, breaking the direct parent-child relationship required by the child selector.
The Solution: Adding tbody Selectors
To resolve this issue, you must add the tbody selector to bridge the gap between table and tr:
table > tbody > tr > td
Implications for XHTML Documents
XHTML documents served as application/xhtml xml do not create implicit tbody elements. Therefore, the above selector would still fail in this context. You would need to explicitly add a tbody element or use a different approach.
The above is the detailed content of Why Doesn't My CSS Child Selector Work on Deeply Nested Elements?. For more information, please follow other related articles on the PHP Chinese website!