Capitalize first letter of sentences CSS - css

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;
}

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>

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).

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.

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.

Related

pseudo- element ::selection doesn't work on the first letter if i use ::first-letter. CSS

After using p::first-letter I cant make p::selection, because it works only to other letters, not including first.picture of site. How can I gat access to the selection of first letter?
I've tried to do smth like this: p::first-letter::selection{}, but as we know we can't use more than one pseudo element.
Unfortunately you are right and there is no way of chaining pseudo elements. As far as I know the only way to achieve what you are trying to do is by wrapping the first letter in a <span> or similar and put the styling onto there.
If your content comes from a CMS or similar you'll probably also need some JavaScript to dynamically render it around the first letter.
span {
font-size: 2rem;
}
::selection {
color: red;
}
<p>
<span>H</span>ello
</p>

Replace all characters from a paragraph

Can you think of a way to replace all characters of a paragraph/string with another text or unicode chars using css? My point is to show this text as "obfuscated"/hidden.
Let's say I have a paragraph, I would like every character to be replaced with a unicode char like this: http://www.fileformat.info/info/unicode/char/2588/index.htm. This is one way but I couldn't implemented without javascript. Another way I thought is using SVGs, but I am not able to think of a more specific way.
Right now, we use Blokk font and it works great, but I want to do it without having to do an extra call for one more font.
I know this is mostly a theoretical question, so I don't expect you to provide me the exact answer with code. I would rather know if someone has an idea that I could work on.
You could use background-color and set it to the same color as the text itself. This doesn't actually change the content, but may give the results you are looking for.
For example:
.hidden {
background-color: #000;
}
.hidden:hover {
background-color: transparent;
}
<p>
Here is some <span class="hidden">hidden</span> text.
</p>

Lower case all then capitalize - pure CSS [duplicate]

This question already has answers here:
First lowercase the text then capitalize it. Is it possible with CSS?
(4 answers)
Closed 8 years ago.
I saw this topic here: First lowercase the text then capitalize it. Is it possible with CSS?
But it wasn't pure CSS. I have a div that contains this text:
<div>
RaWr rAwR
</div>
I want to use css to make it look like "Rawr Rawr". Cut if I just to text-transform capitalize it makes it "RaWr RAwR", the already uppercase letters remain upper case. So I need to lower case it all first then capitalize, is the solution to wrap it in a div?
I tried wrapping it in another div but it didnt work:
<style>
#test3 { text-transform: lowercase; }
#test3 div { text-transform: capitalize; }
</style>
<div id="test3"><div>RaWr rAwR</div></div>
This should do it when you have the single words in different div's
#test3 { text-transform: lowercase; }
#test3::first-letter { text-transform: uppercase; }
<div id="test3">haLLo</div>
Sadly, you cannot do this with pure CSS. For now your best hope is to either rewrite text to avoid medial/final capitals or use JavaScript. (Yes, my eyes are rolling too.)
Your suggested approach doesn't work because only one text-transform property value applies to an element at a time. When you specify something like…
#parent { text-transform: lowercase; }
#parent #child { text-transform: capitalize; }
…the value of text-transform on the child element is now capitalize, and nothing else. This is the only transformation applied to that element.
There is a draft proposal to allow authors to define custom mapping tables with an #text-transform rule, but as it stands I doubt it would work for this scenario. The problem is that the scope descriptor (where in a word the transformation applies) only takes one value—you could define a transformation on the whole word or some combination of the start, end or middle parts of a word, but it's not obvious if you could have different transformations for each part.
This seems to be acknowledged in Issue 8 on the wiki draft proposal, and multiple transforms were discussed a couple of years back on www-style. In that thread it is suggested that only a single text-transform value should be allowed on an element, and that combining should be done in the #text-transform rule; however, the draft spec notes:
If the text-transforms referred to have a different scope than the scope specified
in the text-transform that refers to them, they apply at the intersection of the
two scopes.
So a rule like this wouldn't work:
#text-transform lowercase-then-capitalize {
transformation: lowercase, "a-z" to "A-Z" single;
scope: initial;
}
I can see three obvious solutions:
allow multiple scope values to be specified in the #text-transform rule;
add a descriptor to inherit a previous transformation without overriding its scope value; or
permit multiple text-transform values for a selector.
The www-style mailing list might be a good place to take this if you ever want to see a pure CSS solution to this question.
You are using nested div,
So #test3 { text-transform: lowercase; } is applied for Parent div so it applies for Both Parent and Child Divs
when you override the #test3 div { text-transform: capitalize; } to the Child Div the first style text-transform: lowercase; is ignored
Unfortunately, until CSS 3 there is no way to do this in pure CSS way, as per this document there are only 5 possible values to text-transform and none of them will solve this requirement!
But in future there might be provision for custom rule for text transform similar to Counter Sytle

How to capitalize first line with CSS? ::first-line pseudo-element not working

Right now I've got a paragraph and I'd like to capitalize the entire first line. I've set the first paragraph to an ID "firstp" and tried:
#firstp::first-line {
text-transform: uppercase;
}
I've tried it with text-transform: capitalize but that doesn't work either. It's strange because I've managed to change the first letter (changed font size) using #firstp:first-letter.
text-transform on :first-line is really buggy right now, see the reference here http://reference.sitepoint.com/css/text-transform
You can use this jquery plugin called linify https://github.com/octopi/Linify to select the first line and then apply the property of text-transform: uppercase
Regards,
I think Chrome has a problem with ":first-line". Try removing that and using james31rock's syntax on this page, except in his example the CSS should be "#firstp{..." to reflect the ID in his HTML rather than ".makeupper".
You might want to use:
font-variant: small-caps;
It looks better in my opinion and is supported in all major browsers.
More info
http://en.wikipedia.org/wiki/Small_caps
In order to accomplish this you must use the following Pseudo-element within the correct syntax. Example:
HTML PORTION:
<selection id="Welcome">
<p>Some text</p>
</section>
CSS SHEET:
#Welcome p:first-of-type:first-line {
text-transform: uppercase;
}
I hope this helps. Sorry for the late entry. I guess, better late than never!
There might be another way ...
What if you have two spans with text inside paragraph, one of the spans would have position set to upper left corner of the paragraph, so the two spans would overlap. Now set height of the upper span to about line-height of your text and overflow to hidden so only first line of the span would be visible. Set text-transform to uppercase on the upper span and vou la you have first line in uppercase.
Downside of this solution is, uppercase text is wider so there might be missing words on next line. You can fix this by using fixed-width font or try to set letter-spacing on both spans so width of uppercase and lowercase texts would be same.
Here is jsFiddle, though it's not ideal sometimes when you change the window size, it might be sufficient.
It doesnt seem to work either way, in Chrome. In I.e. text-transform works. I do not have firefox to test with.
http://jsfiddle.net/vJSeq/4/ - using text-decoration
http://jsfiddle.net/vJSeq/3/ - using text-transform
You can see that the selector is right because it is highlighted.
My suggestion would be to use something to seperate the text you want highlighted, by creating a span tag inside of the paragraph and assign it a class
<p id="firstp">to stop the degradation of the planet's natural environment and to build a future in <span class="makeupper">which humans live in harmony with nature, by; conserving</span> the world's biological diversity, ensuring that the use of renewable natural resources is sustainable, and promoting the reduction of pollution and wasteful consumption.</p>
​
and the css
.makeupper
{
text-transform: uppercase;
}
http://jsfiddle.net/vJSeq/5/

Styling the first letter of a particular word, etc

I'm trying to style a word with a big first letter and spacing for the other letters. My current solution is pretty ugly: see here (and a malfunctioning jsfiddle here).
Ideally, instead of something ugly like this: <dropcap>T</dropcap><span style="letter-spacing:.2em;">HERE</span><span style="margin-left:-.2em;"> is</span> nothing more unreasonable...
I could have something sensible like this: <dropcap>THERE</dropcap> is nothing more unreasonable...
Any ideas? Thanks.
You could just use the :first-letter pseudo-element.
The :first-letter pseudo-element is
mainly used for creating common
typographical effects like drop caps.
This pseudo-element represents the
first character of the first formatted
line of text in a block-level element,
an inline block, a table caption, a
table cell, or a list item.
It's supported in IE8+ and all modern browsers.
For example: http://jsfiddle.net/HTnBP/4/
For the other half of your question, try:
T<span>HERE</span>
div > span:first-child {
letter-spacing: .2em;
margin-right: -.2em
}
http://jsfiddle.net/HTnBP/5/
I don't really see the point in using a custom dropcap element. Unless you don't mind adding extra complexity to support IE8 and lower, or you simply don't care about those browsers.
Or maybe you can use this JS solution i found
http://webplatform.adobe.com/dropcap.js/

Resources