将Min-vh-100设置为Div的,已经在Min-VH-100的尸体中

发布于 2025-02-09 21:53:57 字数 2488 浏览 0 评论 0原文

我试图将Div的最小高度设置为容器的100%或100VH。

但是:
设置最小值:100% div不起作用,
将类min-vh-100添加到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>

这就是我想要的:

”预期DIV

<!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/[email protected]/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>

I am trying to set a DIV's min-height to 100% or 100vh of the container.

But:
setting min-height:100% to the DIV isn't working and
adding class min-vh-100to the DIV is giving it's height more than the parent 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/>
    © 2022 Bootstrap
</footer>

</body>

This is what I want :

expected div min-vh-100

<!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/[email protected]/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/>
    © 2022 Bootstrap
</footer>

</body>

</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2025-02-16 21:53:57

如果没有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/[email protected]/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 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/>
    © 2022 Bootstrap
</footer>

</body>
</html>

I can't seem to solve this without the use of 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/[email protected]/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 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/>
    © 2022 Bootstrap
</footer>

</body>
</html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文