{"id":6119,"date":"2022-05-31T16:11:16","date_gmt":"2022-05-31T20:11:16","guid":{"rendered":"https:\/\/themespress.ca\/?p=6119"},"modified":"2022-08-23T07:45:35","modified_gmt":"2022-08-23T11:45:35","slug":"configuring-anchors-sticky-or-fixed","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/configuring-anchors-sticky-or-fixed\/","title":{"rendered":"Configuring Anchors: Sticky or Fixed"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will show you how to configure anchors at the perfect height with either position sticky or fixed.<\/p>\n<p>We found various ways to pull this off with CSS but we also found additional coding information on the web.<\/p>\n<p>The first thing required is that your fixed or sticky header needs to be measured in pixels to get an approximate height of your header.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Result of anchors with a fixed header.<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We can clearly see in our example that the title is in the anchor. Logically, seeing as my ID is above it, we should land above the title.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n\n\t<div  class=\"wpb_single_image wpb_content_element vc_align_left wpb_content_element\">\n\t\t\n\t\t<figure class=\"wpb_wrapper vc_figure\">\n\t\t\t<div class=\"vc_single_image-wrapper   vc_box_border_grey\"><img decoding=\"async\" width=\"1903\" height=\"955\" src=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01.png\" class=\"vc_single_image-img attachment-full\" alt=\"Param\u00e9trer des anchors \u2013 Ent\u00eate sticky ou fixed\" title=\"Screenshot 2022-05-31 at 15-51-01\" srcset=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01.png 1903w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01-600x301.png 600w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01-300x151.png 300w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01-1024x514.png 1024w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01-768x385.png 768w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01-1536x771.png 1536w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-15-51-01-1320x662.png 1320w\" sizes=\"(max-width: 1903px) 100vw, 1903px\" \/><\/div>\n\t\t<\/figure>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >CSS for your sticky or fixed header<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>You need to make sure to add the following CSS value: top:0. It&#8217;s this value that will position perfectly your anchor at the right spot.<\/p>\n<p>The problem with headers configured with a sticky or fixed position is that the anchor doesn&#8217;t always land where you want it to be given there&#8217;s a gap caused by the pixel height of the header.<\/p>\n<p>Here our HTML body needs to have the sticky CSS while the header needs position and top.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-3089063\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-3089063\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"css\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">body.stiky header {\n  position: sticky;\n  background: #fff;\n  z-index: 3;\n  width: 100%;\n  left: 0;\n  top: 0;\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We then found this helpful code which will fix the gap issue.<\/p>\n<p><strong>In our example, the height of our header is 215px.<\/strong><br \/>\nOFFSET_HEIGHT_PX: 215,<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading vc_custom_1654028270519\" >Javascript to add in the footer<\/h2><div id=\"ts-enlighterjs-container-5657102\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-5657102\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"javascript\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;script&gt;\n(function(document, history, location) {\n  var HISTORY_SUPPORT = !!(history &amp;&amp; history.pushState);\n\n  var anchorScrolls = {\n    ANCHOR_REGEX: \/^#[^ ]+$\/,\n    OFFSET_HEIGHT_PX: 215,\n\n    \/**\n     * Establish events, and fix initial scroll position if a hash is provided.\n     *\/\n    init: function() {\n      this.scrollToCurrent();\n      $(window).on(&#039;hashchange&#039;, $.proxy(this, &#039;scrollToCurrent&#039;));\n      $(&#039;body&#039;).on(&#039;click&#039;, &#039;a&#039;, $.proxy(this, &#039;delegateAnchors&#039;));\n    },\n\n    \/**\n     * Return the offset amount to deduct from the normal scroll position.\n     * Modify as appropriate to allow for dynamic calculations\n     *\/\n    getFixedOffset: function() {\n      return this.OFFSET_HEIGHT_PX;\n    },\n\n    \/**\n     * If the provided href is an anchor which resolves to an element on the\n     * page, scroll to it.\n     * @param  {String} href\n     * @return {Boolean} - Was the href an anchor.\n     *\/\n    scrollIfAnchor: function(href, pushToHistory) {\n      var match, anchorOffset;\n\n      if(!this.ANCHOR_REGEX.test(href)) {\n        return false;\n      }\n\n      match = document.getElementById(href.slice(1));\n\n      if(match) {\n        anchorOffset = $(match).offset().top - this.getFixedOffset();\n        $(&#039;html, body&#039;).animate({ scrollTop: anchorOffset});\n\n        \/\/ Add the state to history as-per normal anchor links\n        if(HISTORY_SUPPORT &amp;&amp; pushToHistory) {\n          history.pushState({}, document.title, location.pathname + href);\n        }\n      }\n\n      return !!match;\n    },\n    \n    \/**\n     * Attempt to scroll to the current location&#039;s hash.\n     *\/\n    scrollToCurrent: function(e) { \n      if(this.scrollIfAnchor(window.location.hash) &amp;&amp; e) {\n      \te.preventDefault();\n      }\n    },\n\n    \/**\n     * If the click event&#039;s target was an anchor, fix the scroll position.\n     *\/\n    delegateAnchors: function(e) {\n      var elem = e.target;\n\n      if(this.scrollIfAnchor(elem.getAttribute(&#039;href&#039;), true)) {\n        e.preventDefault();\n      }\n    }\n  };\n\n\t$(document).ready($.proxy(anchorScrolls, &#039;init&#039;));\n})(window.document, window.history, window.location);\n&lt;\/script&gt;<\/pre><\/div><h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading vc_custom_1654028345080\" >Final result with the anchor<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We see that the anchor is positioned exactly where the ID is, i.e. above the title.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n\n\t<div  class=\"wpb_single_image wpb_content_element vc_align_left wpb_content_element\">\n\t\t\n\t\t<figure class=\"wpb_wrapper vc_figure\">\n\t\t\t<div class=\"vc_single_image-wrapper   vc_box_border_grey\"><img decoding=\"async\" width=\"1903\" height=\"955\" src=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code.png\" class=\"vc_single_image-img attachment-full\" alt=\"Avec le code\" title=\"Screenshot 2022-05-31 at 16-02-26 - Avec le code\" srcset=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code.png 1903w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code-600x301.png 600w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code-300x151.png 300w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code-1024x514.png 1024w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code-768x385.png 768w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code-1536x771.png 1536w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-31-at-16-02-26-Avec-le-code-1320x662.png 1320w\" sizes=\"(max-width: 1903px) 100vw, 1903px\" \/><\/div>\n\t\t<\/figure>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading vc_custom_1654028555007\" >Slide effect on the anchor<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>You can also add a slide effect that drops the ID towards the anchor.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-3201200\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-3201200\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"css\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">html {scroll-behavior: smooth;}<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will show you how to configure anchors at the perfect height with either position sticky or fixed. We found various ways to pull this off with<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/configuring-anchors-sticky-or-fixed\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":6010,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[],"class_list":["post-6119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6119"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6119\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/6010"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}