lit / lit/lit.dev

[docs] No search results for `How can we apply CSS styles to nested elements inside the slot`

Open
#1,384 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
142
Forks
230
PR merge metrics
No merged PRs in 30d

Description

Hi Team,

Could you please help us in applying the CSS to nested elements inside the slot.

Eg:

Render me in a slot

How can we apply the css to span with class redText inside a P tag which is a slot. It would be helpful if you can share some examples. I have already gone through your workarounds, but couldn't execute it.

Workaround 1 - Use Javascript To Style

(1) Access the slotted elements from javascript by using @queryAssignedElements (See https://lit.dev/docs/components/shadow-dom/#query-assigned-nodes)

(2) For each slotted element, parse its dom tree and style the child nodes using javascript (e.g. childelement.style.backgroundColor = '#ff0';)

Workaround 2 - Copy Slotted HTML to Another Div

(1) Access the slotted elements from javascript by using @queryAssignedElements (See https://lit.dev/docs/components/shadow-dom/#query-assigned-nodes)

(2) For every slotted element under consideration, copy-paste its outerHTML to another empty div, placed next to it.

(3) Since these initially empty divs are not from the slots, you can setup static css rules for them.

(4) Empty the slots after the copy pasting into the adjacent divs is complete.

Regards
Rajesh

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Shadow DOM documentation and review the reported slot example and the two listed workarounds. Determine whether the documentation needs a focused example for styling nested assigned content; done means the relevant guidance and example are added to lit.dev and address the reported question.

Written by the indexing model from the issue text.

Assessment

Tech stack
css
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.