Pure CSS to achieve the internal anchor point of the web page when the up and down offset code example

Pure CSS to achieve the internal anchor point of the web page when the up and down offset code example

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

>>:  MySQL Series 4 SQL Syntax

Recommend

Install Ubuntu 18 without USB drive under Windows 10 using EasyUEFI

1. Check BIOS First check which startup mode your...

Ant Design Blazor component library's routing reuse multi-tab function

Recently, there has been a growing demand for imp...

How to use CSS3 to implement a queue animation similar to online live broadcast

A friend in the group asked a question before, th...

How to use Linux to calculate the disk space occupied by timed files

Open the scheduled task editor. Cent uses vim to ...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

Vue uses vue meta info to set the title and meta information of each page

title: vue uses vue-meta-info to set the title an...

Analysis of the principle of Rabbitmq heartbea heartbeat detection mechanism

Preface When using RabbitMQ, if there is no traff...

How to add a disk in Vmware: Expand the disk

This article describes how to add or expand a dis...

Share 20 JavaScript one-line codes

Table of contents 1. Get the value of browser coo...

Example code for achieving hollowing effect with pure CSS

I have recently studied the hollowing effect. bac...

Vue+express+Socket realizes chat function

This article shares the specific code of Vue+expr...

How to run tomcat source code in maven mode

Preface Recently, I was analyzing the startup pro...

HTML+CSS to create heartbeat special effects

Today we are going to create a simple heartbeat e...