位置:相对有什么用?

发布于 2024-08-22 08:22:01 字数 288 浏览 2 评论 0原文

我今天正在考虑 CSS 定位模式,并意识到除了使 position:absolute 对子元素起作用之外,我从未将 position:relative 用于任何其他用途。

我更像是一名“开发人员”而不是“设计师”,但这些年来我已经完成了相当多的基于 CSS 的布局。我使用过表格、浮动、边距(正值和负值)、位置:绝对,甚至位置:固定,但我认为我从来没有使用过位置:相对来实际定位元素。

是否有一些伟大的 CSS 大师技术依赖于位置:相对(并在现实世界的设计中使用)?我错过了吗?

I was thinking about CSS positioning modes today and realized that I never use position:relative for anything other than making position:absolute work on child elements.

I'm more of a "developer" than a "designer", but I've done quite a few CSS-based layouts over the years. I've used tables, float, margins (positive and negative), position:absolute, and even position:fixed, but I don't think I've ever had an occasion where I've used position:relative for actually positioning an element.

Is there some great CSS-guru technique that relies on position:relative (and is used in real-world designs)? Am I missing out?

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

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

发布评论

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

评论(5

各自安好 2024-08-29 08:22:01

如果您想偏移一个元素而不用担心边距及其对其他元素的影响,那么它很有用。假设您想要故意将图像从容器侧面伸出,并(几乎)与其旁边容器中的某些内容重叠。

------------- -----
|           | |   |
|   X  -------| Y |
|      |  a  ||   |
|      -------|   |
------------- -----

容器 aX 正常文本流的一部分,您希望保持这种状态,您只是希望它从侧面伸出一点,作为一种简洁的效果。如果您使用边距来做到这一点,它可能会变得非常混乱并重排您的一些其他内容。
通过使用 position:relative; left: 10px; 您可以轻松获得该效果,而不会产生副作用。

It's useful if you want to offset an element without futzing around with margins and its consequences for other elements. Let's say you want to intentionally have an image poke out of the side of a container and (nearly) overlap with some content in a container beside it.

------------- -----
|           | |   |
|   X  -------| Y |
|      |  a  ||   |
|      -------|   |
------------- -----

Container a is part of the normal text flow of X and you want to keep it that way, you just want it poking out of the side a bit as a neat effect. If you do that with margins, it may get really messy and reflow some of your other content.
By using position: relative; left: 10px; you can get that effect easily without the side effects.

暗恋未遂 2024-08-29 08:22:01

我过去曾使用 position:relative 作为容器元素,其中包含绝对定位的元素(如居中的三列布局)。例如:

“替代文本”

我没有给容器任何偏移量,但是使用 position:relative 定位它允许我相对于容器绝对定位列。如果容器设置为 position: static(默认情况下),则列将相对于浏览器视口而不是容器绝对定位。

I've used posotion: relative in the past for a container element, with absolutely positioned elements inside it (like a centered three column layout). For example:

alt text

I don't give the container any offset, but positioning it with position: relative allows me to absolutely position the columns relative to the container. If the container is set to position: static, as it is by default, then the columns will be absolutely positioned relative to the browser viewport, not the container.

拔了角的鹿 2024-08-29 08:22:01

我使用 position:relative; ,这样上标字符仍然可以上升到 vertical-align: top; 之上,但不允许它们弄乱我的段落的开头。

sup { 
    font-size: .7em;
    vertical-align: top;
    position: relative;
    top: -.1em;
}

I use position:relative; so that superscript characters can still ascend above vertical-align: top; but doesn't allow them to mess with the leading of my paragraphs.

sup { 
    font-size: .7em;
    vertical-align: top;
    position: relative;
    top: -.1em;
}
傲世九天 2024-08-29 08:22:01

我不止一次地使用它来显示标题图像之类的内容,例如:

<div id="header">
  <div id="logo"></div>
<div>

CSS:

#header { width: 1024px; margin: 0 auto; background: url(string.jpg); }
#logo { position: relative; left: 40px; background: url(logo.jpg); }

在这种情况下,标题有一个大条纹作为背景图像,徽标位于条纹中,只是稍微偏移了一点。我认为在某些情况下比填充/边距更简单,可以稍微改变一些东西,也许这只是意见。

I've used it more than once to display things like header images, something like:

<div id="header">
  <div id="logo"></div>
<div>

CSS:

#header { width: 1024px; margin: 0 auto; background: url(string.jpg); }
#logo { position: relative; left: 40px; background: url(logo.jpg); }

In this case the header has a large stripe all the way across as a background image, the logo sits in the stripe just offset a bit. Simpler than padding/margin in some cases to shift stuff around a bit I think, maybe it's just opinion.

断舍离 2024-08-29 08:22:01

当我想要相对于其父元素定位某个绝对元素时,我主要使用它。在这种情况下,父元素需要设置为 position:relative。这就是它的用途。

此外,我还到处使用它来修复带有背景图像的块元素中的 IE6/7 haslayout/flickering bug。

I mainly use it when I want to position some absolute element relative to its parent element. In that case the parent element need to be set to position: relative. That's where it is for.

Further I also use it here and there to fix IE6/7 haslayout/flickering bugs in block elements with a background image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文