2013-01-24 21 views
2

我试图用一些浮动div和东西创建一个响应式设计。 在我的标题和图像滑块下面,我有一个div用于显示主要内容。在这里,我有2个div,一个向左漂浮,一个向右漂浮。在正确的div中,我有3个div在一个单一的柱子。在左侧的div我有4个格都向左浮动,这将使他们是这样的:为什么我的div推倒对方?

Image - Layout OK

这里是我的问题 - 当我调整我的浏览器的宽度,在左侧div容器将两分底部的div像这样:

Image - Layout NOT OK

我试图用clearfix,但似乎并没有解决这个问题,现在我卡住了。

有谁能告诉我我在这里做错了什么 - 请! ; o)

上面的屏幕截图来自Chrome浏览器。任何其他浏览器(Safari,Firefox,Opera)都会显示相同的错误,但始终。不仅当我调整到与较小的页面内容。

我希望有人能帮助我。获得那种沮丧; O( 下面,你会发现我的代码感谢

//加斯帕

HTML: 内容

 <div id="content-wrap" class="centered"> 
      <div id="fp-branding-area"> 
       <!-- nivo slider here --> 
      </div> 
      <div id="content-main"> 
       <div class="fp-box-main"> 
        <img src="images/fp_box1.jpg" alt="Alttext1"> 
        <h1 class="box-caption">Caption1</h1> 
       </div> 
       <div class="fp-box-main"><img src="images/fp_box2.jpg" alt="Alttext2"> 
        <h1 class="box-caption">Caption2</h1> 
       </div> 
       <div class="fp-box-main"><img src="images/fp_box3.jpg" alt="Alttext3"> 
        <h1 class="box-caption">Caption3</h1> 
       </div> 
       <div class="fp-box-main"><img src="images/fp_box4.jpg" alt="Alttext4"> 
        <h1 class="box-caption">Caption4</h1> 
       </div> 
      </div> <!-- /end #content-main --> 
      <div id="content-side"> 
       <div class="fp-box-side"> 
        <img src="images/fp_box_side1.jpg" alt="Side Alttext1"> 
        <h1 class="box-caption">SideCaption1</h1> 
       </div> 
       <div class="fp-box-side"> 
        <img src="images/fp_box_side2.jpg" alt="Side Alttext2"> 
        <h1 class="box-caption">SideCaption2</h1> 
       </div> 
       <div class="fp-box-side"> 
        <img src="images/fp_box_side3.jpg" alt="Side Alttext3"> 
        <h1 class="box-caption">SideCaption3</h1> 
       </div> 
      </div> <!-- /end #content-side --> 
     </div> <!-- /end #content-wrap --> 
    </div> <!-- /end #page --> 

    <footer id="footer-main"> 
     <div id="footer-wrap" class="centered"> 
      Content 
     </div> 
    </footer> 
</body> 

CSS:

h1, h2, h3, h4, hgroup { 
    font-family: Avenir; 
    font-size: 1.18em; 
    letter-spacing: .05em; 
} 

.centered { 
    position: relative; 
    margin: 0 auto; 
} 

.full-width { 
    width: 960px; 
} 

#page { 
    background: url(../images/gradient_page.png) repeat-x; 
    min-height: 100%; 
    width: 100%; 
} 

#header-main { 
    height: 137px; 
    background: url(../images/bg_header.jpg); 
} 

#header-wrap { 
    width: 960px; 
} 

#content-wrap { 
    background: url(../images/web-dropshadow_header.png) center top no-repeat; 
    width: 100%; 
    max-width: 960px; 
    overflow: auto; 
    padding-bottom: 136px; 
    /* must be same height as the footer */ 
} 

#fp-branding-area { 
    width: 100%; 
    margin-top: 2.11361%; 
    background-color: #c6c6c6; 
} 

#content-main { 
    width: 78.85416666666667%; 
    /* ((757/960) * 100) */ 
    float: left; 
    margin-bottom: 2.11361%; 
} 

#content-side { 
    width: 21.14583333333333%; 
    /* ((203/960) * 100) */ 
    float: left; 
} 

.fp-box-main { 
    position: relative; 
    width: 47.88639%; 
    /* ((362.5/757) * 100) */ 
    margin: 2.11361% 2.11361% 0 0; 
    float: left; 
    overflow: hidden; 
    clear: none; 
} 

.fp-box-main img, .fp-box-side img { 
    width: 100%; 
    margin-bottom: -3px; 
} 

.fp-box-side { 
    position: relative; 
    margin-top: 7.88177%; 
    width: 100%; 
    overflow: hidden; 
    clear: both; 
} 

.fp-box-side h1 { 
    font-size: .8em; 
} 

.box-caption { 
    background-color: black; 
    color: #FFF; 
    position: absolute; 
    text-align: center; 
    text-transform: uppercase; 
    width: 90%; 
    padding: 5%; 
    bottom: 0; 
} 

#footer-main { 
    position: relative; 
    margin-top: -136px; 
    /* negative value of footer height */ 
    height: 136px; 
    clear: both; 
    background-color: white; 
} 

#footer-wrap { 
    width: 960px; 
} 
+0

你有没有想过使用CSS框架? – Danny

+0

我已经考虑过了。但是,我想知道我的css中有什么,以及它为什么在那里。我已经使用了风格重置,但就是这样。 –

+0

其实我只是试过了。一个非常不错的工具。但是,它并没有解决我的问题。 –

回答

0

我相信,因为所有4张图片向左浮动,调整大小会影响到他们,即使你给他们的父DIV更大宽度他们会浮在1线。

左格的里,新建2周正常的div,在第一个div,把浮动左图像,和浮动右图像,并在第二个div做同样的事情。

----------------------------------------- 
|    |  |    | 
|    |  |    | 
|    |  |    | 
|    |  |    | 
|    |  |    | 
----------------------------------------- 
----------------------------------------- 
|    |  |    | 
|    |  |    | 
|    |  |    | 
|    |  |    | 
|    |  |    | 
----------------------------------------- 

如果您将左边的div和div放在最小宽度内,这样会更好。

min-width:100px; 

更新:另一种可能的解决方案

如果你的页面的最大width960px;左父股利一个min-width:500px;右父股利一个min-width:400px;这样,你的时候重新调整大小,它们将保持固定宽度并且不会变小,意味着它们不会影响它们内部浮动元素的顺序。

+0

我试过你的建议。但是,这并没有解决问题。还有其他建议吗? ; o( –

+0

@ Mr.Freeman我更新了我的答案,检查了新的解决方案 –

0

尝试制作大约1000像素的父级,并给出像素宽度,以便在调整大小或放大或缩小时,可以避免这种情况。

+0

事情是,我不希望它在像素中,我希望它是很好,灵活的百分比和EM的; o) –

0

你的问题是百分比单位和保证金。

使用百分比进行响应式设计的真正方法是在使用保证金时不使用“保证金”,因为您错误地使用了0.01%的错误使所有布局陷入瘫痪。你需要用包装和'填充'来制作。

现在,当您使用填充你可以做到这一点

* { 
box-sizing:border-box; 
-moz-box-sizing:border-box; /* Firefox */ 
-webkit-box-sizing:border-box; /* Safari */ 
} 

这个代码和平要让所有的页面,里面填充的是意思是,当你做框,50%,把50%的填充填充是计算从盒子里面。并且你可以让百分比计算而不用担心错误的0.01%。

0

我曾经遇到过这种情况,它通常与高度有关,即使它关闭1px,它也会错误地显示在布局2中。这是因为所有的盒子都向左浮动,如果它碰到另一个它会继续在一个新的行。

我会建议尝试与您的箱子设置一个明确的高度,而不是自动,并看看是否修复它。

希望这可以帮助