{"id":621,"date":"2018-03-12T07:00:00","date_gmt":"2018-03-12T14:00:00","guid":{"rendered":"https:\/\/coreassistance.com\/tips\/?p=621"},"modified":"2018-02-23T12:23:28","modified_gmt":"2018-02-23T19:23:28","slug":"how-to-easily-style-the-current-navigation-element","status":"publish","type":"post","link":"https:\/\/coreassistance.com\/tips\/2018\/03\/12\/how-to-easily-style-the-current-navigation-element\/","title":{"rendered":"How to Easily Style the Current Navigation Element"},"content":{"rendered":"<p>Let&#8217;s say you have a website with some navigation elements:<\/p>\n<pre><code class=\"html\">&lt;nav&gt;\r\n    &lt;ul&gt;\r\n        &lt;li&gt;&lt;a href=\"\/\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\r\n        &lt;li&gt;&lt;a href=\"\/blog\/\"&gt;Blog&lt;\/a&gt;&lt;\/li&gt;\r\n        &lt;li&gt;&lt;a href=\"\/about\/\"&gt;About&lt;\/a&gt;&lt;\/li&gt;\r\n    &lt;\/ul&gt;\r\n&lt;\/nav&gt;<\/code><\/pre>\n<p>Now, imagine you want to make the active navigation element bold.  That is, when you&#8217;re on the home page, the Home link should be bold; when you&#8217;re somewhere inside the blog section the Blog link should be bold, and so on.<\/p>\n<p>You might be thinking about writing some code on the server that spits out a special class for the current section&#8217;s navigation link.  That would work, but it isn&#8217;t going to scale well as more sections are added.<\/p>\n<p>But there&#8217;s a better way!<\/p>\n<p>Let&#8217;s start by adding some classes to the navigation elements:<\/p>\n<pre><code class=\"html\">&lt;nav&gt;\r\n    &lt;ul&gt;\r\n        &lt;li&gt;&lt;a class=\"home\" href=\"\/\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\r\n        &lt;li&gt;&lt;a class=\"blog\" href=\"\/blog\/\"&gt;Blog&lt;\/a&gt;&lt;\/li&gt;\r\n        &lt;li&gt;&lt;a class=\"about\" href=\"\/about\/\"&gt;About&lt;\/a&gt;&lt;\/li&gt;\r\n    &lt;\/ul&gt;\r\n&lt;\/nav&gt;<\/code><\/pre>\n<p>Now, for each section of the site, add a matching class to the <code>body<\/code> element, like this:<\/p>\n<pre><code class=\"html\">&lt;body class=\"home\"&gt;<\/code><\/pre>\n<p>Now, with this CSS, the current section&#8217;s navigation element will key off of the body class:<\/p>\n<pre><code class=\"css\">body.home nav a.home,\r\nbody.blog nav a.blog,\r\nbody.about nav a.about {\r\n    font-weight: bold;\r\n}<\/code><\/pre>\n<p>Nothing dynamic, and no server-side coding required!<\/p>\n<p>If a new section is added to the site all you need to do is add the new navigation link:<\/p>\n<pre><code class=\"html\">&lt;nav&gt;\r\n    &lt;ul&gt;\r\n        &lt;li&gt;&lt;a class=\"home\" href=\"\/\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\r\n        &lt;li&gt;&lt;a class=\"blog\" href=\"\/blog\/\"&gt;Blog&lt;\/a&gt;&lt;\/li&gt;\r\n        &lt;li&gt;&lt;a class=\"about\" href=\"\/about\/\"&gt;About&lt;\/a&gt;&lt;\/li&gt;\r\n        &lt;li&gt;&lt;a class=\"contact\" href=\"\/contact\/\"&gt;Contact&lt;\/a&gt;&lt;\/li&gt;\r\n    &lt;\/ul&gt;\r\n&lt;\/nav&gt;<\/code><\/pre>\n<p>Then add a new selector to the CSS:<\/p>\n<pre><code class=\"css\">body.home nav a.home,\r\nbody.blog nav a.blog,\r\nbody.about nav a.about,\r\nbody.contact nav a.contact {\r\n    font-weight: bold;\r\n}<\/code><\/pre>\n<p>There you go!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This trick makes it easy to give the current section&#8217;s navigation element a unique appearance, and it scales easily as your site grows.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-621","post","type-post","status-publish","format-standard","hentry","category-htmlcss"],"_links":{"self":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":16,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"predecessor-version":[{"id":642,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/621\/revisions\/642"}],"wp:attachment":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}