Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
Web Components - Multiple elements per slot

I'm working on new web-components and ran into some issues concerning slots.

I have an outer container-component with a "main-slot" inside, in which multiple elements should be inserted (into the same slot). However, it is only possible to add one element per named slot.

My question: is there a way to add multiple elements to one named slot? Like shown here:

<own-container>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
</own-container>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There is also imperative <slot>

super().attachShadow({
        mode: 'open',
        slotAssignment: 'manual' // imperative assign only
      })

But! you get Named slots OR Imperative slots, on one Web Component

To mimic named <slot>, assigning content to the same named <slot>
you probably need the Mutation Observer API

addendum

You can have multiple elements per slot:

<component-with-slots>
    <H1 slot="title">Web Components are great!</H1>
    <h2 slot="title">A bit hard to master</h2>
    <b slot="title">But Great!</b>
</component-with-slots>

<script>
  customElements.define('component-with-slots', class extends HTMLElement {
    constructor() { 
        super()
          .attachShadow({mode:'open'})
          .innerHTML="<slot name='title'></slot>"; 
    }
  });
</script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Nope. It is not possible for named slot. The trick is to have a wrapper div element to wrap your lightDOM.

<own-container>
  <div slot="main">
    <own-element></own-element>
    <own-element></own-element>
    <own-element></own-element>
    <own-element></own-element>
  </div>
</own-container>

If the presence of additional div causes styling problem, then you can use new contents type of display box.

div {
  display: contents;
}

The display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. However, note that it can cause accessibility issues.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda