MDN混淆的top属性概念

huangapple go评论64阅读模式
英文:

MDN confusing concept for top property

问题

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.text {
  position: relative;
  top: 50%;
  }

<!-- language: lang-html -->
<a class="text">Hello</a>


<!-- end snippet -->
[Top][1]

> So As per MDN [Top][1] link quote:**When position is set to relative, the top property specifies the distance the element&#39;s top edge is moved below its normal position.**

So as per above quote my understanding is element&#39;s top edge should be moved below its normal position by 50% so why isn&#39;t it happening . Browser is EDGE 11.3 .

[1]: https://developer.mozilla.org/en-US/docs/Web/CSS/top
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.text {
  position: relative;
  top: 50%;
  left: 50%;
  }

<!-- language: lang-html -->

&lt;a class=&quot;text&quot;&gt;Hello&lt;/a&gt;

<!-- end snippet -->

Top

> So As per MDN Top link quote:When position is set to relative, the top property specifies the distance the element's top edge is moved below its normal position.

So as per above quote my understanding is element's top edge should be moved below its normal position by 50% so why isn't it happening . Browser is EDGE 11.3 .

答案1

得分: 6

MDN链接到规范

仔细查看在这个上下文中百分比长度的含义:

插入偏移量是相对于相应轴上的包含块尺寸的百分比(例如,左侧或右侧的宽度,顶部和底部的高度)。对于粘性定位的框,插入偏移量相对于相关滚动视窗的尺寸。允许负值。

包含块的高度为auto(默认值),当计算auto的百分比时,结果为0

英文:

MDN links to the specification.

Look carefully at what a percentage length means in this context:

> The inset is a percentage relative to the containing block’s size in the corresponding axis (e.g. width for left or right, height for top and bottom). For sticky positioned boxes, the inset is instead relative to the relevant scrollport’s size. Negative values are allowed.

The height of the containing block is auto (the default) and when a percentage is calculated of auto you get 0.

答案2

得分: 2

50%指的是什么?答案是父元素的50%。但默认情况下,父元素只高到能容纳其中的内容。如果你在父元素上设置一个高度,使其完全填充页面的垂直高度,你会看到这个元素向下移动到该高度的50%,就像这样:

英文:

50% of what? The answer is 50% of the parent element. But by default, the parent element is only tall enough to contain the things within it. If you set a height on the parent element to completely fill the vertical height of the page, you will see that this element moves down to 50% of that, like this:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->
html, body {
height: 100%;
}

.text {
  position: relative;
  top: 50%;
  left: 50%;
  }

<!-- language: lang-html -->

&lt;a class=&quot;text&quot;&gt;Hello&lt;/strong&gt;

<!-- end snippet -->

Or a fixed height, like this:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->
html, body {
height: 200px;
}

.text {
  position: relative;
  top: 50%;
  left: 50%;
  }

<!-- language: lang-html -->

&lt;a class=&quot;text&quot;&gt;Hello&lt;/a&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 02:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定