Recently, when I was working on my "Football Navigation" website, I encountered a requirement to shift the internal anchor of a web page downward a little after jumping to avoid being covered by the top fixed navigation bar. I searched for some methods online, and most of them used js to control the jump. Later, I found a method that was implemented only with CSS on a foreign developer's personal blog. I thought it was very simple, so I translated his implementation method over. Usually the style of the fixed navigation bar at the top of our web page is implemented as follows: <div class="header" style="position: fixed; top: 0;"></div> Afterwards, there will be a list of a links to jump to: <ul> <li><a href="#section1">Anchor Text</a></li> <li><a href="#section2">Anchor Text</a></li> </ul> The requirement is that when each a link above is clicked, the page is located at the anchor position of the corresponding id: <div class="section" id="section1"></div> <div class="section" id="section2"></div> However, if there is a div with position:fixed at the top, when positioning to this anchor point, the upper part of the content in the anchor point will be covered by the div fixed at the top. The solution is to add an empty page anchor at each positioned content, put the page element ID to be jumped to this empty element, and set the CSS familiarity of the empty element to achieve the offset when jumping. The empty element we define is the div with the class anchor, and the div's id is set to the id that the a link above wants to jump to: <div class="anchor" id="section1"></div> <div class="section"></div> <div class="anchor" id="section2"></div> <div class="section"></div> The CSS properties of this empty element are as follows: .anchor{ display: block; height: 60px; /*same height as top fix*/ margin-top: -60px; /*same height as top fix*/ visibility: hidden; } At this point, we have realized the function of internal anchor point jump that we want. In short, the offset value here is the height occupied by the empty element when jumping. This placeholder element is used to achieve the offset effect we need when the anchor point jumps. This concludes this article about pure CSS to achieve internal anchor point jump up and down offset example code introduction, for more CSS internal anchor point jump up and down offset content, please search 123WORDPRESS.COM previous articles or continue to browse the following related articles, I hope you will support 123WORDPRESS.COM in the future! |
<<: Detailed explanation of adding dotted lines to Vue element tree controls
1. Check BIOS First check which startup mode your...
This article shares with you how to use JavaScrip...
Recently, there has been a growing demand for imp...
A friend in the group asked a question before, th...
Open the scheduled task editor. Cent uses vim to ...
Table of contents Preface Why does limit deep pag...
title: vue uses vue-meta-info to set the title an...
Preface When using RabbitMQ, if there is no traff...
This article describes how to add or expand a dis...
Table of contents 1. Get the value of browser coo...
I have recently studied the hollowing effect. bac...
This article shares the specific code of Vue+expr...
In actual development, the primary key of MySQL c...
Preface Recently, I was analyzing the startup pro...
Today we are going to create a simple heartbeat e...