Search code examples
htmlcssmargins

How to get rid of whitespace/margin between 2 divs?


Im in the process of making a sort of meme/troll website that I am messing around with and kind of applying everything i learned so far in HTML/CSS, but I have come across a problem that I can not figure out. In the picture below there is a block element with "Article" in it and then a picture above that. But between the below space and the picture at the top there is a whitespace between the two. Ive tried the following: using * to make all the margins 0 on the page, getting rid of the margin on the top pic and the bottom section but both seem to do nothing. Is this just something I have to deal with or is there a way around it?

https://i.sstatic.net/l45oQ.png

code below:

<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>Document</title>
    <style type="text/css">
        *{
            margin: 0;
        }
        #header{
            width: 100%;
            height: 18rem;
            margin: 0;
        }
        .dropdown1{
            display: none;
        }
        .navbar ul li:hover .dropdown1{
            display: inline-flex;

        }
        .navbar{
            background-color: #7E7B7B;
            margin: 0;
        }
        
        

    </style>
</head>
<body>
    <img id="header" src="http://tencowchick.com/wp-content/uploads/2011/03/2011-03-09-001.jpg" alt="header">
    <div class="navbar">
    <ul>
        <li>
            <a href="#">Artices</a>

            <div class="dropdown1">
                <ul>
                    <li>John Cena Dies After Tragic Accident</li>
                    <li>PewDiePie Dies After T-Series Assasination</li>
                    <li>Will Smith's Wife Kills Him After Grammy's Incident!</li>
                </ul>
            </div>
        </li>
    </ul>
    </div>
</div>
    

</body>
</html>```

[![pictare][1]][1]


  [1]: https://i.sstatic.net/l45oQ.png

Solution

  • add display:block to your image

    img{
    display:block;
    }
    <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>Document</title>
        <style type="text/css">
            *{
                margin: 0;
            }
            #header{
                width: 100%;
                height: 18rem;
                margin: 0;
            }
            .dropdown1{
                display: none;
            }
            .navbar ul li:hover .dropdown1{
                display: inline-flex;
    
            }
            .navbar{
                background-color: #7E7B7B;
                margin: 0;
            }
            
            
    
        </style>
    </head>
    <body>
        <img id="header" src="http://tencowchick.com/wp-content/uploads/2011/03/2011-03-09-001.jpg" alt="header"><div class="navbar">
        <ul>
            <li>
                <a href="#">Artices</a>
    
                <div class="dropdown1">
                    <ul>
                        <li>John Cena Dies After Tragic Accident</li>
                        <li>PewDiePie Dies After T-Series Assasination</li>
                        <li>Will Smith's Wife Kills Him After Grammy's Incident!</li>
                    </ul>
                </div>
            </li>
        </ul>
        </div>
    
        
    
    </body>
    </html>