Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何跳到片段标识符稍高的点?_Html_Fragment Identifier - Fatal编程技术网

Html 如何跳到片段标识符稍高的点?

Html 如何跳到片段标识符稍高的点?,html,fragment-identifier,Html,Fragment Identifier,我不喜欢片段标识符如何跳到页面上内容开始的实际位置。从屏幕顶部开始显示框看起来很尴尬。这让我有点烦。因此,我使用了一个锚,上面有一个name属性,设置为display:block,这样它就会注意到它下面框顶部的10px边距。使用这种方法,它实际上跳到了该内容,内容的开始和浏览器窗口的实际顶部边缘之间的间隔为10px <a name="some-text"></a> 假设它们的顶部都有一个10px的边距,有没有办法对它们重新应用ID,这样当我使用片段标识符跳转到不同的点时

我不喜欢片段标识符如何跳到页面上内容开始的实际位置。从屏幕顶部开始显示框看起来很尴尬。这让我有点烦。因此,我使用了一个锚,上面有一个name属性,设置为
display:block
,这样它就会注意到它下面框顶部的10px边距。使用这种方法,它实际上跳到了该内容,内容的开始和浏览器窗口的实际顶部边缘之间的间隔为10px

<a name="some-text"></a>
假设它们的顶部都有一个10px的边距,有没有办法对它们重新应用ID,这样当我使用片段标识符跳转到不同的点时,它仍然包括内容和浏览器窗口顶部边缘之间的10px空间


注意:我已尝试将ID直接应用于每个分区,但在确定跳转位置时,浏览器会忽略元素上的边距。

我起初没有正确理解,让我再试一次。你说:

我不能将所有的name属性都更改为id属性,因为它会变成一个空的超链接,而不是一个命名的锚(我也尝试过,display:block不再应用于它)

我真的不明白这里的问题在哪里,或者您为什么希望它使用
display:block
。它的目的正如我和W3C所看到的,它应该像在HTML4中一样充当锚,只使用
id
而不是
name
属性

如果您通过W3C的标记验证器运行这个简单的html,您将看到它是有效的html5

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <a id="test"></a>
</body>
</html>

因此,它归结为以下两种选择:

a。我有点不对劲,很抱歉,希望你能纠正我的错误


b。你正在不遗余力地完成一些容易实现的事情。

使用填充可以避免这个问题。由于边距不包括在内容边界中,因此浏览器将忽略它

<!doctype html>
<html>

<head>
<style type="text/css">
body {
  margin: 0;
  padding: 0;
}
div {
  padding-top: 10px;
}

</style>
</head>
<body>

<div id="1">How can I jump to a point slightly above the fragment identifier?<br/>
up vote 2 down vote favorite</div>

<div id="2">
I don't like how the fragment identifier jumps to the actual point of where the content begins on the page. It looks awkward having the box start right at the top of the screen. It kind of bugs me. So I was using an anchor with a name attribute above it, set to display: block so that it would pay attention to the 10px margin on the top of the box below it. Using this method, it essentially jumped down to that content, with 10px spacing between the start of the content and the actual top edge of the browser window.</div>

<div id="3">
But it seems that I'm no longer allowed to do this in HTML5, as when I try to validate the page, I get this nice warning:</div>

<div id="4">
    The name attribute is obsolete. Consider putting an id attribute on the nearest container instead.</div>

<div id="5">
I can't just change all the name attributes to id attributes because it becomes an empty hyperlink rather than a named anchor (I've also tried, the display: block no longer gets applied to it).</div>

<div id="6">
So, given a group of divisions, like so:</div>

<div id="7">Content</div>
<div id="8">Content</div>
<div id="9">Content</div>
<div id="10">Content</div>

<div id="11">
and assuming that they all have a 10px margin applied to the top of them, is there any way to re-apply the IDs to them so that when I use fragment identifiers to jump to different spots, it still includes the 10px space between the content and the top edge of the browser window?</div>

<div id="12">
Note: I've tried applying the ID directly to each division, but the browser ignores the margin on the element when determining where to jump.</div>

<div id="13">
html fragment-identifier</div>

</body>

</html>

身体{
保证金:0;
填充:0;
}
div{
填充顶部:10px;
}
如何跳到片段标识符稍高的点?
投赞成票,投反对票,投赞成票 我不喜欢片段标识符如何跳到页面上内容开始的实际位置。从屏幕顶部开始显示框看起来很尴尬。这让我有点烦。所以我使用了一个锚,上面有一个name属性,设置为display:block,这样它就会注意到它下面框顶部的10px边距。使用这种方法,它实际上跳到了该内容,内容的开始和浏览器窗口的实际顶部边缘之间的间隔为10px。 但在HTML5中似乎不再允许我这样做,因为当我尝试验证页面时,我得到了一个很好的警告: name属性已过时。考虑将ID属性放置在最近的容器上。 我不能将所有的name属性都更改为id属性,因为它会变成一个空的超链接,而不是一个命名的锚(我也尝试过,display:block不再应用于它)。 因此,给定一组划分,如下所示: 内容 内容 内容 内容 假设它们的顶部都有一个10px的边距,有没有办法对它们重新应用ID,这样当我使用片段标识符跳转到不同的点时,它仍然包括内容和浏览器窗口顶部边缘之间的10px空间? 注意:我已尝试将ID直接应用于每个分区,但在确定跳转位置时,浏览器会忽略元素上的边距。 html片段标识符
在锚定之后立即放置具有所需高度的类的元素


举个例子

当顶部有固定元素阻碍时,这会变得特别烦人,我不得不多次处理这些元素。虽然这适用于裸露的、没有样式的盒子,但这些盒子将来可能会应用边框和填充,这将导致这不再起作用+但是,我建议您使用脚本解决方案。我不确定您的确切目的是什么。我不想把它调高10像素,我想把它调低。另外,移动带有ID的元素仍然会导致浏览器跳转到元素实际所在的位置。起初我误解了你的意思。希望这次我能帮上点忙。我编辑了我的答案,这很有趣。理论上,它仍然可以工作,但是浏览器不会像对待
名称
属性那样对待
id
属性。这就是我面临的问题:渲染。我知道它可以通过
id
进行验证。所有浏览器都可以吗?听起来很有趣,我一定会仔细看一看。如果JSFIDLE是可能的话,我认为它将是解决您的问题的一个很好的例子。
<!doctype html>
<html>

<head>
<style type="text/css">
body {
  margin: 0;
  padding: 0;
}
div {
  padding-top: 10px;
}

</style>
</head>
<body>

<div id="1">How can I jump to a point slightly above the fragment identifier?<br/>
up vote 2 down vote favorite</div>

<div id="2">
I don't like how the fragment identifier jumps to the actual point of where the content begins on the page. It looks awkward having the box start right at the top of the screen. It kind of bugs me. So I was using an anchor with a name attribute above it, set to display: block so that it would pay attention to the 10px margin on the top of the box below it. Using this method, it essentially jumped down to that content, with 10px spacing between the start of the content and the actual top edge of the browser window.</div>

<div id="3">
But it seems that I'm no longer allowed to do this in HTML5, as when I try to validate the page, I get this nice warning:</div>

<div id="4">
    The name attribute is obsolete. Consider putting an id attribute on the nearest container instead.</div>

<div id="5">
I can't just change all the name attributes to id attributes because it becomes an empty hyperlink rather than a named anchor (I've also tried, the display: block no longer gets applied to it).</div>

<div id="6">
So, given a group of divisions, like so:</div>

<div id="7">Content</div>
<div id="8">Content</div>
<div id="9">Content</div>
<div id="10">Content</div>

<div id="11">
and assuming that they all have a 10px margin applied to the top of them, is there any way to re-apply the IDs to them so that when I use fragment identifiers to jump to different spots, it still includes the 10px space between the content and the top edge of the browser window?</div>

<div id="12">
Note: I've tried applying the ID directly to each division, but the browser ignores the margin on the element when determining where to jump.</div>

<div id="13">
html fragment-identifier</div>

</body>

</html>
.spacer{height:25px;}​