简体   繁体   English

如何使用 Vue 3 和 TailwindCSS 在按钮背景图像顶部设置 Heroicon 图标

[英]How to set Heroicon icon on top of button background image with Vue 3 and TailwindCSS

I am attempting to set a Heroicon icon on top of an button background image in Vue 3 and TailwindCSS.我正在尝试在 Vue 3 和 TailwindCSS 中的按钮背景图像顶部设置一个 Heroicon 图标。 I created a circular button component containing a button with a background image and a slot for Heroicon icons:我创建了一个圆形按钮组件,其中包含一个带有背景图像的按钮和一个用于英雄图标的插槽:

<!--ButtonComponent-->

  <div>
    <button type="button" class="relative rounded-full">
        <img :src="https://image.shutterstock.com/image-illustration/abstract-vector-blue-color-geometric-260nw-167535434.jpg">
        <slot class="absolute"></slot>
    </button>
  </div>

I then imported that button component into another component, then wrapped that button around an imported Heroicon icon:然后我将该按钮组件导入另一个组件,然后将该按钮包裹在导入的 Heroicon 图标周围:

<!--ParentComponent-->

<div>
  <ButtonComponent>
     <VideoCameraIcon />
  </ButtonComponent>
</div>

import { VideoCameraIcon } from '@heroicons/vue/outline'

My goal is to configure the slot handling the Heroicon icon to sit on top of the button background image.我的目标是将处理 Heroicon 图标的插槽配置为位于按钮背景图像的顶部。 To handle this, I added a TailwindCSS relative property to the button, and an absolute property to the slot.为了处理这个问题,我为按钮添加了一个 TailwindCSS 相对属性,并为插槽添加了一个绝对属性。 However, this still doesn't work.但是,这仍然行不通。 How can I go about enabling the slot element to sit on top of image (in order to display the Heroicon icon on the image)?我怎样才能使 slot 元素位于图像顶部(以便在图像上显示 Heroicon 图标)?

slot tag doesn't take class attribute in consideration, you should use the :slotted pseudo-class inside a scoped style: slot标签不考虑class属性,您应该在作用域样式中使用:slotted伪类:

<div>
  <ButtonComponent>
     <VideoCameraIcon class="btn-icon" />
  </ButtonComponent>
</div>
<script >
import { VideoCameraIcon } from '@heroicons/vue/outline'
....
</script>
<style scoped>
:slotted(.btn-icon) {
  @apply absolute
}
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM