# Alpine.js Documentation - Directives
Alpine directives are attributes that you can add to HTML elements to give them special behavior.
## [x-for](/directives/for)
Alpine's `x-for` directive allows you to create DOM elements by iterating through a list. Here's a simple example of using it to create a list of colors based on an array.
```
```
You may also pass objects to `x-for`.
```
```
There are two rules worth noting about `x-for`:
> `x-for` MUST be declared on a `` element.
> That `` element MUST contain only one root element
### [Keys](#keys)
It is important to specify unique keys for each `x-for` iteration if you are going to be re-ordering items. Without dynamic keys, Alpine may have a hard time keeping track of what re-orders and will cause odd side-effects.
```
```
Now if the colors are added, removed, re-ordered, or their "id"s change, Alpine will preserve or destroy the iterated ``elements accordingly.
### [Accessing indexes](#accessing-indexes)
If you need to access the index of each item in the iteration, you can do so using the `([item], [index]) in [items]` syntax like so:
```
```
You can also access the index inside a dynamic `:key` expression.
```
```
### [Iterating over a range](#iterating-over-a-range)
If you need to simply loop `n` number of times, rather than iterate through an array, Alpine offers a short syntax.
```
```
`i` in this case can be named anything you like.
> Despite not being included in the above snippet, `x-for` cannot be used if no parent element has `x-data` defined. [→ Read more about `x-data`](/directives/data)
### [Contents of a ``](#contents-of-a-template)
As mentioned above, an `` tag must contain only one root element.
For example, the following code will not work:
```
nThe next color is
n
```
but this code will work:
```
n
nThe next color is
n
n
```
Code highlighting provided by [Torchlight](https://torchlight.dev/)