css - 棘手的CSS定位问题

标签 css

我的 body 有平铺背景(附图中的蓝色)

我需要将一些固定宽度和高度的内容(绿色)水平居中,与页面顶部的距离固定。

然后我需要将居中内容的背景图像继续到页面的左右两端,无论浏览器有多宽(紫色)

这是因为绿色内容中有一个“洞”,可以让 body 背景显示出来。如果这不是要求 id,请将紫色设置为 100% 宽度的 div 来包裹内容,并简单地为其提供平铺背景图像。

alt text

到目前为止我已经做到了:

<div id="Left"></div>
<div id="Right"></div>
<div id="Content"></div>


#Left
{
    width: 50%;
    margin-right: 480px;
    position: absolute;
    top: 50px;
    right: 50%;
    height: 525px;
    background: transparent url(/images/purple.png) repeat-x scroll center top;
}
#Right
{
    width: 50%;
    margin-left: 480px;
    position: absolute;
    top: 50px;
    left: 50%;
    height: 525px;
    background: transparent url(/images/purple.png) repeat-x scroll center top;
}

#Content
{
    position: absolute;
    top: 50px;
    left: 50%;
    margin-left: -480px;
    width: 960px;
    height: 525px;
    background: transparent url(/images/green.png) no-repeat scroll center top;
}

确实有效,除了页面有一个水平滚动条,右边的紫色部分延伸了某种方式。我怎样才能克服这个问题?

我不能通过简单地复制背景来欺骗这个漏洞,我想避免在主体上放置 overflow-x: hidden

编辑:我还需要一个可变高度的页面,因为给定的 525px 高度 可能要大得多,因此页面需要垂直滚动。

这需要在 IE7+、FF、Safari 中工作

谢谢

最佳答案

我认为这应该可以解决问题:

<html>

<head>
<style type="text/css">

body
{
    overflow-x: hidden;
    width: 100%;
    margin: 0;
    padding: 0;
}

#Left
{
    width: 50%;
    margin-right: 480px;
    position: absolute;
    top: 50px;
    right: 50%;
    height: 5525px;
    border: solid 2px purple;
}
#Right
{
    width: 50%;
    margin-left: 480px;
    position: absolute;
    top: 50px;
    left: 50%;
    height: 5525px;
    border: solid 2px purple;
}

#Content
{
    position: absolute;
    top: 50px;
    left: 50%;
    margin-left: -480px;
    width: 960px;
    height: 5525px;
    border: solid 2px green;
}

</style>
</head>

<body>

<div id="Left"></div>
<div id="Right"></div>
<div id="Content"></div>

</body>
</html>

我向 body 标签添加了样式(以防止出现水平滚动条),并且我在 Content div 上使用了“auto”边距(不确定是否有必要,但我就是这样做的)。

如果我遗漏了什么,请告诉我。

编辑:

我将绝对定位放回到内容 div 上。 GD 你的 Internet Explorer...

关于css - 棘手的CSS定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3523309/

相关文章:

javascript - 如何在需要时突出显示替代隐藏复选框输入的复选框伪元素

jquery - 尝试使用 jQuery 获取 css 左悬停属性

html - .NET/CSS 背景图像不会超过浏览器窗口的底部

html - 带有 float :left 的导航标签

css - 导航选项卡事件时刻图像不会改变

css - 除了 firefox 之外,没有其他浏览器会立即加载 CSS 文件中的更改

javascript - CSS - 仅在 Chrome 中表格单元格边框的奇怪行为

css - 并排设置两个绝对定位的 DIV 元素

html - 页脚居中帮助

css - 如何使用 css 将图标应用于链接?