By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS Empty div has width and height in % but does not appear

Hi All,

I have just bumped into this case while i am doing a freecodecamp challenge.
In the freecodecamp editor the div showed per given % but when i tried to do the same in VSC i did not get the same result, so i created a little example.
Could you please tell me how it works as it works with px and vw/vh, rem etc.. but does not with %. and why?I also tried display: block as I saw that on a different forum but it does not works in this case.
Thank you in advance for your help.

<!DOCTYPE html>
<html lang="en">

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/divs.css">


    <div class="div1">


    <div class="div2"></div>



body {
    background-color: green;

.div1 {
    display: block;
    width: 100%;
    height: 60%;
    /* width: 100rem;
    height: 60rem;
    width: 100vw;
    height: 60vh; */
    background-color: black;
    color: aqua;

.div2 {
    width: 10px;
    height: 50px;
    background-color: aqua;
It's not showing because the body does not have any height. height: 60%; means 60% of the parent element's height, but the body doesn't have any height. 0 * 0.6 = 0

Could you please tell me how it works as it works with px and vw/vh, rem etc.. but does not with %. and why?
It shows with px because you're giving the element a specific height, and the body will automatically take the height of all the content inside it.

It works with vw and vh because that is viewport (the visible part of the page) width and height, so assuming you have a viewport height of 1000px, and your element has height: 20vh;, then the element's height is 20% of the viewport height which is 200px.

To fix your problem you could do what @menator01 did, or give the body a fixed height (e.g. height: 1000px;) or give it a minimum height of 100vh (min-height: 100vh;), so that the body's height is never less than the viewport height.
You could also change the unit you're using for the div elements (e.g. px, vh etc.).
Last edited:
Hi @Johna and @menator01 ,

Thank you for your kind and fast help.
It is my bad, I should have known this when % given it needs to have specific height or width of the parent element.
Just freecodecamp confused me as it worked without adding height or width to a parent element.

How professional is to give specific height/width when you create a website?
Width is ok but i heard that better to give height with padding and the website will be better in responsivness.

Thank you again.

Buy us a coffee!

Top Bottom