Jay Gray
Jay Gray

Reputation: 1726

How to modify HTML table CSS to move td left

This fiddle https://jsfiddle.net/jaygray/oy7r569v/1/ has two images: "Source" and "Target"

"Source" is a JPG (first or left depending on your monitor).

Am reconstructing Source with CSS and <table> ("Target")

The rows beginning with these terms:

are positioned too far to the right.

Following "Source", they need to be positioned under the second 't' in Total Fat and Total Carbohydrates respectively

Have hit a CSS wall and cannot figure how to move those four <td>s as desired.

How to change the CSS to move designated terms to the desired position?

Upvotes: 2

Views: 4742

Answers (1)

Zuber
Zuber

Reputation: 3473

This will help you.

  • No need to use blank-cell classes where you indent th
  • Use colspan in td which you want to align
  • use padding to indent th

.pl-15 {
  padding-left: 15px !important;
}
<!doctype html>
<html ⚡ lang='en'>
<head itemscope itemtype='https://schema.org/WebPage'>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,minimum-scale=1,initial-scale=1'>
<meta http-equiv='X-UA-Compatible' content='IE=Edge'>
<link rel='canonical' href='https://ontomatica.io/canonical.html' />
<title>
NFP CSS
</title>
<script async src='https://cdn.ampproject.org/v0.js'></script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>

.image {
    width: 250px;
    float: left;
    margin: 20px;
}
body {
    font-size: small;
    line-height: 1.4;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', Arial, sans-serif;
}
p {
    margin: 0;
}
.performance-facts {
    border: 1px solid black;
    margin: 20px;
    float: left;
    width: 280px;
    padding: 0.5rem;
}
.performance-facts table {
    border-collapse: collapse;
}
.performance-facts__title {
    font-weight: bold;
    font-size: 2rem;
    margin: 0 0 0.25rem 0;
}
.performance-facts__header {
    border-bottom: 10px solid black;
    padding: 0 0 0.25rem 0;
    margin: 0 0 0.5rem 0;
}
.performance-facts__header p {
    margin: 0;
}
.performance-facts__table,
  .performance-facts__table--small,
  .performance-facts__table--grid {
    width: 100%;
}
.performance-facts__table thead tr th,
  .performance-facts__table--small thead tr th,
  .performance-facts__table--grid thead tr th,
  .performance-facts__table thead tr td,
  .performance-facts__table--small thead tr td,
  .performance-facts__table--grid thead tr td {
    border: 0;
}
.performance-facts__table th,
  .performance-facts__table--small th,
  .performance-facts__table--grid th,
  .performance-facts__table td,
  .performance-facts__table--small td,
  .performance-facts__table--grid td {
    font-weight: normal;
    text-align: left;
    padding: 0.25rem 0;
    border-top: 1px solid black;
    white-space: nowrap;
}
.performance-facts__table td:last-child,
  .performance-facts__table--small td:last-child,
  .performance-facts__table--grid td:last-child {
    text-align: right;
}
.performance-facts__table .blank-cell,
  .performance-facts__table--small .blank-cell,
  .performance-facts__table--grid .blank-cell {
    width: 1rem;
    border-top: 0;
}
.performance-facts__table .thick-row th,
  .performance-facts__table--small .thick-row th,
  .performance-facts__table--grid .thick-row th,
  .performance-facts__table .thick-row td,
  .performance-facts__table--small .thick-row td,
  .performance-facts__table--grid .thick-row td {
    border-top-width: 5px;
}
.performance-facts__table--small {
    border-bottom: 1px solid #999;
    margin: 0 0 0.5rem 0;
}
.performance-facts__table--small thead tr {
    border-bottom: 1px solid black;
}
.performance-facts__table--small td:last-child {
    text-align: left;
}
.performance-facts__table--small th,
  .performance-facts__table--small td {
    border: 0;
    padding: 0;
}
.performance-facts__table--grid {
    margin: 0 0 0.5rem 0;
}
.performance-facts__table--grid td:last-child {
    text-align: left;
}
.performance-facts__table--grid td:last-child::before {
    content: '•';
    font-weight: bold;
    margin: 0 0.25rem 0 0;
}
.small-info {
    font-size: 0.7rem;
}
.text-center {
    text-align: center;
}
.thick-end {
    border-bottom: 10px solid black;
}
.thin-end {
    border-bottom: 1px solid black;
}
.target-img {
    font-size: 28px;
    margin-top: 20px;
    margin-left: 25px;
}
</style>
</head>
<body>

<!--
reference:
https://www.fda.gov/downloads/Food/GuidanceRegulation/GuidanceDocumentsRegulatoryInformation/LabelingNutrition/UCM513883.pdf
https://www.fda.gov/Food/GuidanceRegulation/GuidanceDocumentsRegulatoryInformation/LabelingNutrition/ucm385663.htm
font is Helvetica Black
-->

<p class='target-img'>
Source image on left. HTML-table+CSS on right.
</p>
<img src='https://s.cdpn.io/3/NutritionFacts.gif' class='image'>

<section class='performance-facts'>

<header class='performance-facts__header'>
<h1 class='performance-facts__title'>Nutrition Facts</h1>
<p>Serving Size 1/2 cup (about 82g)</p>
<p>Serving Per Container 8</p>
</header>

<table class='performance-facts__table'>

<thead>
<tr>
<th colspan='3' class='small-info'>Amount Per Serving</th>
</tr>
</thead>

<tbody>
<tr>
<th colspan='1'><b>Calories</b>&nbsp;200</th>
<!-- align right -->
<td colspan='2'>Calories from Fat 130</td>
</tr>
<tr class='thick-row'>
<td colspan='3' class='small-info'><b>% Daily Value*</b></td>
</tr>
<tr>
<th colspan='2'><b>Total Fat</b>&nbsp;14g</th>
<td><b>22%</b></td>
</tr>

<!-- problem begin -->
<tr>
<!--<td class='blank-cell'></td>-->
<th class="pl-15">Saturated Fat&nbsp;9g</th>
<td colspan="2"><b>45%</b></td>
</tr>
<tr>
<!--<td class='blank-cell'></td>-->
<th class="pl-15">Trans Fat&nbsp;0g</th>
<td colspan="2"></td>
</tr>
<!-- problem end -->

<tr>
<th colspan='2'><b>Cholesterol</b>&nbsp;55mg</th>
<td><b>18%</b></td>
</tr>
<tr>
<th colspan='2'><b>Sodium</b>&nbsp;40mg</th>
<td><b>2%</b></td>
</tr>
<tr>
<th colspan='2'><b>Total Carbohydrate</b>&nbsp;17g</th>
<td><b>6%</b></td>
</tr>

<!-- problem-begin -->
<tr>
<!--<td class='blank-cell'></td>-->
<th class="pl-15">Dietary Fiber&nbsp;1g</th>
<td colspan="2"><b>4%</b></td>
</tr>
<tr>
<!--<td class='blank-cell'></td>-->
<th class="pl-15">Sugars&nbsp;14g</th>
<td colspan="2"></td>
</tr>
<!-- problem-end -->

<tr class='thick-end'>
<th colspan='2'><b>Protein</b>&nbsp;3g</th>
<td></td>
</tr>
</tbody>

</table>

<table class='performance-facts__table--grid'>

<tbody>
<tr>
<td colspan='2'>Vitamin A&nbsp;10%</td>
<td>Vitamin C&nbsp;0%</td>
</tr>
<tr class='thin-end'>
<td colspan='2'>Calcium&nbsp;10%</td>
<td>Iron&nbsp;6%</td>
</tr>
</tbody>
</table>

<p class='small-info'>* Percent Daily Values are based on a 2,000 calorie diet. Your daily values may be higher or lower depending on your calorie needs:</p>

<table class='performance-facts__table--small small-info'>

<thead>
<tr>
<td colspan='2'></td>
<th>Calories:</th>
<th>2,000</th>
<th>2,500</th>
</tr>
</thead>

<tbody>
<tr>
<th colspan='2'>Total Fat</th>
<td>Less than</td>
<td>65g</td>
<td>80g</td>
</tr>
<tr>
<td class='blank-cell'></td>
<th>Saturated Fat</th>
<td>Less than</td>
<td>20g</td>
<td>25g</td>
</tr>
<tr>
<th colspan='2'>Cholesterol</th>
<td>Less than</td>
<td>300mg</td>
<td>300 mg</td>
</tr>
<tr>
<th colspan='2'>Sodium</th>
<td>Less than</td>
<td>2,400mg</td>
<td>2,400mg</td>
</tr>
<tr>
<th colspan='3'>Total Carbohydrate</th>
<td>300g</td>
<td>375g</td>
</tr>
<tr>
<td class='blank-cell'></td>
<th colspan='2'>Dietary Fiber</th>
<td>25g</td>
<td>30g</td>
</tr>
</tbody>
</table>

<p class='small-info'>Calories per gram:</p>
<p class='small-info text-center'>
    Fat 9
    &bull;
    Carbohydrate 4
    &bull;
    Protein 4
</p>

</section>

</body></html>

Working fiddle here

Upvotes: 2

Related Questions