69

I want to capitalize the first letter of sentences, and also the first letter after commas if possible. I want to add the code in here:

.qcont {
    width: 550px;
    height: auto;
    float: right;
    overflow: hidden;
    position: relative;
}
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
1907
  • 825
  • 1
  • 7
  • 8
  • 9
    You can't do that with pure CSS. (And why would you want to capitalize the first letter after a *comma*?) – BoltClock Jun 20 '12 at 23:42

4 Answers4

217

You can capitalize the first letter of the .qcont element by using the pseudo-element :first-letter.

.qcont:first-letter{
  text-transform: capitalize
}

This is the closest you're gonna get using only css. You could use javascript (in combination with jQuery) to wrap each letter which comes after a period (or comma, like you wish) in a span. You could add the same css as above to that span. Or do it in javascript all together.

Here's a snippet for the css approach:

.qcont:first-letter {
  text-transform: capitalize
}
<p class="qcont">word, another word</p>
YakovL
  • 7,557
  • 12
  • 62
  • 102
Luuuud
  • 4,206
  • 2
  • 25
  • 34
  • 38
    Keep in mind that `.qcont` has to be a block. So if you want to capitalize the first word of an inline element ( such as `span` ), you need to make it at least an inline-block. – pyronaur Oct 30 '14 at 12:40
  • 1
    Thanks a lot! Saved me a line of javascript code! :) – acesmndr May 30 '18 at 09:13
  • @justnorris, you just saved me some serious time. Thank you for pointing out that this only works with elements that are set with "position: block" - worked like a charm. – Birdie Golden Jul 24 '19 at 21:32
  • According to the docs, this approach only works if the block-level element is not preceded by any other content: "...but only when not preceded by other content (such as images or inline tables)" – David Sep 28 '21 at 18:15
6

This cannot be done in CSS. The text-transform property makes no distinction according to the placement of a word inside the content, and there is no pseudo-element for selecting the first word of a sentence. You would need to have real elements, in markup, for each sentence. But if you can do that, then you could probably just as well change the initial letters to uppercase in the content proper.

Capitalization at the start of a sentence is a matter of orthography and should be done when generating the content, not with optional stylistic suggestions (CSS) or with client-side scripting. The process of recognizing sentence boundaries is far from trivial and cannot in general be performed automatically without complex syntactic and semantic analysis (e.g., an abbreviation ending with a period may appear inside a sentence or at the end of a sentence).

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • 1
    Not true, the accepted answer shows how this can be achieved. – Joan-Diego Rodriguez Nov 08 '16 at 17:04
  • the accepted answer is only for first letter. what if there is one paragraph and after each full stop you want to make first letter capitalize. I think on that situation this answer make sense. – ThisisFish Mar 05 '20 at 07:42
5

If you need to capitalize the first letter in contenteditable container you can't use the css property

#myContentEditableDiv:first-letter {
    text-transform: capitalize;
}

because when you try to delete a letter automatically you will delete all the text contained in the contenteditable.

Try instead the example provided by sakhunzai in https://stackoverflow.com/a/7242079/6411398 for a working solution.

0

text-transform:capitalize; will capitalize the first letter of a sentence, but if you want to also do it for commas you will have to write some javascript. I agree with @BoltClock, though. Why on earth would you want to capitalize after a comma?

Edit: For the sake of readers: text-transform:capitalize; will capitalize each word of a sentence, not the first one only. You must use the :first-letter CSS selector with the above.

Gregoire
  • 749
  • 6
  • 15
kristina childs
  • 2,190
  • 1
  • 20
  • 19
  • 3
    İ thought text-transform:capitalize is capitalizing every word in sentences. Not the first word of sentences. Am i wrong? – 1907 Jun 21 '12 at 02:25
  • 3
    @1907, you are right, `text-transform:capitalize` capitalizes every word. – Jukka K. Korpela Jun 21 '12 at 05:20
  • 3
    my bad. luud jacobs had it - it needs `:first-letter` with it – kristina childs Jun 21 '12 at 16:41
  • 1
    -1 for being wrong. Please edit the answer itself with the :first-letter instead of having it in comments. Also note that :first-letter only works with block level elements so they are not applicable to for example. – Jukka Dahlbom Feb 11 '14 at 11:21
  • -1 for being a jerk. no, i will not edit my answer. i will admit that i was wrong and not try to hide it by changing my answer later. i made a boo-boo. sue me. vote it down and let that be the end of it. – kristina childs Feb 11 '14 at 14:59
  • 4
    Right.. it is not about "hiding it", it's about providing the best quality answers. Now readers need to read the comments to get the actual working solution. The -1 is not about "being a jerk" either, it's to point out the erroneous information with a comment on how to improve. – Jukka Dahlbom Feb 13 '14 at 12:56