使 CSS 中的第一个字符大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5577364/
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-30 00:08:47  来源:igfitidea点击:

Make the first character Uppercase in CSS

css

提问by enjoylife

Is there a way to make the first character Uppercasein a label in CSS.

有没有办法在 CSS 中的标签中制作第一个字符大写

Here is my HTML:

这是我的 HTML:

<a class="m_title" href="">gorr</a>
<a class="m_title" href="">trro</a>
<a class="m_title" href="">krro</a>
<a class="m_title" href="">yrro</a>
<a class="m_title" href="">gwwr</a>

回答by BoltClock

There's a property for that:

有一个属性:

a.m_title {
    text-transform: capitalize;
}

If your links can contain multiple words and you only want the first letter of the first word to be uppercase, use :first-letterwith a different transform instead (although it doesn't really matter). Note that in order for :first-letterto work your aelements need to be block containers (which can be display: block, display: inline-block, or any of a variety of other combinations of one or more properties):

如果您的链接可以包含多个单词并且您只希望第一个单词的第一个字母大写,请改用:first-letter不同的转换(尽管这并不重要)。注意,为了使:first-letter工作的a元件需要是块容器(其可以是display: blockdisplay: inline-block或任何各种一个或多个属性的其他组合的):

a.m_title {
    display: block;
}

a.m_title:first-letter {
    text-transform: uppercase;
}

回答by Logus

Note that the ::first-letterselector does not work with inline elements, so it must be either blockor inline-block, as follows:

请注意,::first-letter选择器不适用于内联元素,因此它必须是blockinline-block,如下所示:

.m_title {display:inline-block}
.m_title:first-letter {text-transform: uppercase}

回答by Amr Elgarhy

CSS :first-letter Selector

CSS:首字母选择器

or :

或者 :

text-transform: capitalize;

回答by Maurizio Battaghini

I suggest to use

我建议使用

#selector {
    text-transform: capitalize;
}

or

或者

#selector::first-letter {
    text-transform: uppercase;
}

By the way, check this w3schools link: http://www.w3schools.com/cssref/pr_text_text-transform.asp

顺便说一下,检查这个 w3schools 链接:http: //www.w3schools.com/cssref/pr_text_text-transform.asp

回答by OzgurG

Make it lowercase first:

首先将其设为小写:

.m_title {text-transform: lowercase}

Then make it the first letter uppercase:

然后使它的第一个字母大写:

.m_title:first-letter {text-transform: uppercase}

"text-transform: capitalize" works for a word; but if you want to use for sentences this solution is perfect.

“文本转换:大写”适用于一个词;但如果你想用于句子,这个解决方案是完美的。

回答by Gnana Sekar

<script type="text/javascript">
     $(document).ready(function() {
     var asdf = $('.capsf').text();

    $('.capsf').text(asdf.toLowerCase());
     });
     </script>
<div style="text-transform: capitalize;"  class="capsf">sd GJHGJ GJHgjh gh hghhjk ku</div>

回答by Kevin Davis

I would recommend that you use the following code in CSS:

我建议您在 CSS 中使用以下代码:

    text-transform:uppercase; 

Make sure you put it in your class.

确保你把它放在你的班级里。