CSS 3柱液体布局,带固定中心柱

时间:2013-06-04 02:34:18

标签: html css layout liquid-layout

我想为我的营销网站制作一个3列布局,顶部横幅中有图像。

我希望液体左/右侧有固定的中心。理想情况下,html看起来像这样:

<div id="pixelLeft">&nbsp;</div>
<div id="bannerCenter">
  <img src="images/mybanner.png" />
</div>
<div id="pixelRight">&nbsp;</div>

<style>
#pixelLeft { background: url(../../images/pixel_left_fixed.png) 0 0 repeat-x; }
#pixelRight { background: url(../../images/pixel_right_fixed.png) 0 0 repeat-x; }
#bannerCenter { /* something here to make fixed width of 1550px */ }
</style>

左/右像素侧的图像是1px x 460px。 图像mybanner.png是1550px x 460px。

提前致谢! (特别是如果它适用于所有浏览器!)

4 个答案:

答案 0 :(得分:16)

这有用吗?

CSS Only Demo

jQuery Demo(Cross Browser Compatible)

<div class="wrap">
    <div id="pixelLeft">&nbsp;</div>
    <div id="bannerCenter">
      <img src="images/mybanner.png" />
    </div>
    <div id="pixelRight">&nbsp;</div>
</div>
<div style="clear:both;"></div>

*{
    margin:0;
    padding:0;
}
#bannerCenter{
    background:#ddd;
    width: 500px;
    float:left;
}
#pixelLeft{
    background:#999;
    width: calc(50% - 250px);
    float:left;
}
#pixelRight{
    background:#999;
    width: calc(50% - 250px);
    float:right;
}

#bannerCenter,#pixelLeft,#pixelRight{
    height: 400px;
}

您可以使用jQuery而不是calc(50% - 250px);来使其与旧版浏览器兼容。

$(document).ready(function() {
    $(window).on('resize', function() {
         $('#pixelLeft, #pixelRight').css('width',($('body').width()-$('#bannerCenter').width())/2);
    }).trigger('resize');      
});

更新:2018年6月

为同样的问题添加了flexbox解决方案。

*{
    margin:0;
    padding:0;
}
.wrap {
  display: flex;
}
#pixelLeft, #pixelRight{
  display: flex;
  flex:1;
}
#bannerCenter{
    background:#ddd;
    min-width: 500px;
    display: flex;
    flex: 1;
}
#pixelLeft{
    background:#999;
}
#pixelRight{
    background:#999;
}
#bannerCenter,#pixelLeft,#pixelRight{
    height: 400px;
}
<div class="wrap">
    <div id="pixelLeft">&nbsp;</div>
    <div id="bannerCenter">
      <img src="images/mybanner.png" />
    </div>
    <div id="pixelRight">&nbsp;</div>
</div>

答案 1 :(得分:9)

这是一个很好的解决方案,在我看来是最简单的解决方案。它看起来很干净,不需要包装div。

Demo

<强> HTML

<body>

<div id="left_container">
    <div id="left">
        left content
    </div>
</div>

<div id="center">
    center content
</div>

<div id="right_container">
    <div id="right">
        right content
    </div>
</div>

</body>

<强> CSS

#left_container {
  width:50%;
  float:left;
  margin-right:-480px; /* minus half of the center container width */

  /* not important */
  height: 200px;
}
#left {
  margin-right:480px; /* half of the center container width */

  /* not important */
  background: #888;
  height: 600px;
}
#center {
  width:960px; /* size of the fixed width */
  float:left;

  /* not important */
  color: #FFF;
  background: #333;
  height: 500px;
}
#right_container {
  width:50%;
  float:right;
  margin-left:-480px; /* minus half of the center container width */

  /* not important */
  height: 300px;
}
#right {
  margin-left:480px; /* half of the center container width */

  /* not important */
  height: 300px;
  background-color: #888;
}

享受!

答案 2 :(得分:1)

对此有几种解决方案,可能是流行的一种是圣杯方法。它有点高于我的头脑,但这些链接很好地解释了它。

http://alistapart.com/article/holygrail

http://matthewjamestaylor.com/blog/perfect-3-column.htm

我将从A List Apart的文章开始。祝你好运。

重新阅读后,我认为这就是我要做的事情:

<强> HTML

<div id="header">
</div><div id="container">
    <div id="center" class="column"></div>
    <div id="left" class="column"></div>
    <div id="right" class="column"></div>
</div><div id="footer"></div>

<强> CSS

body {
    min-width: 550px;      /* 2x LC width + RC width */
}
#container {
    padding-left: 200px;   /* LC width */
    padding-right: 150px;  /* RC width */
}
#container .column {
    position: relative;
    float: left;
}
#center {
    width: 100%;
}
#left {
    width: 200px;          /* LC width */
    right: 200px;          /* LC width */
    margin-left: -100%;
}
#right {
    width: 150px;          /* RC width */
    margin-right: -150px;  /* RC width */
}
#footer {
    clear: both;
}
/*** IE6 Fix ***/
* html #left {
  left: 150px;           /* RC width */
}

您需要调整一些维度,但内联注释应该有所帮助。所以你有它。圣杯布局。

答案 3 :(得分:0)

<body>
  <div style="   width: 200px;    float: left;    background: red;    height: 100px;">Left</div>
 
  <div style="    float: right;    width: 200px;    background: red;    height: 100px;">Right</div>
   <div style="    background: blue;  margin:0 auto; height:100px;">Center content goes here</div>
</body>

这是通过html和css的简单技巧,只做这样的分层结构,即使你要调整浏览器的大小,它也会将中间层保持在中心位置。

相关问题