简体   繁体   English

偏移同一页面中的链接

[英]Offsetting link within same page

I have a menu bar at the top that has links as below: 我在顶部的菜单栏具有以下链接:

<a href="#1">Football</a>
<a href="#2">Cricket</a>
<a href="#3">Rugby</a>
<a href="#4">Tennis</a>

I have a fixed menu bar that is 50px in height. 我有一个固定的菜单栏,高度为50px。 When the links are clicked, the headings are hidden behind the fixed menu bar. 单击链接后,标题将隐藏在固定菜单栏的后面。 Is it possible to have the headings be displayed 50px lower (so they can show) when its corresponding link is clicked. 单击相应的链接时,标题是否可以低50px显示(以便显示)。

html: HTML:

<h2 class="menuheading" id="1">Football</h2>
...
<h2 class="menuheading" id="2">Cricket</h2>
...
<h2 class="menuheading" id="3">Rugby</h2>
...
<h2 class="menuheading" id="4">Tennis</h2>

you may apply always a padding-top/margin-top to your headings or (in modern browser) only when they are targetted — as you asked, eg 您可以始终在标题上使用padding-top/margin-top ,或者(仅在现代浏览器中)仅在定位目标时使用-例如,根据您的要求

h2:target {
  padding-top: 50px;
}

Further information about :target pseudoclass on MDN 有关:target DN上MDN的更多信息

Add this to your CSS style for menuheading 将此添加到您的CSS样式中作为menuheading

.menuheading {
  margin-top:50px;
}

A possible duplicate of this... How can I make a menubar fixed on the top while scrolling 可能与此重复... 滚动时如何使菜单栏固定在顶部

What happens is the body tag is loaded at the top of the page by default, so all you need to do is put 发生的是默认情况下,body标签被加载到页面顶部,因此您所要做的就是

body {margin-top:50px;} /* Same height as the menu bar */

it goes into more detail on the attached question link. 在随附的问题链接上有更详细的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM