content icon indicating copy to clipboard operation
content copied to clipboard

Issue with ":target" and Web Components

Open acarlstein opened this issue 4 years ago • 7 comments

MDN URL: https://developer.mozilla.org/en-US/docs/Web/CSS/:target

What information was incorrect, unhelpful, or incomplete?

Let's assume you have this Web Component:


let template = `
	<div>
		<a href="#message">Make the message red</a>
		<h2 id="message">Message in a bottle</h2>
	</div>
	<style>
		#message:target {
			color: red;
		}
	</style>
`;

customElements.define('my-component', class myElement extends HTMLElement {
	constructor() {
		super();
		let shadowRoot = this.attachShadow({ mode: 'open' });
		shadowRoot.innerHTML = template;
	}
});

This code fails to work because the event :target is missing in the root of the shadow.

Specific section or headline?

What did you expect to see?

I expected that :target would work inside the web components. I expect the message to run red when clicking on the link.

Did you test this? If so, how?

MDN Content page report details
  • Folder: en-us/web/css/_colon_target
  • MDN URL: https://developer.mozilla.org/en-US/docs/Web/CSS/:target
  • GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/css/_colon_target/index.html
  • Last commit: https://github.com/mdn/content/commit/f4fb4c2d90a134366c280eec6cb599825981ad5d
  • Document last modified: 2021-03-15T17:55:56.000Z

acarlstein avatar Apr 29 '21 19:04 acarlstein

Can i take up this issue and start working on it?

Manthanjain avatar Jul 26 '21 18:07 Manthanjain

Can i take up this issue and start working on it?

Ratnadeepdeyroy avatar Oct 31 '21 16:10 Ratnadeepdeyroy

Absolutely! Assigned to @Ratnadeepdeyroy .

[Sorry I missed your original request @Manthanjain - assigning to Ratnadeepdeyroy because I assume you have moved on to other things]

hamishwillee avatar Oct 31 '21 22:10 hamishwillee

@hamishwillee Anyone is working on this issue? If not, can i take this up?

sidverma32 avatar Nov 20 '21 05:11 sidverma32

@sidverma32 Yes thanks, assigned to you. First thing to do will be to check that it hasn't already been fixed. It may be that @Ratnadeepdeyroy did fix this but did not close this issue.

@Ratnadeepdeyroy Unassigned from you.

hamishwillee avatar Nov 21 '21 23:11 hamishwillee

I think this issue is very much the intended behaviour. In general, id references work within each tree, not across shadow boundaries. For example, document.getElementsById(id) will only find elements with the specified id in the document tree, not any of the shadow trees connected to it.

The example which you showcased @acarlstein is a new feature request.

So this issue is not related to this MDN Web CSS Doc en-us/web/css/_colon_target

sidverma32 avatar Nov 28 '21 12:11 sidverma32

@sidverma32 thank you for taking a look into this issue. I am fine if this becomes a feature. I'm glad you folks are on top of this development and I appreciate the effort.

I would like to make it clear for anyone who might be reading this.

The HTML code, inside the template variable, is what "creates and manipulates" this particular shadow tree of this custom web component myElement.

This part of the code "creates" the shadow tree:

	<div>
		<a href="#message">Make the message red</a>
		<h2 id="message">Message in a bottle</h2>
	</div>

This part of the code tries to "manipulate" the element inside this particular shadow tree:

	<style>
		#message:target {
			color: red;
		}
	</style>

This code works fine inside the context of the document tree; however, it fails to work inside the context of the shadow tree.

If the manipulation using the pseudo-class :target fails inside the shadow tree, it might be going against the current public documentation that explains what pseudo-class does:

  • https://www.w3.org/wiki/CSS/Selectors/pseudo-classes/:target
  • https://www.w3.org/TR/selectors-3/#target-pseudo
  • https://developer.mozilla.org/en-US/docs/Web/CSS/:target
  • https://www.w3schools.com/cssref/sel_target.asp

Nothing here says that shadow trees are an exception when trying to use this pseudo-class.

Again, thank you for taking a look into this issue.

acarlstein avatar Nov 29 '21 02:11 acarlstein

Worth reading https://discourse.wicg.io/t/target-css-does-not-work-because-shadowroot-does-not-set-a-target-element/2070

Josh-Cena avatar Jan 08 '23 19:01 Josh-Cena