Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useStyleTag.md
1---2category: Browser3---45# useStyleTag67Inject reactive `style` element in head.89## Usage1011### Basic usage1213Provide a CSS string, then `useStyleTag` will automatically generate an id and inject it in `<head>`.1415```ts16import { useStyleTag } from '@vueuse/core'1718const {19id,20css,21load,22unload,23isLoaded,24} = useStyleTag('.foo { margin-top: 32px; }')2526// Later you can modify styles27css.value = '.foo { margin-top: 64px; }'28```2930This code will be injected to `<head>`:3132```html33<style id="vueuse_styletag_1">34.foo {35margin-top: 64px;36}37</style>38```3940### Custom ID4142If you need to define your own id, you can pass `id` as first argument.4344```ts45import { useStyleTag } from '@vueuse/core'46// ---cut---47useStyleTag('.foo { margin-top: 32px; }', { id: 'custom-id' })48```4950```html51<!-- injected to <head> -->52<style id="custom-id">53.foo {54margin-top: 32px;55}56</style>57```5859### Media query6061You can pass media attributes as last argument within object.6263```ts64import { useStyleTag } from '@vueuse/core'65// ---cut---66useStyleTag('.foo { margin-top: 32px; }', { media: 'print' })67```6869```html70<!-- injected to <head> -->71<style id="vueuse_styletag_1" media="print">72.foo {73margin-top: 32px;74}75</style>76```7778## Type Declarations7980```ts81export interface UseStyleTagOptions extends ConfigurableDocument {82/**83* Media query for styles to apply84*/85media?: string86/**87* Load the style immediately88*89* @default true90*/91immediate?: boolean92/**93* Manual controls the timing of loading and unloading94*95* @default false96*/97manual?: boolean98/**99* DOM id of the style tag100*101* @default auto-incremented102*/103id?: string104/**105* Nonce value for CSP (Content Security Policy)106*107* @default undefined108*/109nonce?: string110}111export interface UseStyleTagReturn {112id: string113css: ShallowRef<string>114load: () => void115unload: () => void116isLoaded: Readonly<ShallowRef<boolean>>117}118/**119* Inject <style> element in head.120*121* Overload: Omitted id122*123* @see https://vueuse.org/useStyleTag124* @param css125* @param options126*/127export declare function useStyleTag(128css: MaybeRef<string>,129options?: UseStyleTagOptions,130): UseStyleTagReturn131```132