【问题标题】:Centering a fixed header and sidebar?居中固定的标题和侧边栏?
【发布时间】:2011-08-29 19:36:17
【问题描述】:

我正在尝试使用固定的标题和侧边栏创建布局,但我不想在浏览器的中心进行布局。显然这是一个问题,因为固定通常不允许这样做。我考虑过在所有内容周围放置一个包装器并使用 margin: 0px auto;等等,但那没有用,有可能吗?

   #header {
position: fixed;
top: 0;
left: 0;
width: 100%; 
height: 150px;
background-color: #FFF;
z-index: 100;
}


#content {
width: 1112px;
overflow: auto;
padding-top: 150px;

}


#sidebar {
position: fixed;
top: 0;
left: 0;
width: 275px;
height: 100%;
z-index: 100;

}

想过这样的事情,但没有用:

body {
    margin:0px 0px; padding:0px;
    text-align:center;  
}


#wrapper {
text-align: left;
margin: 0px auto;
}

div:

<div id="wrapper">
<div id="header"></div>
<div id="content"></div>
<div id="sidebar"></div>
</div>

【问题讨论】:

  • 您能否详细说明您想要什么。您想要将标题和侧边栏固定在左侧并且内容居中吗?
  • @Amy 你在 ie6 上遇到这个问题了吗

标签: css width position center fixed


【解决方案1】:

试试这个:

CSS:

body {
margin:0; 
padding:0; 
}


#wrapper {
margin: 0 auto 0 auto;
width:1112px;
min-height:600px;
height:auto;
background-color:#009900;
}

#header {
position: fixed;
width: 1112px; 
height: 150px;
background-color: #ccc;
z-index: 100;
}


#content {
position:absolute;
top:150px;
margin-left:275px;
overflow: auto;
width:837px;  /* 1112px - 275px = 837px */
background-color:#CC9900; 
}


#sidebar {
position: fixed;
top:150px;
width: 275px;
height: 100%;
min-height:200px; /* just do simulate content */
z-index: 100;
background-color:#96F;
}

HTML:

<div id="wrapper">
<div id="header">Fixed header</div>
<div id="content">
<p>Dummy Text</p><br /><br /><br />
<p>Dummy Text</p><br /><br /><br />
<p>Dummy Text</p><br /><br /><br />
<p>Dummy Text</p><br /><br /><br />
<p>Dummy Text</p><br /><br /><br />
<p>Dummy Text</p><br /><br /><br />
<p>Dummy Text</p><br /><br /><br />
<p>Dummy Text</p><br /><br /><br />
</div>
<div id="sidebar">Fixed sidebar</div>
</div>

如果我理解您的问题,我认为这是一种可行的方法。试试看,然后告诉我! :)

【讨论】:

  • @ninja_corp 您好,我还有一个问题。如果我打算将内容与导航侧栏交换以使侧栏在右侧保持静止,我该怎么做?我尝试使用浮动,但它不适用于 position:fixed。
  • 一流的ninja_corp!
【解决方案2】:

不确定我是否正确 - 你的意思是这样的吗?

<!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" >
<head>
    <title>Untitled Page</title>
    <style type="text/css">
        body {margin:0;padding:0;}
    .wrapper {position:relative;margin:0 auto;width:600px;}
#header {
position: fixed;
top: 0;
left: 0;
width:100%;
height: 150px;
background: #ccc;
z-index: 100;
}
#content {
padding-top: 150px;
width:400px;
background:green;
overflow:auto;
}
#sidebar {
width: 200px;
padding-top: 150px;
float:left;
background:red;
position:fixed;
margin-left:400px;
top:0;
height:100%;
}

    </style>
</head>
<body>
<div id="header">
    <div class="wrapper">
        header
    </div>
</div>
<div class="wrapper">
    <div id="content">
        content start<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content end
    </div>
    <div id="sidebar">
        sidebar
    </div>
</div>
</body>
</html>

注意 position:fixed 在 ie6 中不起作用。

【讨论】:

  • 这可行,你知道固定位置的替代方法,即 6 吗?
  • @Amy - 是的,在 ie6 中有多种强制方法 - 取决于您是否需要 css 或 js 解决方案 - 一个开始可能是这个链接:ryanfait.com/position-fixed-ie6
猜你喜欢
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2016-12-13
  • 2015-09-27
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多