---
title: "New Syntax for HTML Tag helpers in Rails 5.1"
description: "Rails 5.1 introduces new shorter syntax for HTML tag helpers"
canonical_url: "https://www.bigbinary.com/blog/new-syntax-for-tag-helpers-in-rails-5-1"
markdown_url: "https://www.bigbinary.com/blog/new-syntax-for-tag-helpers-in-rails-5-1.md"
---

# New Syntax for HTML Tag helpers in Rails 5.1

Rails 5.1 introduces new shorter syntax for HTML tag helpers

- Author: Prathamesh Sonpatki
- Published: August 23, 2017
- Categories: Rails 5.1, Rails

Rails is great at generating HTML using helpers such as
[content_tag](http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag)
and
[tag](http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag).

```erb
content_tag(:div, , class: "home")

<div class="home">
</div>
```

Rails 5.1 has [introduced](https://github.com/rails/rails/issues/25195)
[new syntax](https://github.com/rails/rails/pull/25543) for this in the form of
enhanced `tag` helper.

Now that same HTML div tag can be generated as follows.

```erb
tag.div class: 'home'

<div class="home">
</div>
```

Earlier, the tag type was decided by the positional argument to the
`content_tag` and `tag` methods but now we can just call the required tag type
on the `tag` method itself.

We can pass the tag body and attributes in the block format as well.

```erb
<%= tag.div class: 'home' do %>
  Welcome to Home!
<% end %>


<div class="home">
  Welcome to Home!
</div>
```

### HTML5 compliant by default

The new `tag` helper is also HTML 5 compliant by default, such that it respects
HTML5 features such as
[void elements](https://www.w3.org/TR/html5/syntax.html#void-elements).

### Backward compatibility

The old syntax of `content_tag` and `tag` methods is still supported but might
be deprecated and removed in future versions of Rails.

## Links

- [Human page](https://www.bigbinary.com/blog/new-syntax-for-tag-helpers-in-rails-5-1)
