0

I made the following code but the two DIV blocks have a blank space between them.

<!DOCTYPE html>
<html>
    <body>
        <div style="background:blue; width: 200px; height: 200px">
        <p>123</p>
        </div>

        <div style="background: yellow; width: 200px; height: 200px">
        <p>456</p>
    </div>
    </body>
</html>


enter image description here




I would like to have a code where there is no space between the two blocks. How do I do that?

pr0grammar
  • 107
  • 10
  • Browsers have default stylings for everything. You should add a [CSS reset](https://stackoverflow.com/questions/11578819/css-reset-what-exactly-does-it-do) – Peter Krebs Aug 06 '21 at 12:38

3 Answers3

4

<html>
    <body style="display:flex; flex-direction: column">
        <div style="background:blue; width: 200px; height: 200px">
        <p>123</p>
        </div>

        <div style="background: yellow; width: 200px; height: 200px">
        <p>456</p>
    </div>
    </body>
</html>
Community
  • 1
  • 1
Shafqat Jamil Khan
  • 1,039
  • 1
  • 9
  • 17
2

You can normalize the CSS and define margin: 0:

* {
  margin: 0;
}
<!DOCTYPE html>
<html>
    <body>
        <div style="background:blue; width: 200px; height: 200px">
        <p>123</p>
        </div>

        <div style="background: yellow; width: 200px; height: 200px">
        <p>456</p>
    </div>
    </body>
</html>
biberman
  • 5,606
  • 4
  • 11
  • 35
2

The gap issue is coming because of the default margin applied for the p tag. The * selector will target all elements. So it is better you can add class for p tag and apply your styles.

p.no-margin {
  margin: 0px;
}
<div style="background:blue; width: 200px; height: 200px">
  <p class="no-margin">123</p>
</div>

<div style="background: yellow; width: 200px; height: 200px">
  <p class="no-margin">456</p>
</div>
biberman
  • 5,606
  • 4
  • 11
  • 35
Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54