Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make first letter capital using css

Tags:

css

I wand to make my first letter in <span> in capital. But when i use :before in the span capitalize is not working.

span { display:inline-block; color:#66a400; }
span:first-letter { text-transform:capitalize; }
span:before { content:"-"; padding:0 2px; }

<span>my first word</span>

I need out put like below

- My first word
like image 999
Code Maker Avatar asked Feb 08 '17 09:02

Code Maker


3 Answers

You can use :after instead of :before and float it to the left:

span {
  display: inline-block;
  color: #66a400;
}
span:first-letter {
  text-transform: capitalize;
}
span:after {
  content: "-";
  padding: 0 2px;
  float: left;
}
<span>my first word</span>
like image 161
dfsq Avatar answered Sep 20 '22 11:09

dfsq


It's a problem due to the span:before selector, see below.

From https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter

The first letter of an element is not necessarily trivial to identify:

...

Finally, a combination of the ::before pseudo-element and the content property may inject some text at the beginning of the element. In that case, ::first-letter will match the first letter of this generated content.

If you want the "-" before the content with first letter capitalized, you can do as follows, changing your structure and css

CSS

span { display:inline-block; color:#66a400; }
span#before::before { content:"- "; padding:0 2px; }
span#content { text-transform:capitalize; }

HTML

<span id="before"></span><span id="content">my first word</span>
like image 23
Massimo Petrus Avatar answered Sep 21 '22 11:09

Massimo Petrus


span { display:inline-block; color:#66a400; }

span:before { content:"-"; padding:0 2px; } span { text-transform:capitalize; }

<p><span>my</span> first word</p>
like image 29
sneha Avatar answered Sep 19 '22 11:09

sneha