Skip to content

Unexpected behaviour selecting <picture> element #3818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MelvinIdema opened this issue Apr 10, 2019 · 2 comments
Closed

Unexpected behaviour selecting <picture> element #3818

MelvinIdema opened this issue Apr 10, 2019 · 2 comments

Comments

@MelvinIdema
Copy link

MelvinIdema commented Apr 10, 2019

Issue

The <picture> element is doing nothing, only providing context for the browser. So the expected behaviour should be that it's being treated as an img element. This is the case using the Descendant Selector. But as soon as a selector other than the Descendant Selector is introduced this is no longer the case and CSS won't select the picture anymore.

Examples

1. Descendant Selector
A <picture> with a <div> as parent and a Descendant Selector:
https://codepen.io/melvinidema/pen/WWpBjp

2. Selector other than Descendant
A <picture> with a <div> as parent and a selector other Descendant Selector:
https://codepen.io/melvinidema/pen/zXZdKw

Expected Behaviour

The expected behaviour would be to have CSS treating the <picture> as a whole as if it was an <img>. As the <picture> and the <source> are only providing context. This is already the case using the Descendant Selector [space] as seen in the first example.

@AmeliaBR
Copy link
Contributor

For the purposes of CSS, a <picture> element acts the same as a <span>. It isn't the element that draws the image, and by default it doesn't have any styles, but you can style it if you want.

If display: contents had existed when the picture element was added, a picture might have been defined to use that rule, creating a container that isn't drawn at all, just passing through its child contents. But, even if a <picture> had display: contents, and was therefore removed from the CSS layout rendering tree, your selector still would not work.

Selectors operate on the DOM tree, not on the rendering tree. The <img> element is not a direct child of the <div> in your DOM tree, it is a direct child of <picture>. Your <img> element is a descendent of your <div> in the DOM tree, so the descendent combinator works fine.

@MelvinIdema
Copy link
Author

Thank you for the answer @AmeliaBR. That makes a lot of sense. Selectors operating on the DOM tree is a lot more logical than on the rendering tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants