Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Giving the html/body a min height prevents elements from having a percentages as height

Problem
I’ve given a child element (id=main) of the body a height percentage, but it doesn’t seem to do anything.

I have labelled the problematic CSS code.

Code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

*{
    margin:0;
    padding:0;
    background-color: black;
    color: white;
    border-radius: 10px;
}

/* Problematic Code!------------------------------------------------------------- */

html, body {
   min-height: 100%;

  }
/* ---------------------------------------------------*/

/* Header */
header{
    height: 80px;
    display: flex;
    align-items: center;
}

/* Main section */
#main{
    width: 100%;
    height: 80%;
    background-color: rgb(12, 12, 12);
    display: flex;
    justify-content: center;
}
#main #center_piece{
    background-color: rgb(12, 12, 12);
    height: 80%;
}
<!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.0">
    <title>Forum</title>

    <link rel="stylesheet" href="index.css">
</head>
<body>
    <header>
        <h1>Forum (v0.3)</h1>
    </header>

    <section id="main">

        <section id="center_piece">

            <div id="middle_column">

                <div id="title">Posts</div>
                <div id="posts_section">

                    <div class="post">
                        <span>Username</span>
                        <p>Comment body</p>
                    </div>

                </div>
                <div id="input_area">
                    <form>
                        <textarea cols="" rows=""></textarea>
                        <span>Submit</span>
                        <span>Refresh</span>
                    </form>
                </div>

            </div>

        </section>

    </section>
    
</body>
</html>

Solutions that don’t work

  • When I remove the min-height and give both the html and the body a height of 100%, the problem goes away, but then I won’t get the layout I need.
  • Changing the display of the body to grid also solves the problem but the layout changes, so I’d prefer if I didn’t have to do that.
  • I removed the min-height from the html and gave it a height of 100%; this didn’t do anything.

Thank you in advance.

>Solution :

you can use viewport height vh instead of percentage.

*{
    margin:0;
    padding:0;
    background-color: black;
    color: white;
    border-radius: 10px;
}

/* Problematic Code!------------------------------------------------------------- */

html, body {
   min-height: 100%;

  }
/* ---------------------------------------------------*/

/* Header */
header{
    height: 80px;
    display: flex;
    align-items: center;
}

/* Main section */
#main{
    width: 100%;
    height: 80vh;
    background-color: rgb(12, 12, 12);
    display: flex;
    justify-content: center;
}
#main #center_piece{
    background-color: rgb(12, 12, 12);
    height: 80%;
}
<!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.0">
    <title>Forum</title>

    <link rel="stylesheet" href="index.css">
</head>
<body>
    <header>
        <h1>Forum (v0.3)</h1>
    </header>

    <section id="main">

        <section id="center_piece">

            <div id="middle_column">

                <div id="title">Posts</div>
                <div id="posts_section">

                    <div class="post">
                        <span>Username</span>
                        <p>Comment body</p>
                    </div>

                </div>
                <div id="input_area">
                    <form>
                        <textarea cols="" rows=""></textarea>
                        <span>Submit</span>
                        <span>Refresh</span>
                    </form>
                </div>

            </div>

        </section>

    </section>
    
</body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading