how to size a div's height to its container height, using CSS?

后端 未结 14 1968
太阳男子
太阳男子 2020-12-16 19:34

How to size a div\'s height to its container height, using CSS ?


  
相关标签:
14条回答
  • 2020-12-16 19:43

    It seems like you are trying to get equal height columns. You could use the fauxcolumns method where a background image is used to fake the equal height. There are other methods out there.

    0 讨论(0)
  • 2020-12-16 19:44

    i use the overflow:hidden it work properly.

    .bu {
        overflow: hidden;
        background-color:blue;
    }
    <div class="bu">
        <button>english</button>
    </div>

    0 讨论(0)
  • 2020-12-16 19:46

    I know this was answered forever ago, but when I run into this issue nowadays, I use Flex Box. It's awesome. See A Complete Guide to Flexbox by Chris Coyier

    .parent {
      display: -webkit-box;
      display: -moz-box;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      width: 100%;
    }
    .child {
      flex-grow: 1;
    }
    .child1 {
      min-height: 200px;
      background-color: #fee;
    }
    .child2 {
      background-color:#eef;
    }
    <div class="parent">
      <div class="child child1">Child 1</div>
      <div class="child child2">Child 2</div>
    </div>

    The Flexbox Layout (Flexible Box) module aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word "flex").

    The main idea behind the flex layout is to give the container the ability to alter its items' width/height (and order) to best fill the available space (mostly to accommodate to all kind of display devices and screen sizes). A flex container expands items to fill available free space, or shrinks them to prevent overflow.

    Most importantly, the flexbox layout is direction-agnostic as opposed to the regular layouts (block which is vertically-based and inline which is horizontally-based). While those work well for pages, they lack flexibility (no pun intended) to support large or complex applications (especially when it comes to orientation changing, resizing, stretching, shrinking, etc.).

    0 讨论(0)
  • 2020-12-16 19:49
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <style type="text/css">
    .container{
        position:relative;
        background-color:#999;
    }
    #to-be-sized{
        position:absolute;
        top:0;
        height:100%;
        background-color:#ffffd;
    }
    </style>
    <body>
    
    <div class='container'>
        <br>
        &nbsp;&nbsp;
        <div style='display: block; height: 500px'>left</div>
        <br>
        &nbsp;&nbsp;
        <div id='to-be-sized' >right</div><br>
    </div>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-16 19:50

    Sample code, you need to start from the html element so you can make use of the flexible height in the containers.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title>100% Test</title>
     <style type="text/css">
    
       html, body, #inner { height: 100% }
       #inner { border: 4px blue solid } 
       #container { height: 200px; border: 4px red solid }
    
     </style>
    </head>
    <body>
    
        <div id="container">
            <div id="inner">
                lorem ipsum
            </div>
        </div>
    
    </body>
    </html>
    0 讨论(0)
  • 2020-12-16 19:51

    It's a tricky thing to do--there's no clear-cut best approach, but there are a few common ones. If we assume that what you REALLY need is for the height of the right column to be (or appear to be) equivalent to the height of the left column, you can use any of the techniques frequently used to get equal height columns. This piece contains a few tricks to get the right look and behavior. I recommend reading it to see if it solves your problem.

    The other approach uses Javascript to determine the height of the container, and setting your right-hand column to that. That technique has been discussed on SO here. As long as your container's size is not the only thing determining the size of your outer container, that should be a valid approach (if that's not the case, you'll have a chicken-egg problem that could cause weird behavior).

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