CSS - 使用浮动元素定位不起作用

发布于 2024-12-18 18:38:49 字数 916 浏览 2 评论 0原文

我正在尝试定位两个面板,但无法使其工作...

我有一个容器页面包装两个面板,每个面板都有自己的页面。我想使用浮动将面板并排放置。

这是我的 CSS:

  .pages {width: 100%; position: absolute;}
  .leftPanel {position: relative; width: 25%; min-width:100px; float: left;}
  .rightPanel {position: static;}

和 HTML,

  <div class="page">
    <div id="lefty" class="leftPanel">
      <div class="page">
        <p>helloworld</p>
      </div>
    </div>
    <div id="righty" class="rightPanel">
      <div class="page">
         <p>HELLO WORLD</p>
      </div>
    </div>    
 </div>

我必须对左面板使用 position:relative ,对右面板使用 position:static 。奇怪的是,这在 JSBin 中有效,但在我的实际页面中,带有position:static的右侧面板始终有100 % 覆盖整个屏幕的宽度。

关于我可能做错了什么的任何提示吗?

谢谢!

I'm trying to position two panels and just can't get it to work...

I have a container-page wrapping two panels, each with it's own page. I want to position the panels side by side using float.

This is my CSS:

  .pages {width: 100%; position: absolute;}
  .leftPanel {position: relative; width: 25%; min-width:100px; float: left;}
  .rightPanel {position: static;}

and HTML

  <div class="page">
    <div id="lefty" class="leftPanel">
      <div class="page">
        <p>helloworld</p>
      </div>
    </div>
    <div id="righty" class="rightPanel">
      <div class="page">
         <p>HELLO WORLD</p>
      </div>
    </div>    
 </div>

I have to use position:relative for the left panel and position:static for the right panel. Strangely this works in JSBin but in my actual page, the right panel with position:static always has 100% width covering the whole screen.

Any hints on what I may be doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

最好是你 2024-12-25 18:38:49

默认情况下,div 元素的宽度为其父元素的 100%。由于您浮动了左撇子 div,因此您将其从流中取出,因此发生的情况是左撇子 div 实际上位于元素流之外。浮动也会导致 div 收缩到其子级的大小。因此,如果您想将右侧 div 设置为与左侧 div 相对,那么您应该做两件事:首先添加 float:left; position:relative; 到正确的样式。其次,您应该在其底部添加一个 div 以清除浮动。

另一方面,如果您要以相同的方式设置多个元素的样式,则应该仅使用类,否则只需根据 ID 设置元素的样式即可。

  .pages {width: 100%; position: absolute;}
  .leftPanel {position: relative; width: 25%; min-width:100px; float: left;}
  .rightPanel {position: relative; float: left;}

  <div class="page">
    <div id="lefty" class="leftPanel">
      <div class="page">
        <p>helloworld</p>
      </div>
    </div>
    <div id="righty" class="rightPanel">
      <div class="page">
         <p>HELLO WORLD</p>
      </div>
    </div>
    <div style="clear:left;"></div>    
 </div>

div elements by default have a width of 100% of their parent. Since you floated the lefty div you took it out of the flow so what is happening is that the lefty div is effectively sitting outside the flow of the elements. Also float causes the div to shrink-wrap to the size of it's children. So if you are wanting to set the righty div to but up against the lefty div then you should do two things: first add float:left; position:relative; to the righty styling. Second you should add a div at the bottom of that to clear your floats.

On another note you should only use a class if you are going to be styling multiple elements the same way, otherwise just style the element off of the ID.

  .pages {width: 100%; position: absolute;}
  .leftPanel {position: relative; width: 25%; min-width:100px; float: left;}
  .rightPanel {position: relative; float: left;}

  <div class="page">
    <div id="lefty" class="leftPanel">
      <div class="page">
        <p>helloworld</p>
      </div>
    </div>
    <div id="righty" class="rightPanel">
      <div class="page">
         <p>HELLO WORLD</p>
      </div>
    </div>
    <div style="clear:left;"></div>    
 </div>
霊感 2024-12-25 18:38:49

尝试浮动两个面板吗?截至目前,只有左边的一个浮动...尝试将它们都浮动到左侧,然后在它们之间放置正确的边距量,以使它们按照您想要的方式对齐。或者甚至一个向左浮动,另一个向右浮动也可能有效。

Try floating both of the panels? As of right now only the left one is floated... try floating both of them to the left and then putting the correct amount of margin between them to line them up like you want them. Or even floating one left and the other right would probably work.

百合的盛世恋 2024-12-25 18:38:49

将其添加到您的 CSS 中,

div.clear-both {clear: both;}

并将您的 HTML 更改为:

<div class="page">
  <div id="lefty" class="leftPanel">
    <div class="page"
      <p>helloworld</p>
    </div>
  </div>
  <div id="righty" class="rightPanel">
    <div class="page">
       <p>HELLO WORLD</p>
    </div>
  </div>    
  <div class="clear-both"></div>
</div>

Add this to your CSS,

div.clear-both {clear: both;}

And change your HTML to this:

<div class="page">
  <div id="lefty" class="leftPanel">
    <div class="page"
      <p>helloworld</p>
    </div>
  </div>
  <div id="righty" class="rightPanel">
    <div class="page">
       <p>HELLO WORLD</p>
    </div>
  </div>    
  <div class="clear-both"></div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文