【问题标题】:Top Container Background Problem顶级容器背景问题
【发布时间】:2011-02-07 10:15:21
【问题描述】:

截图如下:

header http://dl.getdropbox.com/u/118004/Screen%20shot%202010-04-13%20at%202.50.49%20PM.png

左侧的红色条是我为#personal div 设置的背景,我希望它垂直对齐到容器的顶部。

问题是我在#container div 的顶部有绝对定位的#container-top div 的背景。有没有办法将#personal div 向上移动,这样就没有空间了?

HTML

<div id="container">
  <div id="container-top"></div>
    <div id="personal">
      <h1>Jonathan Doe</h1>
      <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliqua erat volutpat.</p>
    </div> <!-- end #personal -->
</div> <!-- end #container -->

CSS

#container {
  background: url(images/bg-mid.png) repeat-y top center;
  width: 835px;
  margin: 40px auto;
  position: relative;
}

#container-top {
  background: url(images/bg-top.png) no-repeat top center;
  position: absolute;
  height: 12px;
  width: 835px;
  top: -12px;
} 

#container-bottom {
  background: url(images/bg-bottom.png) no-repeat top center;
  position: absolute;
  height: 27px;
  width: 835px;
  bottom: -27px;
}

#personal {
  background: url(images/personal-info.png) no-repeat 0px left;
}

【问题讨论】:

  • 我通过将红色条的上部添加到“bg-top.png”然后将#personal 背景位置设置为“-12px left”来修复它。感谢您的回答!

标签: html background background-image css


【解决方案1】:

除非我遗漏了什么,否则我会看到您发布的内容会产生您想要的结果。

我想知道您是否已将重置 CSS 应用于您的样式?如果没有,您可以查看yahoo user interface css resetEric Meyer's reset CSS

浏览器可能有一些默认的填充和边距。

在其他样式之前包含重置 CSS。也许您已经知道这一点,但值得一问。

【讨论】:

  • 我有一个重置文件。我不想要红色背景上方的间隙。
【解决方案2】:

更改以下 CSS:

    #personal {
  background: url(images/personal-info.png) no-repeat 0px left;
position: relative;
top: 0px;
left: 0px;
}

你需要position这个元素relative给它的父元素; container,以便它适合您想要的位置。 topleft 属性是您想要特定元素的父 div左上角 的距离。

或者这个:

#personal {
      background: url(images/personal-info.png) no-repeat 0px left;
    margin-top:-10px; /*px value that works*/
    }

这将获取其在其父 div 中的当前位置,并将其渲染为比当前位置高 10 个像素。

【讨论】:

  • 这根本不会改变任何东西,不是吗?
  • 据我所知,它应该把它推到他想要的地方,还是我做错了什么?
  • 容器顶部背景消失。
  • 试试改成position:absolute;
  • 我的个人div下面还有其他内容。
【解决方案3】:

[编辑:修改。]

HTML:

<div id="container">
  <div id="container-inner">
    <div id="personal">
      <h1>Jonathan Doe</h1>
      <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliqua erat volutpat.</p>
    </div> <!-- end #personal -->
  </div> <!-- end #container-bg -->
</div> <!-- end #container -->

CSS:

#container {
  background: url(images/bg-mid.png) repeat-y top center;
  width: 835px;
  /*margin: 40px auto;*/
  margin: 28px auto 40px auto;
  position: relative;
}

#container-inner {
  background: url(images/bg-top.png) no-repeat top center;
}

#personal {
  background: url(images/personal-info.png) no-repeat 0px left;
  padding-top: 12px;
}

这符合你的意愿吗?

【讨论】:

  • 这样会更好。
  • 背景是透明的,因此将它们放在一起会产生双重阴影。
  • 但是您要求将第三个背景向上移动。它怎么不会像你问的那样产生双重阴影?请澄清。
  • 我完全误解了这个问题。让我想想再改写。
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2019-01-02
  • 1970-01-01
  • 2020-11-23
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多