レイヤー
CSS の順序は優先度に影響します。エンジンはルールの順序を保持しますが、明示的に順序を制御するために、一部のユーティリティをグループ化したい場合があります。
使用方法
3 つの固定レイヤー(base
、components
、utilities
)を提供する Tailwind CSS とは異なり、UnoCSS ではレイヤーを自由に定義できます。レイヤーを設定するには、ルール3番目の項目にメタデータを渡すことができます。
ts
rules: [
[/^m-(\d)$/, ([, d]) => ({ margin: `${d / 4}rem` }), { layer: 'utilities' }],
// when you omit the layer, it will be `default`
['btn', { padding: '4px' }],
]
これは以下を生成します
css
/* layer: default */
.btn { padding: 4px; }
/* layer: utilities */
.m-2 { margin: 0.5rem; }
レイヤーは各プリフライトにも設定できます
ts
preflights: [
{
layer: 'my-layer',
getCSS: async () => (await fetch('my-style.css')).text(),
},
]
順序付け
レイヤーの順序は以下によって制御できます
ts
layers: {
'components': -1,
'default': 1,
'utilities': 2,
'my-layer': 3,
}
指定された順序のないレイヤーは、アルファベット順にソートされます。
レイヤー間にカスタム CSS を挿入したい場合は、エントリ モジュールを更新できます。
ts
// 'uno:[layer-name].css'
import 'uno:components.css'
// layers that are not 'components' and 'utilities' will fallback to here
import 'uno.css'
// your own CSS
import './my-custom.css'
// "utilities" layer will have the highest priority
import 'uno:utilities.css'
CSS カスケードレイヤー
以下で CSS カスケードレイヤーを出力できます
ts
outputToCssLayers: true
以下で CSS レイヤー名を変更できます
ts
outputToCssLayers: {
cssLayerName: (layer) => {
// The default layer will be output to the "utilities" CSS layer.
if (layer === 'default')
return 'utilities'
// The shortcuts layer will be output to the "shortcuts" sublayer the of "utilities" CSS layer.
if (layer === 'shortcuts')
return 'utilities.shortcuts'
// All other layers will just use their name as the CSS layer name.
}
}
バリアントを使用するレイヤー
バリアントを使用してレイヤーを作成できます。
uno-layer-<name>:
を使用して UnoCSS レイヤーを作成できます。
html
<p class="uno-layer-my-layer:text-xl">text</p>
/* layer: my-layer */
.uno-layer-my-layer\:text-xl{font-size:1.25rem;line-height:1.75rem;}
layer-<name>:
を使用して CSS @layer を作成できます。
html
<p class="layer-my-layer:text-xl">text</p>
/* layer: default */
@layer my-layer{
.layer-my-layer\:text-xl{font-size:1.25rem;line-height:1.75rem;}
}