【问题标题】::nth-letter pseudo-element is not working [closed]:第n个字母伪元素不起作用[关闭]
【发布时间】:2014-07-11 20:08:25
【问题描述】:

当我为我的面包店建立一个网站时,我想制作一个花哨的标题来触发每个字母的颜色。所以我可以使用 span 但这很累。我想使用:first-letter 或分别使用:first-char 但没有任何效果。你们中有人知道怎么做吗?

谢谢,Saludos!

h4 {
  font-size: 100px;
  margin-bottom: 20px;
  margin-top: 0;
  padding: 0px;
  font-family: "Tangerine", cursive;
  font-weight: normal;
}

h4:nth-letter(2) {
  color: #06c !important;
}

h4:nth-letter(3) {
  color: #c9c !important;
}

【问题讨论】:

  • 大声笑,你是什么意思什么浏览器?!那根本不存在。您对确实存在的:first-letter 感到困惑。
  • @Jonathan 好吧,他确实在标题中提到了:first-letter
  • 是的,但我在代码中的任何地方都没有看到。他使用了根本不存在的nth-letter
  • :nth-child 是合法的。 :nth-letter 不是。您的 CSS 不会引发错误。它只是不会渲染相关的样式。至于你的标题,我也不知道:first-char
  • @DimitriJorge 只是因为有人想让它工作,并没有让它工作!

标签: css firefox


【解决方案1】:

CSS 中没有 :nth-letter 伪元素(也没有 :first-char)。 :first-letter 伪元素(问题在标题和散文中提到但未在代码中使用)有效,但要为其他字母着色,您必须将它们中的每一个包装在它自己的元素,通常是span

【讨论】:

    【解决方案2】:

    您必须使用 js nth-everything-css 才能使您的浏览器支持 nth-letter 功能。

    这里有一些关于nth-everythingconcept(或愿望清单)的信息,这里(更重要的是)是其中一些愿望的具体implementationexample

    这里是the code from codepen(有点哽咽),但工作正常。

    <html>
    <style>
        #letters:nth-letter(even){
            color:red;
        }
    
        #letters:nth-letter(odd){
            color:blue;
        }
    </style>
    <script type="text/javascript" src="https://nt4.com/js/jquery"></script>
    <script>
    (function($) {
      $.fn.nthEverything = function() {
        var styleSuffix = "-nthEvery",
          cssPattern = /\s*(.*?)\s*\{(.*?)\}/g,
          cssComments = /\s*(?!<")\/\*[^\*]+\*\/(?!")\s*/gm,
          partsPattern = /([^:]+)/g,
          nthPattern = /(\w*)-(\w*)(\((even|odd|[\+-n\d]{1,6})\))?/,
          count, parsedStyleMap = {}, genCSS = '',
          runPeriods = function(period, className, a, length, offset) {
            var inBy = 0, sAt = Number(period), n, zB, zE, bF, eF, oldN = -1;
            if (period === 'odd' || period === 'even') {
              sAt = (period === 'odd') ? 1 : 2;
              inBy = 2;
            } else if (/^\d+$/.test(period)) {
              sAt = period - offset, inBy = 0;
            } else {
              zB = (/^(\+|-)?\d+/).exec(period);
              zE = (/(\+|-)?\d+$/).exec(period);
              sAt = (zE) ? Number(zE[0]) : 1;
              inBy = (zB) ? Number(zB[0]) : 1;
              bF = (zB) ? zB.length - 1 : 0;
              eF = (zE) ? zE.length : 0;
              if ((period.substr(bF, period.length - eF - bF).charAt(0)) === '-')
                inBy *= -1;
            }
            // Start index at 0;
            for (n = --sAt; n < length; n += inBy) {
              if (n < 0 || n === oldN) break;
              if (a[n] == null) a[n] = className;
              else a[n] += " " + className;
              oldN = n;
            }
          }, createSpan = function(className, content) {
            return '<span class="' + className + '">' + content + '</span>';
          }, processPeriod = function(classNames, textArray) {
            var newText = '', n, className;
            for (n = 0; n < classNames.length; n++) {
              className = classNames[n];
              if (className == null) newText += textArray[n];
              else newText += createSpan(className, textArray[n]);
            }
            return newText;
          }, prepareTxt = {
            letter: function(text) {
              return text.split('');
            }
          }, pseudoFunc = {
            first: {}, last: {}, nth: {
              letter: function(period) {
                return period;
              }
            }
          }, loopRecursive = function(contents, allText, parsedStyle) {
            var func = parsedStyle.func,
              text, length, classNames, className, cat, period;
            contents.each(function() {
              if (this.nodeType === 1) {
                loopRecursive($(this).contents(), allText, parsedStyle);
              } else if (this.nodeType === 3) {
                text = prepareTxt[func](this.nodeValue);
                length = text.length;
                classNames = new Array(length);
                for (var i = 0; i < parsedStyle.period.length; i++) {
                  className = parsedStyle.className[i];
                  cat = parsedStyle.cat[i];
                  period = parsedStyle.period[i];
                  runPeriods(pseudoFunc[cat][func](period, allText, length), className, classNames, length, count);
                }
                $(this).replaceWith(processPeriod(classNames, text));
                count += length;
              }
            });
            return count;
          }, parse = function(css) {
            var matches, nthMatch, nthFound = false,
              i, thisPeriod, selectors, style, selector, parts, nth, pseudo, cat, func, period, normSelector, ident, className;
            css = css.replace(cssComments, '').replace(/\n|\r/g, '');
            while ((matches = cssPattern.exec(css)) !== null) {
              selectors = matches[1].split(',');
              style = matches[2];
              for (i = 0; i < selectors.length; i++) {
                selector = selectors[i];
                parts = selector.match(partsPattern);
                selector = parts.shift();
                nth = parts.shift();
                pseudo = (parts.length !== 0) ? ':' + parts.join(':') : '';
                if ((nthMatch = nthPattern.exec(nth)) !== null) {
                  nthFound = true;
                  cat = nthMatch[1];
                  func = nthMatch[2];
                  period = (nthMatch[4] != null) ? nthMatch[4] : cat + func;
                  normSelector = selector.replace('#', 'id').replace('.', 'class');
                  ident = normSelector + func;
                  className = normSelector + cat + func + period + styleSuffix;
                  if ((thisPeriod = parsedStyleMap[ident]) != null) {
                    thisPeriod.className.push(className);
                    thisPeriod.period.push(period);
                    thisPeriod.style.push(style);
                    thisPeriod.pseudo.push(pseudo);
                    thisPeriod.cat.push(cat);
                  } else {
                    parsedStyleMap[ident] = {
                      element: selector,
                      func: func,
                      className: [className],
                      cat: [cat],
                      period: [period],
                      style: [style],
                      pseudo: [pseudo]
                    };
                  }
                } else if (nthFound === true) {
                  genCSS += selector + "{" + style + "}";
                }
              }
            }
          }, applyStyles = function() {
            var id, parsedStyle, b;
            for (id in parsedStyleMap) {
              parsedStyle = parsedStyleMap[id];
              func = parsedStyle.func;
              $(parsedStyle.element).each(function() {
                var $this = $(this); count = 0;
                loopRecursive($this.contents(), $this.text(), parsedStyle);
              });
              for (b = 0; b < parsedStyle.className.length; b++)
                genCSS += "." + parsedStyle.className[b] + parsedStyle.pseudo[b] + "{" + parsedStyle.style[b] + "}";
            }
            $('<style>' + genCSS + '</style>').appendTo('head');
          };
        $('link[rel=stylesheet],style').each(function() {
          if ($(this).is('link')) $.get(this.href).success(function(css) { parse(css); });
          else parse($(this).text());
        }); applyStyles();
      };
    })(jQuery);
    $(function() {
        $.fn.nthEverything();
    });
    </script>
    <p id="letters">Hover a red letter. Cool, hu?</p>
    </html>
    

    【讨论】:

    • 弗拉维恩,该链接只是指向一个对 nth-letter 等元素表达愿望的网站,这就是为什么有人(不是我)对你投反对票的原因。您应该将该代码笔链接添加到文章中。
    • 完成...顺便说一句,这与我(现已删除)的第一条评论相同
    • 是否可以将其应用于文本区域?
    • 我对示例中使用的:nth-letter(2n1) 的“AnB”格式感到困惑。与实际 CSS3 使用的“An+B 格式”不一致。
    【解决方案3】:

    使用 ::first-letter 时效果很好。

    检查this小提琴http://jsfiddle.net/n7s75/

    h4 {
      font-size:100px;
      margin-bottom:20px;
      margin-top:0;
      padding:0px;
      font-family:'Tangerine', cursive;
      font-weight:normal;   
    }   
    
    h4:first-letter {
      color:#06C !important;   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-18
      • 2014-06-05
      • 2018-09-23
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      相关资源
      最近更新 更多