CSS:顶部与顶部边缘

发布于 2024-09-29 14:20:25 字数 69 浏览 0 评论 0原文

我不确定我是否完全理解这两者之间的区别。

有人可以解释为什么我会使用其中一种而不是另一种以及它们有何不同吗?

I'm not sure if I fully understand the difference between these two.

Can someone explain why I would use one over the other and how they differ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温馨耳语 2024-10-06 14:20:25

如果您想将一个(块)元素从文档流中的其他元素移开,您可以使用边距。这意味着它将把以下元素推开/进一步向下。请注意,相邻块元素的垂直边距会折叠。

如果您希望元素对周围元素没有影响,则可以使用定位(绝对、相对)和 topbottomleft 设置。

通过相对定位,元素仍将占据其静态定位时的原始空间。这就是为什么如果您只是从 static 位置切换到 relative 位置,什么也不会发生。从那里,您可以将其推过周围的元素。

使用绝对定位,您可以从(静态)文档流中完全删除该元素,因此它将释放它所占用的空间。然后,您可以自由定位它 - 但相对于围绕它的下一个最佳非静态定位元素。如果没有,它将锚定到整个页面。

You'd use margin, if you wanted to move a (block) element away from other elements in the document flow. That means it'd push the following elements away / further down. Be aware that vertical margins of adjacent block elements collapse.

If you wanted the element to have no effect on the surrounding elements, you'd use positioning (abs., rel.) and the top, bottom, left and right settings.

With relative positioning, the element will still occupy its original space as when positioned statically. That's why nothing happens, if you just switch from static to relative position. From there, you may then shove it across the surrounding elements.

With absolute positioning, you completely remove the element from the (static) document flow, so it will free up the space it occupied. You may then position it freely - but relative to the next best non-statically positioned element wrapped around it. If there is none, it'll be anchored to the whole page.

小伙你站住 2024-10-06 14:20:25

top 用于使用 position 属性调整元素。
margin-top 用于测量元素相对于前一个元素的外部距离。

此外,top 行为可能会有所不同,具体取决于位置类型:绝对相对固定

top is for tweak an element with use of position property.
margin-top is for measuring the external distance to the element, in relation to the previous one.

Also, top behavior can differ depending on the type of position, absolute, relative or fixed.

纵山崖 2024-10-06 14:20:25

边距应用并扩展/收缩元素的正常边界,但是当您调用 top 时,您将忽略元素的常规位置并将其浮动到特定位置。

示例:

html:

<div id="some_element">content</div>

css:

#some_element {margin-top: 50%}

表示元素将在其容器的 50% 高度处开始显示 html(即,显示单词“content”的 div 将在其包含的 div 或 html 节点的 50% 高度处显示,直接位于 div#some_element 之前)但是如果您打开浏览器的检查器(在 Windows 上按 f12 或在 Mac 上按 cmd+alt+i)并将鼠标悬停在该元素上,您将看到它的边界突出显示,并注意到该元素已被推下而不是重新定位。

另一方面,顶部:

#some_element {top: 50%}

实际上会重新定位元素,这意味着它仍将显示在其容器的 50% 处,但它将重新定位元素,使其边缘从其包含元素的 50% 处开始。换句话说,元素的边缘与其容器之间会有间隙。

干杯!

Margin applies and extends / contracts the element's normal boundary but when you call top you are ignoring the element's regular position and floating it to a specific position.

Example:

html:

<div id="some_element">content</div>

css:

#some_element {margin-top: 50%}

Means the element will begin displaying html at the 50% height of its container (i.e. the div displaying the word "content" would be displayed at 50% height of its containing div or html node directly before div#some_element) but if you open your browser's inspector (f12 on Windows or cmd+alt+i on mac) and mouse over the element you will see it's boundaries highlighted and notice the element has been pushed down rather than re-positioned.

Top on the other hand:

#some_element {top: 50%}

Will actually reposition the element meaning it will still display at 50% of its container but it will reposition the element so its edge starts at 50% of its containing element. In other words, there will be a gap between the edges of the element and its container.

Cheers!

黯然#的苍凉 2024-10-06 14:20:25

top 属性是一个位置属性。它与 position 属性一起使用,例如 absoluterelativemargin-top 是元素自己的属性。

The top property is a position property. It is used with the position property, such as absolute or relative. margin-top is an element's own property.

梦里南柯 2024-10-06 14:20:25

from bytes:

“边距是元素框的边缘与整个框的边缘之间的空间,例如字母的边距。‘top’将元素的边距边缘从包含块框中替换,例如同一个块纸板箱内有纸张,但没有抵住容器的边缘。”

我的理解是,margin-top 在元素上创建一个边距,而 top 将元素的上边缘设置在包含元素的上边缘下方的偏移量处。

您可以在这里尝试:

http://w3schools.com/css/tryit.asp? filename=trycss_position_top

只需将 top 替换为 margin-top 即可看到差异。

from bytes:

"Margin is that space between the edge of an element's box and the edge of the complete box, such as the margin of a letter. 'top' displaces the element's margin edge from the containing blocks box, such as that same piece of paper inside a cardboard box, but it is not up against the edge of the container."

My understanding is that margin-top creates a margin on the element, and top sets the top edge of the element below the top edge of the containing element at the offset.

you can try it here:

http://w3schools.com/css/tryit.asp?filename=trycss_position_top

just replace top with margin-top to see the difference.

天煞孤星 2024-10-06 14:20:25

这是我的理解...

TOP:它定义了元素相对于它自己的元素或其他元素的位置,即相对顶部是指与它自己的元素进行比较,而在固定顶部是指与视口进行比较。

Margin_Top:它总是引用它自己的元素,并在其边框上添加偏移量(外部)

This is my understanding...

TOP: it defines the position of the element wrt its own elements or other element i.e in case of relative top refers to compare with its own element whereas in fixed top refers to compare with viewport.

Margin_Top: it always refer to its own elements that adds an offset(outside) to its border

稚然 2024-10-06 14:20:25
  • top:它从内容框的顶部定位
  • margin-top:它与边框框或前面的同级元素有间隙或空间
<div class="text"></div>
.text {
  top: 50%;
} 
  • top: it is positioning from top of content box
  • margin-top: it is gap or space away from border box or preceding sibling element
<div class="text"></div>
.text {
  top: 50%;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文