【问题标题】:How to have two vertical divs one next to the other, with the right one overflowing basing on the the other's height?如何让两个垂直 div 一个挨着另一个,右边的一个溢出基于另一个的高度?
【发布时间】:2012-10-15 02:33:14
【问题描述】:

我有两个 div,这两个 div 必须并排排列。基本上情况如下:

<div>
   <div id="divl" style="width:25%;height:100%;float:left;clear:none">Sidemenu</div>
   <div id="divr" style="width:75%;height:100%;clear:none">Content</div>
</div>

好吧,我想要发生的事情如下。我可以将它们并排放置,但我有一个问题。右侧 div 的高度应取决于左侧。我的意思是右 div 的内容不应导致右 div 的高度高于左 div。右侧 div 内容溢出。

总高度应该由左边的div决定,右边的应该随之调整。

就是这样!这个怎么做?普通的xhtml是可能的吗?还是我需要 jQuery?谢谢

【问题讨论】:

  • 如何在不使用 floats 的情况下将这些并排放置
  • 我改了,我打字的时候忘记了...

标签: jquery css html xhtml


【解决方案1】:

作为替代方案,这很容易使用 jQuery 实现:

示例标记

  <div id="navigation">
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
      <li>Item 5</li>
    </ul>
  </div>

  <div id="content">Page content here!</div>

jquery

$('#content').height($('#navigation').height());

编辑

确保将上面的jQuery放入DOM就绪函数中,否则代码永远不会被执行:

$(document).ready(function() {
  $('#content').height($('#navigation').height());
});

Here's a fiddle

【讨论】:

  • 这不起作用,因为我的内容 div 下降到高度 0!可能是因为它不是动态计算的高度...
  • 降到0是什么意思?该示例只是将content 的高度设置为与navigation 的高度相同
  • 啊...导航...对不起,我使用了其他 div 的高度...现在有意义,尝试一下...
  • 您给divs 提供了相关的ids,另请参阅我的编辑
  • 我执行以下操作:$('#divr').height($(#divl).height()),正如我告诉你的那样,divr 消失了...我将这段代码放在关闭 &lt;body&gt; 之前,所以就在 &lt;/body&gt; 之前...
【解决方案2】:

Here 是一些很好的方法,以及它们的各种demos。这是取自 CSS-tricks 的示例:

<div id="one-true" class="group">
    <div class="col"><h3>I am listed first in source order.</h3><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p></div>
    <div class="col"><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p></div>
    <div class="col"><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p></div>
</div>

#one-true { overflow: hidden; }
#one-true .col {
    width: 27%;
    padding: 30px 3.15% 0; 
    float: left;
    margin-bottom: -99999px;
    padding-bottom: 99999px;
}
#one-true .col:nth-child(1) { margin-left: 33.3%; background: #ccc; }
#one-true .col:nth-child(2) { margin-left: -66.3%; background: #eee; }
#one-true .col:nth-child(3) { left: 0; background: #eee; }
#one-true p { margin-bottom: 30px; } /* Bottom padding on col is busy */

【讨论】:

  • 我正在检查,请给我一些时间:)
【解决方案3】:

如果您只是控制父 div 的高度怎么办?这将通过仅控制父 div 的高度来匹配两个 div 的高度。

http://jsfiddle.net/ccLBy/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-31
    • 2011-05-02
    • 2012-12-03
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    相关资源
    最近更新 更多