【问题标题】:position: absolute without setting top/left/bottom/right?位置:绝对没有设置上/左/下/右?
【发布时间】:2012-05-01 21:46:02
【问题描述】:

案例#1:

我想在默认 WordPress 主题 (http://twentyelevendemo.wordpress.com/) 的标题中的照片上方放置一个徽标

我的解决方案:在照片前添加标志,并在其上设置position: absolute无需设置任何top/left/bottom/right属性:

http://jsfiddle.net/TsAJp/

HTML:

<a id="header">
  <img id="logo"> 
  <img id="photo">
</a>

CSS:

#logo {
  position: absolute;
  margin: 10px;
  /* or padding: 10px; */
  /* or border: 10px solid transparent;
     only this works with my elderly iPhone Simulator.app */
}

案例#2:

另一个例子是一个水平的多级菜单,它是 100% 宽并用display: table-* 布局,但table-cells 不支持position: relative,所以我唯一的解决方案是:http://jsfiddle.net/pCe49/

它适用于 IE6-7、Firefox1.5,不适用于 Firefox 0.8 等。

您认为这是一个好的解决方案,还是一种非标准的 hack,随时可能崩溃?

【问题讨论】:

  • 感谢您的提琴!我自己尝试将此解决方案与父级上的line-height 设置结合使用,这将子菜单推得太远。减去line-height 并添加padding 有帮助,耶!

标签: css css-position


【解决方案1】:

AFAIK,你应该注意分层css规则,因为如果你不指定top,left和其他属性,它们是从父元素继承的,或者在浏览器的css中默认设置。恕我直言,最好用您的值初始化元素。

【讨论】:

  • 在这种情况下,如有必要,我会将top/left/etc 设置为auto !important
  • 嗯,但它的目标是什么?
  • 仍然不知道你想做什么。
  • 在水平菜单示例中,除了这个 hack 来正确定位子菜单,我根本找不到任何其他解决方案,但我不知道它有多安全。
  • 我知道这是一个旧答案,但仅供参考:1)上/右/下/左不是从父级继承的 2)AFAIK 没有用户代理也没有这些道具的默认值 -> 应该是安全地将它们排除在外
【解决方案2】:

标准通常说如果上/下、左/右是自动的,则默认它们为position: static 值:

http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-width

http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-height

【讨论】:

猜你喜欢
  • 2014-06-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
相关资源
最近更新 更多