2023-01-11 15:42:21 +00:00
# Iconify for Tailwind CSS
2023-05-31 19:04:11 +00:00
This plugin creates CSS for over 150k open source icons.
2023-01-11 15:42:21 +00:00
[Browse icons at Iconify ](https://icon-sets.iconify.design/ ) to see all icons.
## Usage
1. Install packages icon sets.
2023-01-12 18:02:54 +00:00
2. In `tailwind.config.js` import `addDynamicIconSelectors` from `@iconify/tailwind` .
2023-01-11 15:42:21 +00:00
## HTML
2023-01-12 18:02:54 +00:00
To use icon in HTML, add class with class name like this: `icon-[mdi-light--home]`
2023-01-11 15:42:21 +00:00
```html
2023-01-12 18:02:54 +00:00
< span class = "icon-[mdi-light--home]" > < / span >
2023-01-11 15:42:21 +00:00
```
2023-01-12 18:02:54 +00:00
Class name has 3 parts:
- Selectot prefix, which can be set in `prefix` option of plugin. Default value is `icon` .
- `-` to tell Tailwind that class name is not complete.
- `[{prefix}--{name}]` for icon name, where `{prefix}` is icon set prefix, `{name}` is icon name.
2023-01-11 15:42:21 +00:00
2023-01-12 18:02:54 +00:00
In Iconify all icon names use the following format: `{prefix}:{name}` . Due to limitations of Tailwind CSS, same format cannot be used with plugin, so instead, prefix and name are separated by double dash: `{prefix}--{name}` .
2023-01-11 15:42:21 +00:00
### Color, size, alignment
2023-05-31 19:04:11 +00:00
Monoton icons can change color! See [Iconify documentation ](https://iconify.design/docs/usage/css/#monotone ) for longer explanation.
2023-01-11 17:51:37 +00:00
2023-01-11 15:42:21 +00:00
To change icon size or color, change font size or text color, like you would with any text.
Icon color cannot be changed for icons with hardcoded palette, such as most emoji sets or flag icons.
To align icon below baseline, add negative vertical alignment, like this (you can also use Tailwind class for that):
```html
2023-01-12 18:02:54 +00:00
< span class = "icon-[mdi--home]" style = "vertical-align: -0.125em" > < / span >
2023-01-11 15:42:21 +00:00
```
## Installing icon sets
Plugin does not include icon sets. You need to install icon sets separately.
2023-05-31 19:04:11 +00:00
To install all 150k+ icons, install `@iconify/json` as a dev dependency.
2023-01-11 15:42:21 +00:00
If you do not want to install big package, install `@iconify-json/` packages for icon sets that you use.
See [Iconify icon sets ](https://icon-sets.iconify.design/ ) for list of available icon sets and icons.
2023-05-31 19:04:11 +00:00
See [Iconify documentation ](https://iconify.design/docs/icons/json.html ) for list of packages.
2023-01-11 15:42:21 +00:00
## Tailwind config
Add this to `tailwind.config.js` :
```js
2023-01-12 18:02:54 +00:00
const { addDynamicIconSelectors } = require('@iconify/tailwind');
2023-01-11 15:42:21 +00:00
```
2023-01-12 18:02:54 +00:00
Then in plugins section add `addDynamicIconSelectors` .
2023-01-11 15:42:21 +00:00
Example:
```js
module.exports = {
content: ['./src/*.html'],
theme: {
extend: {},
},
plugins: [
2023-01-12 18:02:54 +00:00
// Iconify plugin
addDynamicIconSelectors(),
2023-01-11 15:42:21 +00:00
],
presets: [],
};
```
2023-01-12 18:02:54 +00:00
### Options
Plugin accepts options as a second parameter:
2023-01-11 15:42:21 +00:00
2023-01-12 18:02:54 +00:00
- `prefix` is class name prefix. Default value is `icon` . Make sure there is no `-` at the end: it is added in classes, but not in plugin parameter.
- `overrideOnly` : set to `true` to generate rules that override only icon data. See below.
- `files` : list of custom files for icon sets. Key is icon set prefix, value is location of `.json` file with icon set in IconifyJSON format.
- `iconSet` : list of custom icon sets. Key is prefix, value is either icon set data in `IconifyJSON` format or a synchronous callback that returns `IconifyJSON` data.
2023-01-11 15:42:21 +00:00
2023-01-12 18:02:54 +00:00
#### overrideOnly
You can use `overrideOnly` to load some icons without full rules, such as changing icon on hover when main and hover icons are from the same icon set and have same width/height ratio.
2023-01-11 15:42:21 +00:00
2023-01-12 18:02:54 +00:00
Example of config:
2023-01-11 15:42:21 +00:00
2023-01-12 18:02:54 +00:00
```js
plugins: [
// `icon-`
addDynamicIconSelectors(),
// `icon-hover-`
addDynamicIconSelectors({
prefix: "icon-hover",
overrideOnly: true,
}),
],
```
and usage in HTML:
```html
< span class = "icon-[mdi--arrow-left] hover:icon-hover-[mdi--arrow-right]" > < / span >
```
2023-01-11 15:42:21 +00:00
## License
This package is licensed under MIT license.
`SPDX-License-Identifier: MIT`
This license does not apply to icons. Icons are released under different licenses, see each icon set for details.
Icons available by default are all licensed under some kind of open-source or free license.
2023-05-31 19:04:11 +00:00
© 2023-PRESENT Vjacheslav Trushkin