如何在HTML H2之前添加一个数字与CSS?

前端之家收集整理的这篇文章主要介绍了如何在HTML H2之前添加一个数字与CSS?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在 HTML和CSS中创建一个漂亮的H2标题,这将允许我在实际标题文本之前有一个很好的格式化的数字,如下图所示:

图像中的示例使用下面的CSS代码,它的工作原理很好,除了我无法在HTML中的橙色圆圈中设置数字值!

  1. h2:before {
  2. content: "2";
  3. text-align: center;
  4. position: relative;
  5. display: inline-block;
  6. margin-right: 0.6em;
  7. -webkit-border-radius: 50%;
  8. -moz-border-radius: 50%;
  9. -ms-border-radius: 50%;
  10. -o-border-radius: 50%;
  11. border-radius: 50%;
  12. width: 1.6em;
  13. height: 1.6em;
  14. font-weight: 900;
  15. line-height: 1.6;
  16. color: #FFF;
  17. font-size: 1em;
  18. background: #F77621;
  19. }
  1. <h2>How to Get Detailed PPC Keyword Data</h2>

我的另一个尝试是把h2和div都放在div里面.跨度保持数字圈:

  1. .number-circles {
  2. margin-bottom: 0.4em;
  3. }
  4. .number-circles h2 {
  5. display: inline-block;
  6. font-size: 1.5em;
  7. text-transform: capitalize;
  8. line-height: 1.75em;
  9. color: #555;
  10. }
  11. .number-circles span.num {
  12. position: relative;
  13. display: inline-block;
  14. margin-right: 0.6em;
  15. -webkit-border-radius: 50%;
  16. -moz-border-radius: 50%;
  17. -ms-border-radius: 50%;
  18. -o-border-radius: 50%;
  19. border-radius: 50%;
  20. width: 1.6em;
  21. height: 1.6em;
  22. font-weight: 900;
  23. line-height: 1.6;
  24. text-align: center;
  25. color: #FFF;
  26. font-size: 1em;
  27. background: #F77621;
  28. }
  29. /* added to show the issue */
  30. .number-circles {
  31. max-width: 15em;
  32. }
  1. <div class="number-circles">
  2. <span class="num">2</span>
  3. <h2>How to Get Detailed PPC Keyword Data</h2>
  4. </div>

使用这种方法,我可以设置HTML中的Number的值.我所遇到的问题是当h2文本对于单行而言太宽,那么它将使整个事情进入一个新行,并且不保留在与行号相符的行上.这不好!查看图片

有人可以帮助我一个很好的解决方案,像第一个这样的行为,一个广泛的h2文本将保持在我的数字跨越?

解决方法

… it works great,except that I cannot set the number value in the
orange circle in the HTML!

您可以使用CSS pseudo elements和HTML5数据属性

  1. h2:before {
  2. content: attr(data-number);
  3. display: inline-block;
  4. /* customize below */
  5. font-size: 1em;
  6. margin-right: 0.6em;
  7. width: 1.6em;
  8. line-height: 1.6em;
  9. text-align: center;
  10. border-radius: 50%;
  11. color: #FFF;
  12. background: #F77621;
  13. }
  1. <h2 data-number="2">Heading</h2>

也可以将数字包在一个跨度内,并将其放在标题内.这使得搜索引擎可以看到该号码.除了不需要content属性之外,CSS将保持不变.

只要记住,如果您只想编号您的标题,您可以使用CSS2 counters

  1. .use-counter {
  2. counter-reset: foo 0;
  3. }
  4. .count-this:before {
  5. counter-increment: foo 1;
  6. content: counter(foo) ".";
  7. display: inline-block;
  8. /* customize below */
  9. font-size: 1em;
  10. margin-right: 0.6em;
  11. width: 1.6em;
  12. line-height: 1.6em;
  13. text-align: center;
  14. border-radius: 50%;
  15. color: #FFF;
  16. background: #F77621;
  17. }
  1. <section class="use-counter">
  2. <h1>Lorem ipsum dolor sit amet</h1>
  3. <h2 class="count-this">Fusce sed nunc eget sem euismod</h2>
  4. <h2 class="count-this">In vel libero in nibh finibus finibus</h2>
  5. <h2 class="count-this">Nam eleifend nulla ut placerat interdum</h2>
  6. </section>
  7. <section class="use-counter">
  8. <h1>Aenean ac urna id sapien</h1>
  9. <h2 class="count-this">Nulla molestie nunc non ultrices</h2>
  10. <h2 class="count-this">Nam eleifend nulla ut placerat interdum</h2>
  11. <h2 class="count-this">Curabitur eget sapien tempor arcu</h2>
  12. </section>

猜你在找的HTML相关文章