代码之家  ›  专栏  ›  技术社区  ›  Tony

HTML浮动分隔的问题

  •  0
  • Tony  · 技术社区  · 15 年前

    如上所述,我有一个问题。

    在我的Web应用程序中,我将通过jquery(ASP.NET MVC)动态生成许多div。 每个新的DIV可以有不同的宽度,所有宽度 MUST be floated to the left

    我试着(测试)到左边的2个跳水,但没有成功。我做错什么了?

    Each div has a defined width, because when the total width of all divs > mainDIV's width, then the scrollbar will appear. Now, in that case, this 2 divs are not floated to the left

    这是密码

    <div id="mainDIV" style="overflow:auto; width:100%;">
       <div style="width:960px; float:left; background-color:Lime;">
          a
       </div>
       <div style="width:960px; float:left; background-color:Red;">
          b
       </div>
    </div>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   jeroen    15 年前

    您必须确保包含DIV的宽度足以容纳浮动的DIV并排。

    所以在您的示例中,您必须设置包含DIV的宽度 mainDIV 至少1920像素。

    如果希望滚动条出现在 主分区 :

    HTML:

    <div id="mainDIV" style="overflow:auto; width:100%;">
       <div id="wrapper">
         <div style="width:960px; float:left; background-color:Lime;">
           a
         </div>
         <div style="width:960px; float:left; background-color:Red;">
           b
         </div>
       </div>
    </div>
    

    CSS:

    #wrapper {
        width: 1920px;
    }
    
        2
  •  1
  •   jon_darkstar    15 年前

    我尝试使用CSS的方式不必为每个元素做style=如果没有更多的上下文和/或测试,我不能保证它能解决您的问题,但它可能会解决问题,而且形式更好。

    为所有DIV标记设置float:left

    div {float:left;}
    

    将要浮动的所有DIV标记放在同一类中

    <div class="className" style="width:960px; background-color:Red;">
    a
    </div>
    
    div.className {float:left;}
    

    此外,请确保不要指定任何类型的绝对位置,因为这将覆盖浮动。关于浮点数和宽度,似乎有一些微妙的地方,所以也要检查一下。 http://www.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/
    http://css.maxdesign.com.au/floatutorial/