Vue's built-in <component> element is documented primarily as a way to switch between Vue components on demand, but both the Vue 2 and Vue 3 docs carry a brief note that it also works with plain HTML elements. That lesser-known aspect lets a single component render as different tags based on props, replacing chains of v-if and v-else in the template with a small computed property in the script block — where such logic is easier to read and maintain.

Buttons and links are frequently used interchangeably, yet they differ in semantics, behavior and default visuals: a <button> triggers an action in the current view via JavaScript, while an <a> points to another resource. Since links are commonly styled to look like buttons — Bootstrap's .btn class being one example — a single component can serve both roles, defaulting to a button and rendering a link when an href prop is present.

<component
  :is="element"
  :href="href"
  class="my-button"
>
  <slot />
</component>

The bound is attribute targets a computed method called element, while href is bound directly to the prop of the same name. This relies on Vue's normal behavior of omitting a bound attribute from the rendered HTML when its value is empty. The slot supplies the inner content whether the result is a button or a link.

element () {
  return this.href ? 'a' : 'button';
}

If the href prop exists, an <a> is used; otherwise the component returns a <button>.

<my-button>this is a button</my-button>
<my-button href="https://www.css-tricks.com">this is a link</my-button>

The rendered output:

<button class="my-button">this is a button</button>
<a href="https://www.css-tricks.com" class="my-button">this is a link</a>

The two elements need not look alike. Styles can target the element through a selector such as div.my-button, or a dynamic class can be swapped along with the element.

Switching between list types

The same pattern works for lists. Because ordered and unordered lists share the same <li> children, only the wrapper changes; a description list (<dl>) is equally feasible, since the slot simply accepts <li> elements or <dt>/<dd> combinations.

<component
  :is="element"
  class="my-list"
>
  <slot>No list items!</slot>
</component>

The template includes default slot content, which the computed property takes into account. A prop selects the list type, defaulting to <ul>:

props: {
  listType: {
    type: String,
    default: 'ul'
  }
}
element () {
  if (this.$slots.default) {
    return this.listType;
  } else {
    return 'div';
  }
}

Here the computed checks whether the default slot has content. If it does, the listType prop determines the element; if not, the element becomes a <div> carrying the "No list items!" message from the slot. This avoids emitting a list with a single item stating that no items exist — a semantics problem that could also confuse accessibility tools into reporting a one-item list.

Each list type can be styled separately through selectors like ul.my-list, or through a class that changes with the element. Below is a BEM-like naming structure:

<component
  :is="element"
  class="my-list"
  :class="`my-list__${element}`"
>
  <slot>No list items!</slot>
</component>

Usage mirrors the button example:

<my-list>
  <li>list item 1</li>
</my-list>

<my-list list-type="ol">
  <li>list item 1</li>
</my-list>

<my-list list-type="dl">
  <dt>Item 1</dt>
  <dd>This is item one.</dd>
</my-list>

<my-list></my-list>

Every instance renders its requested list element; the last one produces a <div> saying there are no list items, because no list is shown.

A component that bundles list styling and markup aids maintainability, and it leaves room for behavior tied to the list type — sorting an <ul>, for instance, then switching to an <ol> to display the sort order and back again when finished.

Controlling elements inside a component

The technique is not limited to a component's root element. A heading inside a component might switch from <h2> to <h3> depending on some condition. But when an element needs ternaries or other logic for more than a few attributes, classes or properties, stick with v-if — the extra machinery complicates the code more than the directive does, and simpler code is easier to read and maintain. The <component> approach is worth considering whenever a simple v-if would otherwise be switching between elements.

Case study: a flexible card

A card component that serves three roles in an article layout puts the idea to work at a larger scale:

  • Hero card: sits at the top of the page and draws more attention than the others.
  • Call to action card: a line of user actions before or within the article.
  • Info card: intended for pull quotes.

Each type follows a design system, with the component owning the HTML for semantics and styling.

<component :is="elements('root')" :class="'custom-card custom-card__' + type" @click="rootClickHandler">
  <header class="custom-card__header" :style="bg">
    <component :is="elements('header')" class="custom-card__header-content">
      <slot name="header"></slot>
    </component>
  </header>
  <div class="custom-card__content">
    <slot name="content"></slot>
  </div>
  <footer class="custom-card__footer">
    <component :is="elements('footer')" class="custom-card__footer-content" @click="footerClickHandler">
      <slot name="footer"></slot>
    </component>
  </footer>
</component>

The card template holds three <component> elements, one per section, each changed by card type. Each calls the elements() method with a parameter identifying that section:

elements(which) {
  const tags = {
    hero: { root: 'section', header: 'h1', footer: 'date' },
    cta: { root: 'section', header: 'h2', footer: 'div' },
    info: { root: 'aside', header: 'h3', footer: 'small' }
  }
  return tags[this.type][which];
}

The implementation stores an object mapping HTML tags for each section of each card type, then returns the right tag for the current type and the section passed in. Other approaches may suit other requirements.

For styling, the root element carries a class based on the card type, which makes writing CSS per type straightforward. Targeting the HTML elements directly is possible, but classes are preferable: future changes to the card's HTML structure are less likely to require touching the class logic.

The hero card's header also supports a background image through a computed property named bg:

bg() {
  return this.background ? `background-image: url(${this.background})` : null;
}

When the background prop contains an image URL, the computed returns an inline style string applying it as a background image. The approach is deliberately simple and could be extended to support custom colors, gradients or fallback colors — each card type could also expose its own optional props.

<custom-card type="hero" background="https://picsum.photos/id/237/800/200">
  <template v-slot:header>Article Title</template>
  <template v-slot:content>Lorem ipsum...</template>
  <template v-slot:footer>January 1, 2011</template>
</custom-card>

Every section of the card receives its own slot, and the slots expect text only. The component decides the HTML element purely from the card type, so consumers never make decisions about markup structure — which keeps usage trivial.

<custom-card type="cta">
  <template v-slot:header>CTA Title One</template>
  <template v-slot:content>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</template>
  <template v-slot:footer>footer</template>
</custom-card>

<custom-card type="info">
  <template v-slot:header>Here's a Quote</template>
  <template v-slot:content>“Maecenas ... quis.”</template>
  <template v-slot:footer>who said it</template>
</custom-card>

The same constraint applies to the other two types: slots hold text, and the elements() method generates the markup. If a different element is needed later, only the component changes. Accessibility features and interaction behaviors tied to card type are likewise candidates for future expansion.

A small side effect worth knowing

The oddly named <component> element exists to swap Vue components inside another component — the tab-style example in the Vue documentation shows the basic idea. Its ability to do the same for HTML elements is a side effect, but a useful one: attribute, class and structural logic collapses into computed properties and a single element in the template.