HTML5 flexible box model height calculation

后端 未结 3 1174
走了就别回头了
走了就别回头了 2020-11-30 02:21

after researching the flexible box model for a whole day, I must say I really like it. It implements the functionality I implement in JavaScript in a fast and clean way. One

相关标签:
3条回答
  • 2020-11-30 03:06

    since there is some interest in this question, I will give you the current solution I found. It's test in chrome (no other browser currently working with this sample, but got another bigger on working in FF which somehow doesn't work when I "simplyfy it"...).

    The idea behind it is, to somehow make the element nested inside the flexible box extend to the full size of the flexible box element. To do so an absolut position element is added inside, which has 0 distance to the left, right, top and bottom.

    However, even in Chrome, there is an error, when downsizeing and resizing the element so that the scrollbar disapears.

    Here is the html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>flexbox example - 2 column layout</title>
        <meta name="author" content="Gwilym Johnston">
        <style type="text/css">   
        html, body{
            height: 100%;
            margin: 0px;
        }
        #box-orient-example {
            -moz-box-orient: horizontal;
            -webkit-box-orient: horizontal;
            -ms-box-orient: horizontal;
            box-orient: horizontal;
            display: -moz-box;
            display: -webkit-box;
            display: -ms-box;
            display: box;
            height: 100%;
            overflow: auto;
        }
        #box1 {
            -moz-box-flex: 1;
            -webkit-box-flex: 1;
            -ms-box-flex: 1;
            background: none repeat scroll 0 0 lightblue;
            text-align: center;
            overflow: auto;
            position: relative;
        }
        #box2 {
            -moz-box-flex: 1;
            -webkit-box-flex: 1;
            -ms-box-flex: 1;
            background: none repeat scroll 0 0 #DDDDDD;
            text-align: center;
            overflow: auto;
            position: relative;
        }
    
        .abs {
            position: absolute;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
        }
        .rel{
            position: relative;
        }
        </style>
    </head>
    <body>
        <div id="box-orient-example" class="example">
            <div id="box1">
                <div class="abs">
                    <div class="rel"> 
                        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   
                    </div>
                </div>
            </div>
            <div id="box2">
                <div class="abs">
                    <div class="rel"> 
                        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   
                        Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.   
                        Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.   
                </div>
            </div>
        </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-30 03:12

    I've been wrestling with this myself, but have finally managed to come up with a solution.

    See this jsFiddle, although I have only added webkit prefixes so open in Chrome.

    You basically have 2 issues which I will deal with separately.

    1. Getting the child of a flex-item to fill height 100%
      • Set position:relative; on the parent of the child.
      • Set position:absolute; on the child.
      • You can then set width/height as required (100% in my sample).
    2. Fixing the resize scrolling "quirk" in Chrome
      • Put overflow-y:auto; on the scrollable div.
      • The scrollable div must have an explicit height specified. My sample already has height 100% but if none is already applied you can specify height:0;

    See this answer for more information on the scrolling issue.

    0 讨论(0)
  • 2020-11-30 03:23

    You must also make the div you want to expand a flex-box as well and add a flex value. This fixes the problem.

    #fullsize{
        background-color: red;
    
        display: -webkit-box;
        display: box;
        display: -moz-box;
    
        box-flex:1;
        -webkit-box-flex:1;
        -moz-box-flex:1;
    }
    
    0 讨论(0)
提交回复
热议问题