2

I try to render a table with as litte text cell padding above and below as possible.

The snippet below shows what I have done so far. However it still contains visible top and bottom "padding" for the cell text.

What other CSS options exist to reduce this white space above the text, so that the text is rendered just below the top line, and the bottom line is drawn just below the text without a gap?

td {
  border: 1px solid silver;
  line-height: 1;
}

table {
  border-collapse: collapse;
}
<!DOCTYPE html>
<html>

<head>
  <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Table Test</title>
</head>

<body>
  <table>
    <tr>
      <td>
        First row
      </td>
    </tr>
    <tr>
      <td>
        Second row
      </td>
    </tr>
  </table>
</body>

</html>

Many thanks for the answers so far!

In my case, the actual font families and font sizes are unknown, this means that the CSS can't use any fixed values for line height as this has the risk of cut-off characters. So I am looking for a 'canonical' and safe solution.

Ideally, there is just one single CSS setting which causes the 'gap' between text and border to disappear, and works with all fonts families and font sizes (there might be more than one font used in a single cell).

mjn
  • 36,362
  • 28
  • 176
  • 378

5 Answers5

1

first make the cell font family same, here you are describing only one cell font family, that's why both cells have different line height and padding.

so make it one font family && make line height 12px;

Harikrishnan k
  • 203
  • 2
  • 8
  • Many thanks, please see my edit - font families and font sizes are unknown so there is the danger that fixed values for line height cause cut-off characters. – mjn Jun 03 '19 at 13:02
  • ok use this way td { margin:0px; padding: 0px; line-height: 12px; } so no more padding and margin, basically you need to specify the line-height in pixels – Harikrishnan k Jun 03 '19 at 13:36
0

Try this:

body,
p {
  margin: 0px;
}

td {
  border: 1px solid silver;
  padding:0; 
  margin:0;
}

table {
  border-collapse: collapse;
}

It minimizes even more the margin and the padding in the cell.

wasanga7
  • 242
  • 1
  • 3
  • 17
0

Do you tried to add a div on each row to margin-bottom them ?

Like that :

<!DOCTYPE html>
<html>

<head>
  <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Table Test</title>
</head>

<body>
  <table>
    <tr>
      <td>
        <p>
   <div class="FirstRow">
          First row
   </div>
        </p>
      </td>
    </tr>
    <tr>
      <td>
        <p>
          <div class="SecondRow">
          Second row
          </div>
        </p>
      </td>
    </tr>
  </table>
</body>

</html>

and

body,
p {
  margin: 0px;
}

td {
  border: 1px solid silver;
}

table {
  border-collapse: collapse;
}

 .FirstRow {
  margin-bottom: ...px;
}

 .SecondRow {
  margn-bottom: ...px;
}

 table {
  border-collapse: collapse;
}

?

SuRo
  • 38
  • 11
  • 1
    How would adding more elements to the HTML reduce the amount of space? Please [edit] your question to explain how this answers the OP's problem. – Heretic Monkey Jun 03 '19 at 12:37
  • Hello, i added
    element into html to edit them into css. And then add a margin-top: ...px; to reduce the padding
    – SuRo Jun 03 '19 at 12:39
0

Try this:

td {
  line-height: 10px; /* or value which suits your design */
}

How about this?:

td div{
    margin-top: -4px; /* or value which suits your design */
}

....
<td><div>Your Data</div></td>
....
maddy23285
  • 738
  • 6
  • 16
  • line-height: 1; (unitless) is the recommended way https://developer.mozilla.org/en-US/docs/Web/CSS/line-height - I don't have control over the actual font sizes so I can't use a fixed value – mjn Jun 03 '19 at 12:14
  • 1
    "Try this" is rarely a good answer. Better to explain why you are suggesting the code present in the answer. – Heretic Monkey Jun 03 '19 at 12:23
  • @Heretic Monkey , property line-height controls the space between lines of text. As we know that, by default TD get automatically resized according to its child contents. So forcing it to reduce the line space might do the trick. – maddy23285 Jun 03 '19 at 12:30
  • [Edit] your question with the explanation so the OP and future visitors know that. – Heretic Monkey Jun 03 '19 at 12:33
  • In case of
    css property margin-top accepts negative values also, so similarly you can push the div to pass through / overlap the default margin.
    – maddy23285 Jun 03 '19 at 12:33
0

the cell itself also give padding.So please give "padding:0" and "line-height:.9em"(90% of font-size).

td {
  border: 1px solid silver;
  padding:0;
  line-height:.9em;
}

body,
p {
  margin: 0;
}

td {
  border: 1px solid silver;
  vertical-align:top;
  line-height:.9em;
padding:0;

}

table {
  border-collapse: collapse;
    border: 1px solid;
   
}
<!DOCTYPE html>
<html>

<head>
  <title>Table Test</title>
</head>

<body>
  <table>
    <tr>
      <td>
        <p>
          First row
        </p>
      </td>
    </tr>
    <tr>
      <td>
        <p>
          Second row jygq
        </p>
      </td>
    </tr>
  </table>
</body>

</html>