Make div expand to occupy available height of parent container

前端 未结 9 387
别那么骄傲
别那么骄傲 2021-01-11 10:28

I have this code:

html:

short

Lorem ipsum dolor sit amet, consec
相关标签:
9条回答
  • 2021-01-11 11:10

    Solving this problem is pretty simple with flexbox.

    By creating a column flex container the flex items stack vertically. You can then apply flex: 1 to the text box (.content) which makes it expand the full available height of the container.

    HTML

    <div id="container"><!-- container to align .tile boxes in flexbox row; 
                             (this flexbox is optional) -->
        <div class="tile">
            <h3>short</h3>
            <div class="content">Lorem ipsum dolor sit amet ... </div>
        </div>
    
        <div class="tile">
            <h3>longLongLong longLongLong longLongLong</h3>
            <div class="content">Lorem ipsum dolor sit amet ... </div>
        </div>
    
    </div><!-- end #container -->
    

    CSS

    #container {
        display: flex; /* establish flex container; 
                          aligns flex items (child elements) in a row by default; */        
    }   
    
    .tile {
        display: flex; /* establish (nested) flex container */
        flex-direction: column; /* override default row alignment */
        height: 300px;
        width: 200px;
        background: #cecece;
        /* float: left; */
        margin: 0 10px 0 0;
    }
    
    .tile h3 {
        min-height: 20px;
        max-height: 61px;
        /* display: inline-block; */
        overflow: hidden;
    }
    
    .tile .content {
        flex: 1; /* tells flex item to use all available vertical space in container */
        height: 162px;
        overflow: hidden;
        margin: 0 10px 40px 10px; /* added bottom margin for spacing from container edge */
    }
    

    DEMO

    Note that flexbox is supported by all major browsers, except IE 8 & 9. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add all the prefixes you need, use Autoprefixer. More browser compatibility details in this answer.


    Using Ellipsis (...)

    If you want to apply ellipsis to a single line of text, CSS makes that somewhat easy with the text-overflow property. It's still a bit tricky (due to all the requirements), but text-overflow makes it possible and reliable.

    If, however, you want to use ellipsis on multi-line text – as would be the case here – then don't expect to have any fun. CSS doesn't provide a single property for doing this, and the workarounds are hit and miss. For details and methods see my answer here: Applying Ellipsis to Multiline Text

    0 讨论(0)
  • 2021-01-11 11:11

    Look at this

    http://jsfiddle.net/v8Lhxk2v/4/

    playing with border-bottom and overflow:hidden on the parent element.

    .tile{
        height: 300px;
        width: 200px;
        background: #cecece;
        float: left;
        margin: 0 10px 0 0;
        border-bottom: 22px solid #cecece;
        overflow: hidden;
    }
    
    .tile h3{
        min-height: 25px;
        max-height: 61px;
        display: inline-block;
        overflow: hidden;
    }
    
    .tile .content{
        margin: 0 10px;
    }
    
    0 讨论(0)
  • 2021-01-11 11:11

    Try this use extra div with wrap. h3 & div.content tag are wrapped by extra div and some css to be change as following:

    .tile > div {
      height: calc(100% - 20px);
      margin: 10px;
      overflow: hidden;
      line-height: 22px!important;
    }
    .tile {
      height: 300px;
      width: 200px;
      background: #cecece;
      float: left;
      margin: 0 10px 0 0;
    }
    .tile h3 {
      min-height: 20px;
      display: inline-block;
      overflow: hidden;
      margin: 5px 0;
    }
    .tile .content {
      margin: 0;
    }
    <div class="tile">
      <div>
        <h3>short</h3>
        <div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing 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>
    </div>
    
    <div class="tile">
      <div>
        <h3>longLongLong longLongLong longLongLong</h3>
        <div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing 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>
    </div>

    0 讨论(0)
提交回复
热议问题