HTML浮动div的问题

发布于 2024-10-02 23:04:53 字数 572 浏览 0 评论 0原文

我有一个问题,正如我上面提到的。

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-10-09 23:04:53

您必须确保包含的 div 足够宽,可以并排容纳浮动的 div。

因此,在您的示例中,您必须将包含 div mainDIV 的宽度设置为至少 1920px。

如果您希望滚动条出现在 mainDIV 上,您需要一个额外的包装器:

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;
}

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:

<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;
}
止于盛夏 2024-10-09 23:04:53

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

为所有 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/

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

div {float:left;}

put all div tags to be floated left in the same class

<div class="className" style="width:960px; background-color:Red;">
a
</div>

div.className {float:left;}

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文