Search code examples
htmlcsssvg

Issue with spacing between SVG and div elements when arranging them vertically


I'm trying to arrange SVG and div elements vertically within a container, but I'm encountering an unexpected spacing issue between them. I can't seem to figure out where this spacing is coming from.

Here's a simplified version of my HTML structure:

<!doctype html>
<html>
  <head>
    <title>This is the title of the webpage!</title>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
  </head>
  <style>
    *{
        margin: 0;
        top: 0;
    }
  </style>
  <body>
    <div style="width: 100vw; height: 100vh; text-align: center; margin-top: 2em;">
        <div style="width: 600px; min-height: 20px; background: red; margin: auto;">
            <svg viewBox="0 0 600 60" width="600px" height="60px" xmlns="http://www.w3.org/2000/svg" xmlns:bx="https://boxy-svg.com">
                <path style="fill: rgb(216, 216, 216); stroke: rgb(0, 0, 0); stroke-width: 0px;" d="M 30 0 H 570 A 30 30 0 0 1 600 30 V 60 H 0 V 30 A 30 30 0 0 1 30 0 Z" />
              </svg>
            <div style="width: 100%; height: 60px; background: #d6d6d6;"></div>
        </div>
    </div>
  </body>
</html>

And here my screenshoot. enter image description here

How to remove the spacing between the two elements?


Solution

  • This is because your <svg> is an inline element (its default) but in this case you want it to be a block element.

    If you apply the CSS display: block; to the <svg>, then the gap will disappear.