tawsif torabi
tawsif torabi

Reputation: 833

How to get rid of margin of HTML Canvas element?

I was trying to make a electronic resistor image with canvas for my next project. Now I have gave it a resistor shape, but I was not able to remove the margin.

I've tried setting padding and margin to the value 0px. But not seems to work.

My HTML/CSS Code:

.canvas-wire {
  border: 1px solid #a09898;
  background-color: #a09898;
  vertical-align: middle;
  margin: 0;
  padding: 0;
}
.canvas-film-1 {
  border-radius: 8px 0px 0px 8px;
  background: linear-gradient(#FFEB3B, #FF9800);
  vertical-align: middle;
  margin: 0;
  padding: 0;
}
.canvas-film-2 {
  border-radius: 0px 8px 8px 0px;
  background: linear-gradient(#FFEB3B, #FF9800);
  vertical-align: middle;
  margin: 0;
  padding: 0;
}
.canvas-film-mid {
  background: linear-gradient(#FFEB3B, #FF9800);
  vertical-align: middle;
  margin: 0;
  padding: 0;
}
.metal {
  position: relative;
  text-shadow: hsla(0, 0%, 40%, .5) 0 -1px 0, hsla(0, 0%, 100%, .6) 0 2px 1px;
  background-color: hsl(0, 0%, 90%);
  margin: 0;
  padding: 0;
}
.metal.linear {
  background-image: -webkit-repeating-linear-gradient(left, hsla(0, 0%, 100%, 0) 0%, hsla(0, 0%, 100%, 0) 6%, hsla(0, 0%, 100%, .1) 7.5%), -webkit-repeating-linear-gradient(left, hsla(0, 0%, 0%, 0) 0%, hsla(0, 0%, 0%, 0) 4%, hsla(0, 0%, 0%, .03) 4.5%), -webkit-repeating-linear-gradient(left, hsla(0, 0%, 100%, 0) 0%, hsla(0, 0%, 100%, 0) 1.2%, hsla(0, 0%, 100%, .15) 2.2%), linear-gradient(180deg, hsl(0, 0%, 78%) 0%, hsl(0, 0%, 90%) 47%, hsl(0, 0%, 78%) 53%, hsl(0, 0%, 70%)100%);
  margin: 0;
  padding: 0;
}
<div style="background-color: black; padding: 5px 10px; width: 900px; height:500px;">

  <canvas class="canvas-wire metal linear" width="200px" height="5px"></canvas>

  <canvas class="canvas-film-1" width="20px" height="30px"></canvas>

  <canvas id="canvas-band1" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>

  <canvas class="canvas-film-mid" width="20px" height="30px"></canvas>

  <canvas id="canvas-band2" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>

  <canvas class="canvas-film-mid" width="20px" height="30px"></canvas>

  <canvas id="canvas-band3" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>

  <canvas class="canvas-film-mid" width="20px" height="30px"></canvas>

  <canvas id="canvas-band4" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>

  <canvas class="canvas-film-2" width="20px" height="30px"></canvas>

  <canvas class="canvas-wire metal linear" width="200px" height="5px"></canvas>

</div>

Upvotes: 2

Views: 3370

Answers (2)

d_z90
d_z90

Reputation: 1263

With your current code you are seeing a margin because it is applied to the body. To solve your issue, you simply need to apply body {margin: 0}, and apply the same property for the wrapper div.

Here is the JSfiddle with the complete code.

Upvotes: 2

Sebastian Brosch
Sebastian Brosch

Reputation: 43584

The problem are the <canvas> elements. You have to set the margin-right:-4px; for the <canvas> items. See the following solution:

canvas {
  margin-right:-4px;
}
.canvas-wire{
  border:1px solid #a09898; 
  background-color: #a09898;
  vertical-align: middle;
}
.canvas-film-1{
  border-radius: 8px 0px 0px 8px;
  background: linear-gradient(#FFEB3B, #FF9800);
  vertical-align: middle;
}
.canvas-film-2{
  border-radius: 0px 8px 8px 0px;
  background: linear-gradient(#FFEB3B, #FF9800);
  vertical-align: middle;
}
.canvas-film-mid{
  background: linear-gradient(#FFEB3B, #FF9800);
  vertical-align: middle;
}
.metal {
  position: relative;
  text-shadow: hsla(0,0%,40%,.5) 0 -1px 0, hsla(0,0%,100%,.6) 0 2px 1px;
  background-color: hsl(0,0%,90%);
}
.metal.linear {
  background-image: -webkit-repeating-linear-gradient(left, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 6%, hsla(0,0%,100%, .1) 7.5%), -webkit-repeating-linear-gradient(left, hsla(0,0%, 0%,0) 0%, hsla(0,0%, 0%,0) 4%, hsla(0,0%, 0%,.03) 4.5%), -webkit-repeating-linear-gradient(left, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 1.2%, hsla(0,0%,100%,.15) 2.2%), linear-gradient(180deg, hsl(0,0%,78%) 0%, hsl(0,0%,90%) 47%, hsl(0,0%,78%) 53%, hsl(0,0%,70%)100%);
}
<div style="background-color: black; padding: 5px 10px; width: 900px; height:500px;">
  <canvas class="canvas-wire metal linear" width="200px" height="5px"></canvas>
  <canvas class="canvas-film-1" width="20px" height="30px"></canvas>
  <canvas id="canvas-band1" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>
  <canvas class="canvas-film-mid" width="20px" height="30px"></canvas>
  <canvas id="canvas-band2" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>
  <canvas class="canvas-film-mid" width="20px" height="30px"></canvas>
  <canvas id="canvas-band3" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>
  <canvas class="canvas-film-mid" width="20px" height="30px"></canvas>
  <canvas id="canvas-band4" width="7px" height="30px" style="background-color: red; vertical-align: middle;"></canvas>
  <canvas class="canvas-film-2" width="20px" height="30px"></canvas>
  <canvas class="canvas-wire metal linear" width="200px" height="5px"></canvas>
</div>

More information about this topic: https://css-tricks.com/fighting-the-space-between-inline-block-elements/

Upvotes: 3

Related Questions