HTML浮动div的问题
我有一个问题,正如我上面提到的。
在我的 Web 应用程序中,我将通过 jQuery(ASP.NET MVC) 动态生成许多 div。 每个新的 div 可以有不同的宽度,并且所有它们必须向左浮动
我尝试(测试)向左浮动 2 个 div,但没有成功。我做错了什么?
每个div都有一个定义的宽度,因为当所有div的总宽度>时mainDIV 的宽度,然后滚动条就会出现。现在,在这种情况下,这 2 个 div 不会向左浮动
这是代码
<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>
I have a problem as I mentioned above.
In my web app, I'll be generating many divs dynamically by jQuery(ASP.NET MVC).
Each new div can have a different width, and all of them MUST be floated to the left
I tried (test) to float to the left 2 divs, but with no success. What am I doing wrong ?
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
Here's the code
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须确保包含的 div 足够宽,可以并排容纳浮动的 div。
因此,在您的示例中,您必须将包含 div
mainDIV
的宽度设置为至少 1920px。如果您希望滚动条出现在
mainDIV
上,您需要一个额外的包装器:html:
css:
You have to make sure that the containing div is wide enough to accommodate the floated div's side by side.
So in your example, you would have to set the width of the containing div
mainDIV
to at least 1920px.You need an additional wrapper if you want the scroll-bars to appear on
mainDIV
:html:
css:
我会尝试以一种不必为每个元素执行 style= 的方式使用 CSS。如果没有更多的上下文和/或测试,我不能保证它会解决您的问题,但它有可能会解决并且有更好的形式。
为所有 div 标签设置 float:left 将
所有要向左浮动的 div 标签放在同一个类中
另外,请确保您没有指定任何类型的绝对位置,因为这将覆盖浮动。关于浮动和宽度似乎有一些微妙之处,所以也请检查一下http://www.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/
http://css.maxdesign.com.au/floatutorial/
I'd try to use CSS in a way that doesn't have to do style= for each element. Without more context and/or testing I can't guarantee it will fix your problem, but its possible it will and its better form.
Either set float:left for all div tags
put all div tags to be floated left in the same class
Also, make sure you do not specify any kind of absolute position as this will override the float. There appear to be some subtleties concerning float and width, so check those out too http://www.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/
http://css.maxdesign.com.au/floatutorial/