0

我试图让一个元素的宽度大于主体,但不会导致水平滚动。

http://jsfiddle.net/hYRGT/

这希望能在一定程度上说明我的问题。#header 包含 #imghead 并设置为 960px 宽度。

我想要的是浏览器“认为”页面是 960px 宽。因为#imghead 比#header 更宽,并且相对定位,所以它位于中心。

我无法使用背景图像,因为#imghead 将被 Flash 组件替换。

我也无法使用溢出:隐藏,因为我确实希望元素显示在 960 像素之外。我只是不希望它导致 h 滚动。

我不想完全禁用 h-scrolling,我真的很喜欢 CSS 解决方案。但如果 javascript 是处理这个问题的唯一方法,我想它会做。

4

2 回答 2

0

您不能将它相对于主体绝对定位,距左侧 50%,然后在内部元素上做一个负左边距,该左边距为元素本身总宽度的一半,这将使它居中?

于 2010-09-08T15:28:15.073 回答
0

我想我得到了我想要的:http: //jsfiddle.net/hYRGT/3/

以防 jsfiddle 宕机:

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />

<title>WEBSITE title</title>

</head>
<body>
    <div id="header">
        <div id="imghead"><img src="/img.jpg" alt=""/></div>
    </div>
    <div id="wrapper" class="index">
        <div id="container">SOME CONTENT</div>
    </div>
</body>

CSS:

/*RESET*/
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,dd,dl,dt,li,ol,ul,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;font-family:inherit;font-weight:inherit;font-style:inherit;text-align:left;vertical-align:baseline}
table{border-collapse:collapse;border-spacing:0}
a img,:link img,:visited img{border:0}
address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal}
ol,ul{list-style:none}
caption,th{text-align:left}
h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}
q:before,q:after{content:''}
abbr,acronym{border:0}
img{display:block}
a{color:inherit}

/*STYLES*/
html, body{
height:100%}
body{
background:#000;
text-align:center;
overflow:auto;
overflow-x:hidden;
overflow-y:auto}

#wrapper{
    z-index:12;
    position:relative;
    height:auto!important;
    min-height:100%;
    width:100%;
    background:#0f0;
    overflow:auto;
    overflow-x:auto;
    overflow-y:visible}
#container{
    width:960px;
    margin:0 auto;
    overflow:auto;
    background:#00f}

#header{
    z-index:50;
    position:relative;
    overflow:visible;
    width:960px;
    margin:0 auto;
    height:0px;
    background:#f00}
#imghead{
    width:1100px;
    position:relative;
    left:-70px;
    background:#ff0}

内容在设计上与标题重叠,我希望这对某人有所帮助。

1个限制是标题不会水平滚动,但在我的设计中没有必要。

在 FF3、IE8、S4 和 C5 中测试

于 2010-09-09T07:38:29.467 回答