---
title: "Float"
description: "Anchors content to an edge or corner of its parent."
url: https://weme-ui.github.io/slim/components/float.md
---
# Float

把内容钉在父级的边上或角上。父级需要 `relative`，否则会贴到更外层。

## Installation

```bash
pnpm dlx @weme-ui/weme-ui add float
```

## Usage

```vue
<script lang="ts" setup>
import Avatar from '../../avatar/avatar.vue'
import Button from '../../button/button.vue'
import Icon from '../../icon/icon.vue'
import Float from '../float.vue'
</script>

<template>
  <div class="flex flex-wrap gap-8 items-center">
    <Avatar>
      <template #extend>
        <Float placement="top-right" offset="2px">
          <Icon name="ri:vip-crown-2-fill" class="text-yellow-10 rotate-40" aria-label="VIP" />
        </Float>
      </template>
    </Avatar>

    <Button label="Click me" color="accent">
      <Float placement="top-right">
        <Icon name="ri:vip-crown-2-fill" class="text-yellow-10 rotate-40" aria-label="VIP" />
      </Float>
    </Button>
  </div>
</template>
```

## Props

| 属性        | 说明                                                                       | 类型                                                                                                                 | 默认值        |
| ----------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | ------------- |
| `as`        | 渲染为的元素或组件（Reka UI `Primitive`）                                  | `AsTag \| Component`                                                                                                 | `'div'`       |
| `asChild`   | 合并到子元素上渲染（Reka UI composition）                                  | `boolean`                                                                                                            | `false`       |
| `placement` | 相对父级的位置                                                             | `'top-left' \| 'top' \| 'top-right' \| 'left' \| 'center' \| 'right' \| 'bottom-left' \| 'bottom' \| 'bottom-right'` | `'top-right'` |
| `offset`    | 两轴共用的偏移。写入 `--offset`，并作为 `--offset-x` / `--offset-y` 的回退 | `string`                                                                                                             | `'0'`         |
| `offsetX`   | 水平偏移，写入 `--offset-x`。未设时用 `offset`                             | `string`                                                                                                             | —             |
| `offsetY`   | 垂直偏移，写入 `--offset-y`。未设时用 `offset`                             | `string`                                                                                                             | —             |
| `class`     | 根节点额外 class                                                           | `any`                                                                                                                | —             |

## Slots

| 插槽      | 说明             |
| --------- | ---------------- |
| `default` | 浮在父级上的内容 |

## Examples

### Placements

```vue
<script lang="ts" setup>
import Float from '../float.vue'

const placements = [
  'top-left',
  'top',
  'top-right',
  'left',
  'center',
  'right',
  'bottom-left',
  'bottom',
  'bottom-right',
] as const
</script>

<template>
  <div class="gap-6 grid grid-cols-3 justify-items-center">
    <div v-for="placement in placements" :key="placement" class="relative">
      <div class="rounded bg-muted size-12" />
      <Float :placement="placement">
        <span class="rounded-full bg-accent size-2.5 ring-2 ring-$docs-well" />
      </Float>
    </div>
  </div>
</template>
```

### Offset

```vue
<script lang="ts" setup>
import Avatar from '../../avatar/avatar.vue'
import Icon from '../../icon/icon.vue'
import Float from '../float.vue'
</script>

<template>
  <div class="flex flex-wrap gap-8 items-center">
    <div class="flex relative">
      <Avatar />
      <Float placement="top-right" offset="0">
        <Icon name="ri:vip-crown-2-fill" class="text-yellow-10 rotate-40" aria-label="VIP" />
      </Float>
    </div>

    <div class="flex relative">
      <Avatar src="https://github.com/moujinet.png" />
      <Float placement="top-right" offset="-2px">
        <Icon
          name="ri:ghost-3-fill"
          class="text-accent animate-bounce"
          aria-label="Ghost behind the avatar"
        />
      </Float>
    </div>
  </div>
</template>
```