当前位置:网站首页>Overflow: the combination of auto and Felx

Overflow: the combination of auto and Felx

2022-07-04 16:45:00 Elephants and ants

overflow:auto; If the content is trimmed , The browser will display a scroll bar , To see the rest .
flex Properties in
display: flex;
flex-direction: column; The principal axis is perpendicular , The starting point is at the top .

1、overflow and flex Layout collocation

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>overflow:auto Usage of </title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <link rel="stylesheet" type="text/css" href="css/reset.css" />

    <style type="text/css">
        html,body{
    
            width: 100%;
            height: 100%;
        }
        .container{
    
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
        }
        .header{
    
            width: 100%;
            height: 100px;
            background: #f99;
        }
        .content{
    
            width: 100%;
            height: 100%;
            overflow: auto;
            background: yellow;
            flex: 1;
        }
        .footer{
    
            width: 100%;
            height: 100px;
            background: #99f;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">

        </div>
        <div class="content">
            <ul>
                <li>111111</li>
                <li>111111</li>
                <li>111111</li>
                <li>111111</li>
                <li>111111</li>
                <li>111111</li>
                <li>111111</li>
                <li>111111</li>
                 there li Write more , This will show the effect , I'm here to save space .
            </ul>
        </div>

        <div class="footer">
        </div>
    </div>
</body>
</html>

To achieve overflow: auto; This effect , First, layout , Then write the style .
The outermost parent box in the style container, Add the following style :

.container{
    
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
}

Also, you must give html and body Give width and height 100%;

html,body{
    
    width: 100%;
    height: 100%;
}

The head and bottom are given a fixed height , General app The head and bottom of the are fixed , Like wechat chat record .

.header{
    
    width: 100%;
    height: 100px;
    background: #f99;
}
.footer{
    
    width: 100%;
    height: 100px;
    background: #99f;
}

In the middle of the content Given flex:1, And add our protagonist overflow:auto; Excess content is automatically cropped .

.content{
    
    width: 100%;
    height: 100%;
    overflow: auto;
    background: yellow;
    flex: 1;
    }

The renderings are as follows :
 Insert picture description here
The middle content area can slide up and down , The excess part is automatically cropped .

原网站

版权声明
本文为[Elephants and ants]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041455113208.html