【问题标题】:How do I make this nutrition label display properly with HTML and CSS如何使用 HTML 和 CSS 正确显示此营养标签
【发布时间】:2019-10-31 12:48:48
【问题描述】:

我正在设计一个网站,该网站将在其上显示营养标签,其中包含动态填充的营养信息。我在网上找到了一些免费的营养标签代码:https://codepen.io/chriscoyier/pen/egHEK 但由于某种原因,我的标签不像示例中的那样出现。

这是我的标签的显示方式:https://i.imgur.com/IEJsCq6.jpg

这就是我希望我的标签显示的方式:https://i.imgur.com/KgMCbJ1.jpg

我遗漏了一些行,并且一些格式不正确。我相信它与 @import "compass/css3";不能正常工作。

有谁知道如何让我的标签显示得与 codepen 中的标签完全一样?谢谢。

<style>
@import "compass/css3";

.image {
  width: 250px;
  float: left;
  margin: 20px;
}
body {
  font-size: small;
  line-height: 1.4;
}
p {
  margin: 0;
}

.performance-facts {
  border: 1px solid black;
  margin: 20px;
  float: left;
  width: 280px;
  padding: 0.5rem;
  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;
  p {
    margin: 0;
  }
}
.performance-facts__table {
  width: 100%;
  thead tr {
    th, td {
      border: 0;
    }
  }
  th, td {
    font-weight: normal;
    text-align: left;
    padding: 0.25rem 0;
    border-top: 1px solid black; 
    white-space: nowrap;
  }
  td {
    &:last-child {
      text-align: right;
    }
  }
  .blank-cell {
    width: 1rem;
    border-top: 0;
  }
  .thick-row {
    th, td {
      border-top-width: 5px;
    }
  }
}
.small-info {
  font-size: 0.7rem;
}

.performance-facts__table--small {
  @extend .performance-facts__table;
  border-bottom: 1px solid #999;
  margin: 0 0 0.5rem 0;
  thead {
    tr {
      border-bottom: 1px solid black; 
    }
  }
  td {
    &:last-child {
      text-align: left;
    }
  }
  th, td {
    border: 0;
    padding: 0;
  }
}

.performance-facts__table--grid {
  @extend .performance-facts__table;
  margin: 0 0 0.5rem 0;
  td {
    &:last-child {
      text-align: left;
      &::before {
        content: "•";
        font-weight: bold;
        margin: 0 0.25rem 0 0;
      }
    }
  }
}
.text-center {
  text-align: center;
}
.thick-end {
  border-bottom: 10px solid black;
}
.thin-end {
  border-bottom: 1px solid black;
}
</style>
<html>
<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>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="2">
          <b>Calories</b>
          200
        </th>
        <td>
          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>
          14g
        </th>
        <td>
          <b>22%</b>
        </td>
      </tr>
      <tr>
        <td class="blank-cell">
        </td>
        <th>
          Saturated Fat
          9g
        </th>
        <td>
          <b>22%</b>
        </td>
      </tr>
      <tr>
        <td class="blank-cell">
        </td>
        <th>
          Trans Fat
          0g
        </th>
        <td>
        </td>
      </tr>
      <tr>
        <th colspan="2">
          <b>Cholesterol</b>
          55mg
        </th>
        <td>
          <b>18%</b>
        </td>
      </tr>
      <tr>
        <th colspan="2">
          <b>Sodium</b>
          40mg
        </th>
        <td>
          <b>2%</b>
        </td>
      </tr>
      <tr>
        <th colspan="2">
          <b>Total Carbohydrate</b>
          17g
        </th>
        <td>
          <b>6%</b>
        </td>
      </tr>
      <tr>
        <td class="blank-cell">
        </td>
        <th>
          Dietary Fiber
          1g
        </th>
        <td>
          <b>4%</b>
        </td>
      </tr>
      <tr>
        <td class="blank-cell">
        </td>
        <th>
          Sugars
          14g
        </th>
        <td>
        </td>
      </tr>
      <tr class="thick-end">
        <th colspan="2">
          <b>Protein</b>
          3g
        </th>
        <td>
        </td>
      </tr>
    </tbody>
  </table>

  <table class="performance-facts__table--grid">
    <tbody>
      <tr>
        <td colspan="2">
          Vitamin A
          10%
        </td>
        <td>
          Vitamin C
          0%
        </td>
      </tr>
      <tr class="thin-end">
        <td colspan="2">
          Calcium
          10%
        </td>
        <td>
          Iron
          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>
</html>

【问题讨论】:

    标签: html css import label


    【解决方案1】:

    您在 html 中嵌入了 scss 样式表,您不能直接将 SCSS (Sassy CSS) 文件插入到您的 HTML 中。这是因为 SASS 是 CSS3 语法的超集(或所谓的预处理器或扩展),Web 浏览器不支持。

    转到您的 codepen (https://codepen.io/chriscoyier/pen/egHEK),在 css 部分中,有一个下拉菜单提供将 scss 编译为 css 的选项(查看已编译的 css)。点击它,你会得到最终编译好的css如下:

    @charset "UTF-8";
    .image {
      width: 250px;
      float: left;
      margin: 20px;
    }
    
    body {
      font-size: small;
      line-height: 1.4;
    }
    
    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;
    }
    
    .small-info {
      font-size: 0.7rem;
    }
    
    .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;
    }
    
    .text-center {
      text-align: center;
    }
    
    .thick-end {
      border-bottom: 10px solid black;
    }
    
    .thin-end {
      border-bottom: 1px solid black;
    }
    

    将此css嵌入到您的html中,它应该可以工作。请把.performance-facts的宽度从280增加到300,这样会很好看。

    【讨论】:

    • 啊,是的,现在知道了。太感谢了。真的很感激。
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多