Bootstrap 4 Scrollable Div占据屏幕中间的剩余高度

问题描述 投票:0回答:1

[我正在尝试使用Bootstrap 4设计一个6格的页面,并且我需要DIV#4占据剩余的高度并且可以滚动。

1-3细分将略有动态,而不是固定的高度。第5和第6分部将是相同的。 Div 4应该占据屏幕的其余部分,但是内容可能更长,因此需要滚动。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name=viewport content="width=device-width,initial-scale=1">
    <meta name="apple-mobile-web-app-capable" content="yes" />

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" crossorigin="anonymous"></script>

    <style>
        html, body {
            height: 100%;
            margin: 0px;
        }
    </style>
</head>
<body>
<div class="d-flex flex-column h-100">
    <div style="background-color: #000000; width:100%; margin: 0 auto; color: white;">
        VARIABLE CONTENT
    </div>

    <div style="background-color: blue; width:100%; margin: 0 auto;">
        <table width="100%">
            <tr>
                <td style="color: white;">Firstname LastName</td>
                <form name="RankingForm" id="RankingForm">
                    <td>
                        <select name="Ranking" id="Ranking">
                            <option>
                        </select>
                    </td>
                </form>
            </tr>
        </table>
    </div>

    <div style="background-color: cyan; width:100%; margin: 0 auto;" class="flex-grow-0">
        CONTENT
    </div>

    <div class="flex-grow-1" style="width:100%; overflow:scroll; margin: 0 auto;">
        <div style="width:100%;">
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
            <p>VARIABLE CONTENT THAT COULD BE LONG
        </div>
    </div>

    <div class="w-100" style="background-color: #d0d0d0; margin: 0 auto;">
        CONTENT
    </div>
</body>
</html>

任何帮助将不胜感激。

html bootstrap-4 scrollable
1个回答
0
投票

正如评论所建议的,flexbox是您想要制作三明治风格的东西。但是,我相信它是在4.1之后引入的。因此,此解决方案仅在您的引导程序版本> = 4.1时有效。

您在代码中犯了一些错误。您正在使用内联样式作为类,例如将class="flex-grow: 0"放入style="flex-grow: 0"class="flex-grow-0"

要保持最后一个<div>或您想要的任何<div>的固定高度,您可以给它一个height: somenumberhere pxheight: somenumberhere vh

<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


    <style>
        html, body {
            height: 100%;
        }
    </style>
</head>

<body>
<div class="d-flex flex-column h-100">

    <div style="background-color: #000000; width:100%; margin: 0 auto; color: white;">
        VARIABLE CONTENT
    </div>

    <div style="background-color: blue; width:100%; margin: 0 auto;">
        <table width="100%">
            <tr>
                <td style="color: white;">Firstname LastName</td>
                <form name="RankingForm" id="RankingForm">
                    <td>
                        <select name="Ranking" id="Ranking">
                            <option>
                        </select>
                    </td>
                </form>
            </tr>
        </table>
    </div>

    <div style="background-color: cyan; width:100%; margin: 0 auto;" class="flex-grow: 0">
        CONTENT
    </div>

    <div class="flex-grow-1" style="width:100%; overflow:scroll; margin: 0 auto;">
        <div style="width:100%;">
            VARIABLE CONTENT THAT COULD BE LONG
        </div>
    </div>

    <div class="w-100" style="background-color: #d0d0d0; margin: 0 auto;">
        CONTENT
    </div>
</body>
© www.soinside.com 2019 - 2024. All rights reserved.