1

我正在尝试制作 2 个具有相同高度的 div:

http://image.prntscr.com/image/32ca3f340fba4e2dbd3c7d1e434f0938.png

<div class="parent">
  <div class="small">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit
  </div>
  <div class="large">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit<br>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit<br>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit<br>
  </div>
</div>

例子

.parent {
  background: red;
}
.small {
  background: blue;
  float: left;
  width: 65%
}
.large {
  background: green;
  float: right;
  width: 35%
}
.clear {
  clear: both
}
<div class="parent">
  <div class="small">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit
  </div>
  <div class="large">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit
    <br>Lorem ipsum dolor sit amet, consectetur adipiscing elit
    <br>Lorem ipsum dolor sit amet, consectetur adipiscing elit
    <br>
  </div>
  <div class="clear">
  </div>
</div>

我希望动态(大一)div 与静态(小一)div 具有相同的高度。动态 div 总是比静态 div 长。我必须用css(没有jquery)来做。我已经找到了几种如何使静态 div 与动态一样大的解决方案(例如 table 或 flex),但我找不到任何解决方案如何使动态与静态一样小。

CSS - 等高列?- 不是答案。这里的柱子都和最长的柱子一样大。

再一次:我需要使动态 div静态一样小。动态 div 应该有滚动条。

另一个限制 -父级、静态和动态 div高度/宽度不应设置为 px 中的某个永久值

请你帮助我好吗?

4

2 回答 2

1

这是诀窍。

  1. 添加position: relative到父级。
  2. 将您的动态 div 元素(高度较大)position: absolute放置在父级内部。

为此将需要以下css:

.parent {
  position: relative;
}
.large-div {
  position: absolute;
  overflow: auto;
  left: 300px;
  bottom: 0;
  top: 0;
}

注意:您可以根据需要更改left,bottomtop值。

.container {
  border: 1px solid #000;
  margin-bottom: 20px;
  position: relative;
  padding: 10px;
}

.container div {
  border: 1px solid #000;
  padding: 10px;
  width: 250px;
}
.container div p {
  margin: 0;
}
.large {
  position: absolute;
  overflow: auto;
  bottom: 10px;
  left: 300px;
  top: 10px;
}
<div class="container">
  <div class="small">
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
  </div>
  <div class="large">
    <p>
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet...
     </p>
  </div>
</div>

<div class="container">
  <div class="small">
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
  </div>
  <div class="large">
    <p>
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet...Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
      Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... Lorem ipsum dolor sit amet... 
     </p>
  </div>
</div>

于 2016-12-01T14:23:11.810 回答
0

如果我理解正确,有一个固定高度的 div,例如 150px 有固定内容,还有一个动态 div 可以有很多内容。

最好的办法是为两者设置一个高度,然后将其添加到动态 div:

overflow-y: scroll

.parent-div div {
  width: 50%;
  padding: 15px;
  box-sizing: border-box;
  float: left;
  height: 150px;
  border: solid 1px grey;
}
.dynamic-height {
  overflow-y: scroll;
}
<div class="parent-div">
  <div class="fixed-height">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
  <div class="dynamic-height">
    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
  </div>
</div>

于 2016-12-01T14:22:57.180 回答