Ilan Keshet
Ilan Keshet

Reputation: 604

Removing unwanted padding/margin/border in table of images HTML

I am trying to create a table of images -- with all cells width and height the same size.

For some reason there is some odd margin / padding added in final result, even though I've set all padding / margin / border to 0.

The dotted red around the images / gold border of table -- is just to show what there is stuff outside of the border that i am trying to get rid of.

HTML:
<table align="center" class="gridStyle">
  <tr><td><img src="~/Images/Heart.png" /></td><td><img src="~/Images/Heart.png" /></td></tr>
  <tr><td><img src="~/Images/Heart.png" /></td><td><img src="~/Images/Heart.png" /></td></tr>
</table>


CSS:
.gridStyle
{
  border: 2px solid gold;
  max-width: 50%;
  margin: 0;
  padding: 0;
}

  .gridStyle th, .gameDisplay td, .gameDisplay tr
  {
    border: 0;
    margin: 0;
    padding: 0;
    border-collapse: collapse;
  }

  .gridStyle img
  {
    width: auto;
    max-width: 100%;
    max-height: 100%;
    height: auto;
    margin: 0;
    padding: 0;
    border: 1px dotted red;
  }

Image with unnecessary spacing

Upvotes: 1

Views: 1520

Answers (2)

Ritesh Khandekar
Ritesh Khandekar

Reputation: 4015

Try to remove cellspacing & cellpadding:

<table align="center" class="gridStyle" cellpadding="0" cellspacing="0">...</table>

Upvotes: 3

Hiren Vaghasiya
Hiren Vaghasiya

Reputation: 5544

Add border-spacing:0 , border-collapse:collapse and img to display:block

table { 
    border-spacing: 0;
    border-collapse: collapse;
}
td {
    padding:0px;
}
td img {
    display:block;
}

.gridStyle
{
  border: 2px solid gold;
  max-width: 50%;
  margin: 0;
  padding: 0;
}

.gridStyle th, .gameDisplay td, .gameDisplay tr
{
  border: 0;
  margin: 0;
  padding: 0;
  border-collapse: collapse;
}

.gridStyle img
{
  width: auto;
  max-width: 100%;
  max-height: 100%;
  height: auto;
  margin: 0;
  padding: 0;
  border: 1px dotted red;
}
<table align="center" class="gridStyle">
  <tr><td><img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /></td><td><img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /></td></tr>
  <tr><td><img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /></td><td><img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /></td></tr>
</table>

Upvotes: 0

Related Questions