【问题标题】:Setting min-vh-100 to a DIV's that's already in a BODY of min-vh-100将 min-vh-100 设置为已经在 min-vh-100 的 BODY 中的 DIV
【发布时间】:2022-06-24 13:33:11
【问题描述】:

我正在尝试将 DIV 的最小高度设置为容器的 100% 或 100vh。

但是:
min-height:100% 设置为 DIV 不起作用并且
min-vh-100 类添加到 DIV 使其高度高于父 DIV。

<body class="min-vh-100 d-flex flex-column bg-primary">

<header class="p-3 bg-success">
    Header<br/>
    Content
</header>

<div class="container flex-grow-1 p-0 bg-secondary">

    <div class="d-flex flex-column"><!-- What do I have to do here to set this DIV's height to 100% of parent DIV ? -->

        <div class="flex-grow-1 p-3 bg-danger">
            Main Content that's need to fill up vertical space
        </div>
        
        <div class="p-3 bg-warning">
            Bottom Content for Main
        </div>

    </div>

</div>
  
<footer class="p-3 bg-info">
    Footer<br/>
    Content<br/>
    &copy; 2022 Bootstrap
</footer>

</body>

这就是我想要的:

<!doctype html>
<html lang="en">
<head>    
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Footer at Bottom when content is not page-full</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>

<body class="min-vh-100 d-flex flex-column bg-primary">

<header class="p-3 bg-success">
    Header<br/>
    Content
</header>

<div class="container flex-grow-1 p-0 bg-secondary">

    <div class="d-flex flex-column">

        <div class="flex-grow-1 p-3 bg-danger">
            Main Content
        </div>
        
        <div class="p-3 bg-warning">
            Bottom Content for Main
        </div>

    </div>

</div>
  
<footer class="p-3 bg-info">
    Footer<br/>
    Content<br/>
    &copy; 2022 Bootstrap
</footer>

</body>

</html>

【问题讨论】:

    标签: flexbox bootstrap-5


    【解决方案1】:

    我似乎无法解决这个问题没有使用 JavaScript。

    <!doctype html>
    <html lang="en">
    <head>    
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>flex child as a flex container with 100% height of parent flex container</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script>
    document.addEventListener("DOMContentLoaded", () =>
    {
        let body = document.body;
        let html = document.documentElement;
    
        let body_height = Math.max( body.scrollHeight, body.offsetHeight,  html.clientHeight, html.scrollHeight, html.offsetHeight );
        let header_height = document.getElementsByTagName('header')[0].offsetHeight;
        let footer_height = document.getElementsByTagName('footer')[0].offsetHeight;
    
        document.getElementsByTagName('main')[0].style.height = (body_height - (header_height + footer_height)) + "px";
    
    });
    </script>
    </head>
    <body>
    
    <div class="min-vh-100 d-flex flex-column bg-primary">
    
    <header class="p-3 bg-success">
        Header<br/>
        Content
    </header>
    
    <main class="container p-0 d-flex flex-column">
    
        <div class="flex-grow-1 p-3 bg-danger">        
            Main Content
        </div>
        
        <div class="p-3 bg-warning">
            Bottom Content for Main
        </div>
    
    </main>
    
      
    <footer class="p-3 bg-info">
        Footer<br/>
        Content<br/>
        &copy; 2022 Bootstrap
    </footer>
    
    </div>
    
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-07
      • 2017-07-26
      • 2018-06-04
      • 2018-04-22
      • 1970-01-01
      • 2020-10-12
      • 2020-12-21
      • 1970-01-01
      相关资源
      最近更新 更多