Answers.Body: <p>Assuming you use a structure called <code>pages</code> to create your navigation and the <code>entry</code> variable represents the currently viewed entry, you can output  your navigation and active state as follows:</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;ul&gt;
  {% nav page in craft.entries.section('pages') %}
      &lt;li class="{{ page.id == entry.id ? 'active' }}"&gt;
          {{ page.link }}
          {% ifchildren %}
              &lt;ul&gt;
                  {% children %}
              &lt;/ul&gt;
          {% endifchildren %}
      &lt;/li&gt;
  {% endnav %}
  &lt;/ul&gt;
  </code></pre>
  
  <p>See the <a href="http://buildwithcraft.com/docs/templating/tags#nav">nav tag</a> for more details.</p>
  
  <p>If, instead of a structure, you are using a channel called <code>pages</code> for the navigation then the <code>nav</code> tag method above will still work but the following code is leaner and simpler to understand:</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;ul&gt;
  {% for page in craft.entries.section('pages') %}
      &lt;li class="{{ page.id == entry.id ? 'active' }}"&gt;
          {{ page.link }}
      &lt;/li&gt;
  {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-28T15:03:29.730Z
Answers.Text: Thanks Ben, I hadn't even looked into the nav tag, this only works for a structured section though, right? Still very useful, thank you.
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T09:01:10.473Z
Answers.Text: it should work for a channel too, i updated my answer above with code for a channel though.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-22T07:51:57.527Z
Answers.Text: If using this with a dedicated menu section, this shouldn't work? Because nav items in this case are pages too and the ids of a nav item and a page corresponding to it are always different.
Answers.UserDisplayName: certainlyakey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T16:19:17.587Z
Answers.LastActivityDate: 2014-06-30T20:25:43.153Z
Answers.LastEditDate: 2014-06-30T20:25:43.153Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <p><a href="http://buildwithcraft.com/docs/templating/craft.request"><strong>craft.request</strong></a> is useful for setting an "active" class based on URL segments. </p>
  
  <p>If your URL was: <code>domain.com/sale/products/shirts</code></p>
  
  <p>You could write conditionals like the following to target each segment:</p>
  
  <pre><code>{% if craft.request.firstSegment == "sale" %}class="active"{% endif %}
  {% if craft.request.segment(2) == "products" %}class="active"{% endif %}
  {% if craft.request.lastSegment == "shirts" %}class="active"{% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-28T18:18:18.900Z
Answers.Text: Thanks Anna, I was thinking down this route, great minds :) thank you for your answer - have you seen Victor's suggestion using a macro with your code, looks  like a nice we to keep things DRY.
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T18:31:08.447Z
Answers.LastActivityDate: 2014-06-27T18:31:08.447Z
Answers.UserDisplayName: Anna_MediaGirl
Answers.Body: <p>Barrel Strength also have a URL segment based plugin called <a href="http://sprout.barrelstrengthdesign.com/craft-plugins/active">Sprout Active</a>, with <a href="http://sprout.barrelstrengthdesign.com/craft-plugins/active/docs/getting-started/introduction">docs/examples here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-08T22:25:20.690Z
Answers.Text: Sprout Active is great! I think the best solution for this.
Answers.UserDisplayName: Johannes Lamers
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T14:46:04.213Z
Answers.LastActivityDate: 2014-06-27T20:43:03.673Z
Answers.LastEditDate: 2014-06-27T20:43:03.673Z
Answers.UserDisplayName: Matt Stein
Answers.Body: <p>Here's yet another way you can do it (in some circumstances) and it's nice and DRY:</p>
  
  <p>Craft 2</p>
  
  <pre><code>{% if craft.request.getPath() ==  entry.uri %}
  </code></pre>
  
  <p>Craft 3</p>
  
  <pre><code>{% if craft.app.request.pathInfo ==  entry.uri %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-05T16:17:55.587Z
Answers.Text: That doesn't seem tp work on the index page because entry.uri on the homepage is `__home__`
Answers.UserDisplayName: KSPR
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-06-29T23:27:11.750Z
Answers.LastActivityDate: 2018-11-19T19:53:11.730Z
Answers.LastEditDate: 2018-11-19T19:53:11.730Z
Answers.UserDisplayName: Simon Kuran
Answers.Body: <p>In your template you can set a var in twig:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set active = 'blog' %}
  </code></pre>
  
  <p>And then in your navigation you can run an if statement to see what the var is</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;nav&gt;
      &lt;a {% if active = 'homepage' %}class="active"{% endif %}&gt; Home &lt;/a&gt;
      &lt;a {% if active = 'blog' %} class="active"{% endif %}&gt; Blog &lt;/a&gt;
  &lt;/nav&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-28T15:07:03.907Z
Answers.Text: Thank you Rob, this looks as if it works great until you re-use a template. Then I guess you would need to use a craft.request to check the segment variable?
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T14:36:51.050Z
Answers.Text: Yeah you are probably right. For simple sites using Craft Personal you can get away with my above answer. However if you're doing something much larger, you might want to check out @Anna_MediaGirl's answer.
Answers.UserDisplayName: Rob Erskine
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T13:45:00.397Z
Answers.LastActivityDate: 2014-06-27T13:55:58.917Z
Answers.LastEditDate: 2014-06-27T13:55:58.917Z
Answers.UserDisplayName: Rob Erskine
Answers.Body: <p>Another way to achieve this:</p>
  
  <pre><code>    {% for entry in entries %}
        {% set active = craft.request.path ~ '/' matches "|^#{entry.uri}/|" %}
        &lt;a href="{{ entry.url }}" class="{{ active ? 'is-active' : '' }}"&gt;{{ entry.title }}&lt;/a&gt;
      {% endfor %}
  </code></pre>
  
  <p>This accounts for even when a navigation item might not be on the top level, but you want every URI that starts with that to trigger the active class.</p>
  
  <p>Note: the pipes around the regex are just the encloser. They can be anything, but since we're dealing with URLs, it's easier not to use the standard <code>/</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-29T05:17:02.867Z
Answers.LastActivityDate: 2014-08-29T05:42:38.263Z
Answers.LastEditDate: 2014-08-29T05:42:38.263Z
Answers.UserDisplayName: Tim Kelty
Answers.Body: <p>Here's one more for this nice collection!</p>
  
  <p>This is what I use for a structure "pages" where I needed some way to not only add a class <code>active</code> to the current entry, but also to all of the entry's ancestors. In an Example URL <code>http://example.com/beers/wheat-beers/</code> both links <code>beers</code> and <code>wheat-beers</code> should be given the <code>active</code>class.</p>
  
  <p>The macro takes the structure entry's <a href="http://buildwithcraft.com/docs/templating/entrymodel#uri" rel="noreferrer"><code>uri</code></a> as one parameter. The other parameter is needed to specify the part of the current URL, the entry's URI gets compared to (→  <a href="http://buildwithcraft.com/docs/templating/craft.request#getSegment" rel="noreferrer"><code>craft.request.getSegment()</code></a>).</p>
  
  <p>Give this parameter a value based on the entry's <a href="http://buildwithcraft.com/docs/templating/entrymodel#level" rel="noreferrer"><code>level</code></a> within its structure.</p>
  
  <p>.</p>
  
  <p><em>_macros.html:</em></p>
  
  <pre><code>{% macro activeLinkClass(linkUri, requestSegmentNumber) %}
  
      {# Request the relevant path segment in the URL #}
      {% set nthRequestSegment = craft.request.segment(requestSegmentNumber) %}
  
      {# Split the link's URI into a list of path segments #}
      {% set linkSegments = linkUri|split('/') %}
  
      {# Get the path segment relevant for our comparison #}
      {% set nthLinkSegment = linkSegments[requestSegmentNumber - 1] %}
  
      {# Return `active` if the nthLinkSegment equals the nthRequestSegment #}
      {% if nthLinkSegment == nthRequestSegment %}active{% endif %}
  
  {% endmacro %}
  </code></pre>
  
  <p>.</p>
  
  <p><em>_navigation.html:</em></p>
  
  <pre><code>{% import '_macros.html' as macros %}
  
  {# Get entries from pages structure (exclude home page)  #}
  {% set pages = craft.entries.section('pages').id('not 1') %}
  
  {# Loop through entries and list the links #}
  {% nav page in pages %}
  
      {# Add a class `active` if needed, using our macro #}
      &lt;a href="{{ page.url }}" class="nav-link{{ macros.activeLinkClass(page.uri, page.level) }}"&gt;
          {{ page.title }}
      &lt;/a&gt;
  
      {% ifchildren %}{% children %}{% endifchildren %}
  
  {% endnav %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-29T09:07:32.453Z
Answers.LastActivityDate: 2014-08-29T10:00:45.757Z
Answers.LastEditDate: 2014-08-29T10:00:45.757Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>I add a variable in my "_entry" template. </p>
  
  <p>Here's a basic example:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% extends "_layout" %}
  
  {% set bodyClass = 'myPageType' %}
  
  {% block content %}
  &lt;!-- entry HTML here --&gt;
  {% endblock %}
  </code></pre>
  
  <p>Then in my _layout template, it begins like this:</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;!doctype html&gt;
  &lt;html class="{% if bodyClass is defined %}{{ bodyClass }}{% endif %}"&gt;
  &lt;!-- the rest of the template follows --&gt;
  </code></pre>
  
  <p>Does something like that work for you?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-28T18:20:12.670Z
Answers.Text: Hey, John there are a few different options now, this page has become a great resource, thank you for your answer.
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T13:41:57.980Z
Answers.LastActivityDate: 2014-06-27T13:56:27.253Z
Answers.LastEditDate: 2014-06-27T13:56:27.253Z
Answers.UserDisplayName: John Morton
Answers.Body: <p>I think comparing the entry id's is the best way to go for the current page. But for determining if a certain page is a parent of the current page, I combined answers from @tim-kelty and @carlcs to come up with this:</p>
  
  <pre><code>{% for entry in entries %}
    {% set isInSection = craft.request.firstSegment == entry.uri|split('/')[0] %}
    &lt;a href="{{ entry.url }}" class="{{ isInSection ? 'active' : '' }}"&gt;{{ entry.title }}&lt;/a&gt;
  {% endfor %}
  </code></pre>
  
  <p>EDIT: I realized my above code does something slightly different than I originally thought: it will add the 'active' class to any items that are in the current' page's section... which includes siblings. If that's what you want, then it's great. But if you only want to indicate the actual top-level section parent page, then you should do this instead:</p>
  
  <pre><code>{% for entry in entries %}
    {% set isTopParent = craft.request.firstSegment == entry.uri %}
    &lt;a href="{{ entry.url }}" class="{{ isTopParent ? 'active' : '' }}"&gt;{{ entry.title }}&lt;/a&gt;
  {% endfor %}
  </code></pre>
  
  <p>Last but not least, if you want to hilite all pages that are in the parent "path" of the current page (which is what I most often need in my menus), you can do this:</p>
  
  <pre><code>{# use different var name in the loop because we assume `entry` is already set to the currently-viewed page #}
  {% for page in entries %}
    {% set isInPath = entry.uri matches '{^' ~ page.uri ~ '}' %}
    &lt;a href="{{ page.url }}" class="{{ isInPath ? 'active' : '' }}"&gt;{{ page.title }}&lt;/a&gt;
  {% endfor %}
  </code></pre>
  
  <p>I've posted a full example that encompasses a lot of "denote the active page or its parents" logic here: <a href="http://craftcookbook.net/recipes/388" rel="nofollow">http://craftcookbook.net/recipes/388</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-04T05:47:55.070Z
Answers.LastActivityDate: 2015-08-05T17:45:17.493Z
Answers.LastEditDate: 2015-08-05T17:45:17.493Z
Answers.UserDisplayName: Jordan Lev
Answers.Body: <p>I know this post is old, but I'd like to share another technique I've been using for the past year. If you need a "hard-coded" navigation with no editing capabilities in the admin but still want dynamic information from the section ID number.</p>
  
  <p>If you use multilingual sites, this is super practical because it will get the correct language page title, URL and check if it's currently active.</p>
  
  <pre><code>&lt;li&gt;                    
      {% set homepage = craft.entries.id(13).one() %}
      &lt;a href="{{ homepage.getUrl }}" {% if craft.app.request.absoluteUrl == homepage.url %} class="active"{% endif %}&gt;{{ homepage.title }}&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;                    
      {% set about = craft.entries.id(23).one() %}
      &lt;a href="{{ about.getUrl }}" {% if craft.app.request.absoluteUrl == about.url %} class="active"{% endif %}&gt;{{ about.title }}&lt;/a&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>As you can see in the example above I get 2 pages for my navigation, home and about pages.</p>
  
  <p>Hopefully this helps out someone in the near future.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-17T17:28:05.883Z
Answers.LastActivityDate: 2018-11-17T17:28:05.883Z
Answers.UserDisplayName: mateostabio
Answers.Body: <p><strong>Tip:</strong> To keep it short and reusable I would recommend that you wrap your preferred "check-method" in a Twig macro, I like <a href="https://craftcms.stackexchange.com/a/572/177">Anna's</a> version the most:</p>
  
  <p><strong>_macros.html:</strong></p>
  
  <pre class="lang-twig prettyprint-override"><code>{% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %}
  </code></pre>
  
  <p><strong>layout.html:</strong></p>
  
  <pre class="lang-twig prettyprint-override"><code>{% import "_macros.html" as macros %}
  
  &lt;a href="{{ url('news') }}" class="{{ macros.is_active('news') }}"&gt;News&lt;/a&gt;
  &lt;a href="{{ url('blog') }}" class="{{ macros.is_active('blog') }}"&gt;Blog&lt;/a&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-15T12:17:34.573Z
Answers.Text: How would you test for pluralisation? So ('projects' || 'project') would both return an 'active' class?
Answers.UserDisplayName: Dan Christian
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T18:44:11.267Z
Answers.LastActivityDate: 2014-06-29T16:19:04.923Z
Answers.LastEditDate: 2017-04-13T12:51:00.200Z
Answers.UserDisplayName: Victor
Body: <p>Just wondering how people approach setting a class of current in their navigation. Is there anything in Craft or Twig where you can set a conditional to check if a page is the current page. Or do you just need to set classes/ids on the body tag based on the segment?</p>
  
  <p>Any ideas appreciated, Gareth</p>
  
Tags: <conditional><navigation>
Title: How can I add a dynamic "active" CSS class to the navigation on any given page?
ThreadText: # How can I add a dynamic "active" CSS class to the navigation on any given page?
  
  (Asked by: username_0 on 2014-06-27)
  
  Just wondering how people approach setting a class of current in their navigation. Is there anything in Craft or Twig where you can set a conditional to check if a page is the current page. Or do you just need to set classes/ids on the body tag based on the segment?
  
  Any ideas appreciated, username_0
  
  **Tags:** conditional, navigation
  
  
  ---
  
  ## 11 answers
  
  ### Answer by username_14 on 2014-06-27. Score: 34 | Accepted answer
  
  **Tip:** To keep it short and reusable I would recommend that you wrap your preferred "check-method" in a Twig macro, I like [Anna's](https://craftcms.stackexchange.com/a/572/177) version the most:
  
  **\_macros.html:**
  
  ```twig
  {% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %}
  
  ```
  
  **layout.html:**
  
  ```twig
  {% import "_macros.html" as macros %}
  
  <a href="{{ url('news') }}" class="{{ macros.is_active('news') }}">News</a>
  <a href="{{ url('blog') }}" class="{{ macros.is_active('blog') }}">Blog</a>
  
  ```
  
  > **Comments:**
  > **username_15**: How would you test for pluralisation? So ('projects' || 'project') would both return an 'active' class?  
  
  ---
  
  ### Answer by username_1 on 2014-06-27. Score: 32
  
  Assuming you use a structure called `pages` to create your navigation and the `entry` variable represents the currently viewed entry, you can output your navigation and active state as follows:
  
  ```twig
  <ul>
  {% nav page in craft.entries.section('pages') %}
      <li class="{{ page.id == entry.id ? 'active' }}">
          {{ page.link }}
          {% ifchildren %}
              <ul>
                  {% children %}
              </ul>
          {% endifchildren %}
      </li>
  {% endnav %}
  </ul>
  
  ```
  
  See the [nav tag](http://buildwithcraft.com/docs/templating/tags#nav) for more details.
  
  If, instead of a structure, you are using a channel called `pages` for the navigation then the `nav` tag method above will still work but the following code is leaner and simpler to understand:
  
  ```twig
  <ul>
  {% for page in craft.entries.section('pages') %}
      <li class="{{ page.id == entry.id ? 'active' }}">
          {{ page.link }}
      </li>
  {% endfor %}
  </ul>
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks Ben, I hadn't even looked into the nav tag, this only works for a structured section though, right? Still very useful, thank you.  
  > **username_1**: it should work for a channel too, i updated my answer above with code for a channel though.  
  > **username_2**: If using this with a dedicated menu section, this shouldn't work? Because nav items in this case are pages too and the ids of a nav item and a page corresponding to it are always different.  
  
  ---
  
  ### Answer by username_3 on 2014-06-27. Score: 15
  
  [**craft.request**](http://buildwithcraft.com/docs/templating/craft.request) is useful for setting an "active" class based on URL segments.
  
  If your URL was: `domain.com/sale/products/shirts`
  
  You could write conditionals like the following to target each segment:
  
  ```
  {% if craft.request.firstSegment == "sale" %}class="active"{% endif %}
  {% if craft.request.segment(2) == "products" %}class="active"{% endif %}
  {% if craft.request.lastSegment == "shirts" %}class="active"{% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks Anna, I was thinking down this route, great minds :) thank you for your answer - have you seen username_14's suggestion using a macro with your code, looks  like a nice we to keep things DRY.  
  
  ---
  
  ### Answer by username_4 on 2014-06-27. Score: 13
  
  Barrel Strength also have a URL segment based plugin called [Sprout Active](http://sprout.barrelstrengthdesign.com/craft-plugins/active), with [docs/examples here](http://sprout.barrelstrengthdesign.com/craft-plugins/active/docs/getting-started/introduction).
  
  > **Comments:**
  > **username_5**: Sprout Active is great! I think the best solution for this.  
  
  ---
  
  ### Answer by username_6 on 2014-06-29. Score: 12
  
  Here's yet another way you can do it (in some circumstances) and it's nice and DRY:
  
  Craft 2
  
  ```
  {% if craft.request.getPath() ==  entry.uri %}
  
  ```
  
  Craft 3
  
  ```
  {% if craft.app.request.pathInfo ==  entry.uri %}
  
  ```
  
  > **Comments:**
  > **username_7**: That doesn't seem tp work on the index page because entry.uri on the homepage is `__home__`  
  
  ---
  
  ### Answer by username_8 on 2014-06-27. Score: 11
  
  In your template you can set a var in twig:
  
  ```twig
  {% set active = 'blog' %}
  
  ```
  
  And then in your navigation you can run an if statement to see what the var is
  
  ```twig
  <nav>
      <a {% if active = 'homepage' %}class="active"{% endif %}> Home </a>
      <a {% if active = 'blog' %} class="active"{% endif %}> Blog </a>
  </nav>
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you Rob, this looks as if it works great until you re-use a template. Then I guess you would need to use a craft.request to check the segment variable?  
  > **username_8**: Yeah you are probably right. For simple sites using Craft Personal you can get away with my above answer. However if you're doing something much larger, you might want to check out @username_3's answer.  
  
  ---
  
  ### Answer by username_9 on 2014-08-29. Score: 7
  
  Another way to achieve this:
  
  ```
      {% for entry in entries %}
        {% set active = craft.request.path ~ '/' matches "|^#{entry.uri}/|" %}
        <a href="{{ entry.url }}" class="{{ active ? 'is-active' : '' }}">{{ entry.title }}</a>
      {% endfor %}
  
  ```
  
  This accounts for even when a navigation item might not be on the top level, but you want every URI that starts with that to trigger the active class.
  
  Note: the pipes around the regex are just the encloser. They can be anything, but since we're dealing with URLs, it's easier not to use the standard `/`.
  
  
  ---
  
  ### Answer by username_10 on 2014-08-29. Score: 6
  
  Here's one more for this nice collection!
  
  This is what I use for a structure "pages" where I needed some way to not only add a class `active` to the current entry, but also to all of the entry's ancestors. In an Example URL `http://example.com/beers/wheat-beers/` both links `beers` and `wheat-beers` should be given the `active`class.
  
  The macro takes the structure entry's [`uri`](http://buildwithcraft.com/docs/templating/entrymodel#uri) as one parameter. The other parameter is needed to specify the part of the current URL, the entry's URI gets compared to (→ [`craft.request.getSegment()`](http://buildwithcraft.com/docs/templating/craft.request#getSegment)).
  
  Give this parameter a value based on the entry's [`level`](http://buildwithcraft.com/docs/templating/entrymodel#level) within its structure.
  
  .
  
  *\_macros.html:*
  
  ```
  {% macro activeLinkClass(linkUri, requestSegmentNumber) %}
  
      {# Request the relevant path segment in the URL #}
      {% set nthRequestSegment = craft.request.segment(requestSegmentNumber) %}
  
      {# Split the link's URI into a list of path segments #}
      {% set linkSegments = linkUri|split('/') %}
  
      {# Get the path segment relevant for our comparison #}
      {% set nthLinkSegment = linkSegments[requestSegmentNumber - 1] %}
  
      {# Return `active` if the nthLinkSegment equals the nthRequestSegment #}
      {% if nthLinkSegment == nthRequestSegment %}active{% endif %}
  
  {% endmacro %}
  
  ```
  
  .
  
  *\_navigation.html:*
  
  ```
  {% import '_macros.html' as macros %}
  
  {# Get entries from pages structure (exclude home page)  #}
  {% set pages = craft.entries.section('pages').id('not 1') %}
  
  {# Loop through entries and list the links #}
  {% nav page in pages %}
  
      {# Add a class `active` if needed, using our macro #}
      <a href="{{ page.url }}" class="nav-link{{ macros.activeLinkClass(page.uri, page.level) }}">
          {{ page.title }}
      </a>
  
      {% ifchildren %}{% children %}{% endifchildren %}
  
  {% endnav %}
  
  ```
  
  
  ---
  
  ### Answer by username_11 on 2014-06-27. Score: 6
  
  I add a variable in my "\_entry" template.
  
  Here's a basic example:
  
  ```twig
  {% extends "_layout" %}
  
  {% set bodyClass = 'myPageType' %}
  
  {% block content %}
  <!-- entry HTML here -->
  {% endblock %}
  
  ```
  
  Then in my \_layout template, it begins like this:
  
  ```twig
  <!doctype html>
  <html class="{% if bodyClass is defined %}{{ bodyClass }}{% endif %}">
  <!-- the rest of the template follows -->
  
  ```
  
  Does something like that work for you?
  
  > **Comments:**
  > **username_0**: Hey, username_11 there are a few different options now, this page has become a great resource, thank you for your answer.  
  
  ---
  
  ### Answer by username_12 on 2015-08-04. Score: 4
  
  I think comparing the entry id's is the best way to go for the current page. But for determining if a certain page is a parent of the current page, I combined answers from @tim-username_9 and @username_10 to come up with this:
  
  ```
  {% for entry in entries %}
    {% set isInSection = craft.request.firstSegment == entry.uri|split('/')[0] %}
    <a href="{{ entry.url }}" class="{{ isInSection ? 'active' : '' }}">{{ entry.title }}</a>
  {% endfor %}
  
  ```
  
  EDIT: I realized my above code does something slightly different than I originally thought: it will add the 'active' class to any items that are in the current' page's section... which includes siblings. If that's what you want, then it's great. But if you only want to indicate the actual top-level section parent page, then you should do this instead:
  
  ```
  {% for entry in entries %}
    {% set isTopParent = craft.request.firstSegment == entry.uri %}
    <a href="{{ entry.url }}" class="{{ isTopParent ? 'active' : '' }}">{{ entry.title }}</a>
  {% endfor %}
  
  ```
  
  Last but not least, if you want to hilite all pages that are in the parent "path" of the current page (which is what I most often need in my menus), you can do this:
  
  ```
  {# use different var name in the loop because we assume `entry` is already set to the currently-viewed page #}
  {% for page in entries %}
    {% set isInPath = entry.uri matches '{^' ~ page.uri ~ '}' %}
    <a href="{{ page.url }}" class="{{ isInPath ? 'active' : '' }}">{{ page.title }}</a>
  {% endfor %}
  
  ```
  
  I've posted a full example that encompasses a lot of "denote the active page or its parents" logic here: <http://craftcookbook.net/recipes/388>
  
  
  ---
  
  ### Answer by username_13 on 2018-11-17. Score: 2
  
  I know this post is old, but I'd like to share another technique I've been using for the past year. If you need a "hard-coded" navigation with no editing capabilities in the admin but still want dynamic information from the section ID number.
  
  If you use multilingual sites, this is super practical because it will get the correct language page title, URL and check if it's currently active.
  
  ```
  <li>                    
      {% set homepage = craft.entries.id(13).one() %}
      <a href="{{ homepage.getUrl }}" {% if craft.app.request.absoluteUrl == homepage.url %} class="active"{% endif %}>{{ homepage.title }}</a>
  </li>
  <li>                    
      {% set about = craft.entries.id(23).one() %}
      <a href="{{ about.getUrl }}" {% if craft.app.request.absoluteUrl == about.url %} class="active"{% endif %}>{{ about.title }}</a>
  </li>
  
  ```
  
  As you can see in the example above I get 2 pages for my navigation, home and about pages.
  
  Hopefully this helps out someone in the near future.
  
  
  ---
  
  