如何在纯css中将较大列的高度设置为小一?

时间:2016-12-01 14:02:13

标签: html css html5 css3 equal-heights

我正在尝试制作两个高度相同的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 - Equal Height Columns? - 不是答案。这里的列与最长的列一样大。

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

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

请你帮助我吗?

2 个答案:

答案 0 :(得分:1)

这是诀窍。

  1. 在父级上添加position: absolute
  2. 将您的动态div元素(具有较大的高度)与.parent { position: relative; } .large-div { position: absolute; overflow: auto; left: 300px; bottom: 0; top: 0; } 放在父级内。
  3. 这需要以下css:

    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>
    Sub ExportToExcel()
    
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim proj As Project
    Dim t As Task
    Dim pj As Project
    Dim pjDuration As Integer
    Dim i As Integer
    Set pj = ActiveProject
    Set xlApp = New Excel.Application
    xlApp.Visible = True
    'AppActivate "Excel"
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
    xlSheet.cells(1, 1).Value = "Project Name"
    xlSheet.cells(1, 2).Value = pj.Name
    xlSheet.cells(2, 1).Value = "Project Title"
    xlSheet.cells(2, 2).Value = pj.Title
    xlSheet.cells(1, 4).Value = "Project Start"
    xlSheet.cells(1, 5).Value = pj.ProjectStart
    xlSheet.cells(2, 4).Value = "Project Finish"
    xlSheet.cells(2, 5).Value = pj.ProjectFinish
    
    xlSheet.cells(1, 7).Value = "Project Duration"
    pjDuration = pj.ProjectFinish - pj.ProjectStart
    xlSheet.cells(1, 8).Value = pjDuration & "d"
    
    xlSheet.cells(4, 1).Value = "Task ID"
    xlSheet.cells(4, 2).Value = "Task Name"
    xlSheet.cells(4, 3).Value = "Task Start"
    xlSheet.cells(4, 4).Value = "Task Finish"
    
    ' Add day of the week headers for the entire Project's duration
    For i = 0 To pjDuration
        xlSheet.cells(4, i + 5).Value = pj.ProjectStart + i
        xlSheet.cells(4, i + 5).NumberFormat = "[$-409]d-mmm-yy;@"
    Next
    
    For Each t In pj.Tasks
        xlSheet.cells(t.ID + 4, 1).Value = t.ID
        xlSheet.cells(t.ID + 4, 2).Value = t.Name
        xlSheet.cells(t.ID + 4, 3).Value = t.Start
        xlSheet.cells(t.ID + 4, 3).NumberFormat = "[$-409]d-mmm-yy;@"
        xlSheet.cells(t.ID + 4, 4).Value = t.Finish
        xlSheet.cells(t.ID + 4, 4).NumberFormat = "[$-409]d-mmm-yy;@"
    
        For i = 5 To pjDuration + 5
            'Loop to add day of week headers and color cells to mimic Gantt chart
            If t.Start <= xlSheet.cells(4, i) And t.Finish >= xlSheet.cells(4, i) Then
                xlSheet.cells(t.ID + 4, i).Interior.ColorIndex = 37
            End If
         Next i
    Next t
    

答案 1 :(得分: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>