Html CSS 句子案例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14931117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:40:30  来源:igfitidea点击:

CSS Sentence Case

htmlcss

提问by Lian

Is there any way to format a "SENTENCE CASE"? e.g. "THIS IS SENTENCE 1. THIS IS SENTENCE 2. THIS IS SENTENCE 3. change to -> This is sentence 1. This is sentence 2. This is sentence 3.

有没有办法格式化“SENTENCE CASE”?例如“这是第 1 句。这是第 2 句。这是第 3 句。更改为 -> 这是第 1 句。这是第 2 句。这是第 3 句。

回答by iamshahid

you can use id/class:first-letterproperty to achieve that

您可以使用id/class:first-letter财产来实现这一目标

回答by Muhammad Talha Akbar

There are only three CSS properties.

只有三个 CSS 属性。

text-transform: capitalize;
text-transform: lowercase;
text-transform: uppercase;

None of them is gonna output as you want. You can use lowercase text-transform and them use JavaScript to capitalize first word of each sentence.

他们都不会像你想要的那样输出。您可以使用小写文本转换,它们使用 JavaScript 将每个句子的第一个单词大写。

Fiddle

小提琴

回答by Mikhail Vladimirov

CSS can convert first letter of each word, but not first letter of each sentence. Probably you need to use Javascript here:

CSS 可以转换每个单词的首字母,但不能转换每个句子的首字母。可能你需要在这里使用 Javascript:

<html>
  <head>
    <script language="javascript">
<!--
function fixCapitalsText (text)
{
  result = "";

  sentenceStart = true;
  for (i = 0; i < text.length; i++)
  {
    ch = text.charAt (i);

    if (sentenceStart && ch.match (/^\S$/))
    {
      ch = ch.toUpperCase ();
      sentenceStart = false;
    }
    else
    {
      ch = ch.toLowerCase ();
    }

    if (ch.match (/^[.!?]$/))
    {
      sentenceStart = true;
    }

    result += ch;
  }

  return result;
}

function fixCapitalsNode (node)
{
  if (node.nodeType == 3 || node.nodeType == 4) // Text or CDATA
  {
    node.textContent = fixCapitalsText (node.textContent);
  }

  if (node.nodeType == 1)
    for (i = 0; i < node.childNodes.length; i++)
      fixCapitalsNode (node.childNodes.item (i));
}
// -->
    </script>
  </head>
  <body onload="fixCapitalsNode (document.body);">
    THIS IS FIRST SENTENCE.
    THIS IS SECOND SENTENCE.
    This Is Third Sentence.
  </body>
<html>

回答by dinodsaurus

You could use css text-transform:capitalize;property

您可以使用 csstext-transform:capitalize;属性