0

I'm trying to make a div the size of its contents, but I'm dealing with a ghost line. Can someone give it a look?

        * {
            margin: 0;
            padding: 0;
        }

        div#holder {
            background-color: blue;
            display: inline-block;
        }

        canvas {
            cursor: pointer;
            background-color: gray;
        }


    <div id="holder">
        <canvas id="canvas" width="300" height="300"></canvas>
    </div>

https://jsfiddle.net/xjmz6n68/

lolol
  • 4,287
  • 3
  • 35
  • 54

5 Answers5

2

The small gap is because the container is an inline element and there is space reserved for descender elements in a font (e.g. j, g, y). Remove it easily by adding vertical-align:top to the canvas rules:

canvas {
    cursor: pointer;
    background-color: gray;
    vertical-align: top;
}

jsFiddle example

or set the font-size to zero on the div:

jsFiddle example

or float the canvas:

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
1

There are three options I found already:

add display:block; to canvas

add vertical-align:top; to canvas

set display:inline-flex; to your div#holder

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
pablito.aven
  • 1,135
  • 1
  • 11
  • 29
0

By default, a canvas is rendered inline, like a letter so you can use these solutions:

  • display:block to canvas
  • vertical-align: bottom; to canvas
  • float: left; to canvas
  • line-height: 0; to div#holder

.

Jsfiddle

Alex
  • 8,461
  • 6
  • 37
  • 49
0

give your #holder the CSS line-height: 0;

Since you're using inline, a text-like line-height is applied.

https://jsfiddle.net/svArtist/cm4r6k7p/

Ben Philipp
  • 1,832
  • 1
  • 14
  • 29
0
    canvas {
            cursor: pointer;
            background-color: gray;
    vertical-align: top;
        }

just add vertical-align: top; to canvas and that fixes it.

Yotzin Castrejon
  • 439
  • 4
  • 16