So, here's my problem: I'm creating a website where I've some posts. In those posts, I put a "::first-letter" highlighting to make it bigger, and it works perfectly.
But, when I'm going to load a post with first letter as a Unicode Emoticon that is a UTF-8 mb4 (2 Unicode Chars), it fails, by trying to load the single char as 2 separated, so the result is something strange.
This is a screenshot:
How can you see, there's a bigger letter and one smaller that are unknown, and then the same emoticon visible, because I created a post with the same emoticons wrote down 2 times.
.first_letter_post::first-letter {
float: left;
padding-right: 20px;
padding-top: 0px;
margin-bottom: -15px;
margin-top: -10px;
font-size: 50px;
font-weight: bold;
text-transform: uppercase;
}
<p class="first_letter_post">๐ฟfoobar</p>
This is the character: ๐ฟ, and I'm using Google Chrome.
I hope someone can help me with this.
Chrome has a long know history of problems with unicode [bug]. This issues is a combination of those problems:
Failing to correctly recognize symbols consisting of more than 3 bytes.
Styling symbols regardless of being a letter unit
This results in Chrome tearing a single symbol apart.
IE is correctly recognizing unicode symbols consisting of multiple codepoints and applies the styling regardless of the spec stating that ::first-letter should be applied to typographic letter units only.
Firefox behaves very strict to the spec, not applying styles to non-letter units. I could not determine whether the Alphanumeric Supplement Space should be treated as letter as well, but Firefox is not treating them as such.
This means, that you should refrain from using ::first-letter when you are heavily relying on it and know that those characters might occur.
A possible solution I could think of, is manually detecting the first character via javascript and wrapping it in a tag and then apply the styles. My solution is a bit messy due to the hard coded hex value, but it might be sufficient.
// manually wrapping the "first character"
Array.prototype.forEach.call(document.querySelectorAll("div"),function(el){wrapFirstChar(el)});
function wrapFirstChar(div){
let content = div.innerHTML,chars=content.charCodeAt(0) >= 55349?2:1;
div.innerHTML = "<span>"+content.substring(0,chars)+"</span>"+content.substring(chars);
}
// this is what javascript sees at the first two positions of the string
//Array.prototype.forEach.call(document.querySelectorAll("p"),(e)=>console.log(e.innerHTML.charCodeAt(0)+"+"+e.innerHTML.charCodeAt(1)));
p::first-letter {
font-weight: bold;
color:red;
}
span {
font-weight: bold;
color:blue;
}
p{
margin:0;
}
<h2>using ::first-letter</h2>
<p>๐ฟ 4 bytes symbol</p>
<p>๐
ฐ Enclosed Alphanumeric Supplement 1F170</p>
<p>๐น Mathematical Alphanumeric Symbols 1D7B9</p>
<p>๐ธ Arabic Mathematical Alphabetic Symbols 1EE00</p>
<p>a normal character (1 byte)</p>
<h2>manually replaced</h2>
<div>๐ฟ 4 bytes symbol</div>
<div>๐
ฐ Enclosed Alphanumeric Supplement 1F170</div>
<div>๐น Mathematical Alphanumeric Symbols 1D7B9</div>
<div>๐ธ Arabic Mathematical Alphabetic Symbols 1EE00</div>
<div>a normal character (1 byte)</div>
Related
The hyphens css property can be used to let browser "automatically break words at appropriate hyphenation points", and insert hyphens on those places.
Unfortunately, the hyphens only get inserted on those appropriate hyphenation points detected based on grammar rules for the current html lang - if no such appropriate hyphenation point is detected, and a word gets broken at an arbitrary character, no hyphen gets inserted.
It is demonstrated in following runnable snippet (and in this sandbox):
.container {
width: 100px;
background: olive;
overflow-wrap: break-word;
hyphens: auto;
margin-bottom: 10px;
padding: 0px 15px;
box-sizing: border-box;
}
<html lang="en">
<div class="container">
fadsafweafewfsaaa
</div>
</html>
The fictional word "fadsafweafewfsaaa" got broken in two places - the first break-point gets a hyphen, because it constitutes an appropriate hyphenation point. The second break gets no hyphen, because no appropriate hyphenation point was found, and the word got broken at point of overflow.
This is especially relevant because english is pretty much the only supported language. Because of this, as of now, this feature is fairly useless with most of other languages.
Is it possible to always display a hyphen wherever word breaks into a new line, regardless of the mechanism?
I'm trying to achieve the following effect:
But it seems like the only way to do this is with word-wrap: overflow-wrap which isn't widely supported.
So instead the only solution I seem to have is to use: word-break: break-all which will produce the following results:
NOTICE: how the from is broken in the second picture? What styles do I need to use to get the results from the first image?
JSFiddle: https://jsfiddle.net/jennifergoncalves/k4yn9ucf/
word-wrap (documentation) is a legacy name for overflow-wrap. word-wrap is required for IE (11), Edge, and Opera Mini. Other major browsers now all support overflow-wrap (source). This property can be set to:
normal - breaks only between words
break-word - may break words in the middle if necessary
In contrast to word-break, overflow-wrap will only create a break if an entire word cannot be placed on its own line without overflowing.
There is another property called word-break (documentation) which control how words are broken. Accepted values:
normal - default
break-all - break between any two characters of a word
keep-all - identical to normal for non-CJK (Chinese/Japanese/Korean) text
In contrast to overflow-wrap, word-break will create a break at the exact place where text would otherwise overflow its container (even if putting an entire word on its own line would negate the need for a break).
So it would seem there are two options to get properly wrapped text:
overflow-wrap: break-word
word-break: break-all
Here's a comparison of the two, along with a no-wrapping example:
p {
background-color: #009AC7;
color: #FFFFFF;
font-size: 18px;
font-family: sans-serif;
padding: 10px;
text-align: justify;
width: 250px;
}
.wrap1 {
overflow-wrap: break-word;
}
.wrap2 {
word-break: break-all;
}
No wrapping:
<p><b>pneumonoultramicroscopicsilicovolcanoconiosis</b> refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano;</p>
<code>word-wrap: normal</code> and <code>word-break: normal</code>
<p class="wrap1"><b>pneumonoultramicroscopicsilicovolcanoconiosis</b> refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano;</p>
<code>word-wrap: normal</code> and <code>word-break: break-all</code>
<p class="wrap2"><b>pneumonoultramicroscopicsilicovolcanoconiosis</b> refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano;</p>
Can you see the problem? If you are using the latest Chrome, for example, the word breaks are placed at arbitrary points. There is nothing preventing your browser from breaking from into f-rom because it would need to understand the hyphenation rules for the particular language.
Fortunately, there is hyphens (documentation) which should solve your problem. The value auto should allow the browser to decide when to hyphenate.
Hyphenation rules are language-specific. In HTML, the language is determined by the lang attribute, and browsers will hyphenate only if this attribute is present and if an appropriate hyphenation dictionary is available. In XML, the xml:lang attribute must be used.
Note: The rules defining how hyphenation is performed are not explicitly defined by the specification, so the exact hyphenation may vary from browser to browser.
Let's see it in action:
p {
background-color: #009AC7;
color: #FFFFFF;
font-size: 18px;
font-family: sans-serif;
hyphens: auto;
-ms-hyphens: auto;
-webkit-hyphens: auto;
padding: 10px;
text-align: justify;
width: 250px;
}
<code>hyphens: auto</code>
<p lang="en-US"><b>pneumonoultramicroscopicsilicovolcanoconiosis</b> refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano;</p>
In the latest Chrome on Mac this produced a nice result (although the gaps in the text are not very pleasant to look at), conforming to the English hyphenation rules.
Unfortunately, support for this property is a little spotty. Chrome supports this only on Mac and Android. Safari, IE, and Edge require vendor prefixes (source). If this is good enough for you, use hyphens. If not, consider using an alternative solution, such as a JavaScript automatic hyphenator, e.g. Hyphenator. Your last resort would be to use a hyphenation engine to parse your text and find all word-break points, then insert them into your as HTML soft hyphens โ ยญ โ in combination with hyphens: manual.
.applyWordWrap{
max-width: 8em;
word-wrap:break-word;
}
(o.4em = 1Character)
8em = 20Characters .
I have a string with 60 characters.
I want to show a line with 20 characters, then wrap the remaining text to the next line which contains 10 characters followed by ... (triple dots)
I tried the above snippet, but its not working
I referred different posts but they are not much helpful.
How i can achieve this using css?
The closest you can get is to set a maximum width in ch units Take a look here.
This approached its the closest approximation to get the width of the characters that we have in css.
try this:
.applyWordWrap{ max-width: 20ch; word-wrap:break-word}
Your calculation is right only if you use fixed-width fonts. In TrueType fonts character width is variable, so the string "www" is wider than "iii". It's the reason why CSS deal with actual string length and not with number of characters.
The following sample illustrates it:
#ttf{
font-size: 16px;
}
#fixed{
font-family: Courier New, monospace;
font-size: 16px;
}
<div id='ttf'>The text in 32 characters length</div>
<div id='fixed'>The text in 32 characters length</div>
If you want to bound the string by the fixed number of characters you should use JavaScript.
I am studying a free css template called zerofour found at http://html5up.net/zerofour/, and running across a css coding that I have never seen before. In the HTML, some divs have class such as "4u", but when I check the css files, this is the only text section that has anything with those terms, and it looks like this:
/* Grid */
/* Cells */
.\31 2u { width: 100% }
.\31 1u { width: 91.6666666667% }
.\31 0u { width: 83.3333333333% }
.\39 u { width: 75% }
.\38 u { width: 66.6666666667% }
.\37 u { width: 58.3333333333% }
.\36 u { width: 50% }
.\35 u { width: 41.6666666667% }
.\34 u { width: 33.3333333333% }
.\33 u { width: 25% }
.\32 u { width: 16.6666666667% }
.\31 u { width: 8.3333333333% }
.\-11u { margin-left: 91.6666666667% }
.\-10u { margin-left: 83.3333333333% }
.\-9u { margin-left: 75% }
.\-8u { margin-left: 66.6666666667% }
.\-7u { margin-left: 58.3333333333% }
.\-6u { margin-left: 50% }
.\-5u { margin-left: 41.6666666667% }
.\-4u { margin-left: 33.3333333333% }
.\-3u { margin-left: 25% }
.\-2u { margin-left: 16.6666666667% }
.\-1u { margin-left: 8.3333333333% }
When I remove the 4u class using chrome developer, the page is affected. However, when I delete this section from the css file, nothing happens. I am quite stumped in this, and it is really bugging me!
Read this spec: http://www.w3.org/International/questions/qa-escapes#cssescapes. The characters following the back slash represent a unicode code point. The space is required if the next character is allowed as part of the hexadecimal values.
It looks like something similar to Foundation. I'm new to Html5up as well but that's my guess. How it normally works (in Foundation at least) you have a "row" which is the entire width of your page (normally). Within that row you have columns (or "u" here for some reason), with each column taking up the same % of width. Usually the standard for Foundation is 12 columns/row and I'm guessing its the same here. So basically if you want the first 50% of a row to be a giant div followed by two divs that split the remaining 50% it would look like:
<div class="row">
<div class="6u">
Big Div Here.
</div>
<div class="3u">
Small Div Here.
</div>
<div class="3u">
Small Div here (same size as the other small div)>
</div>
</div> (This ends the row).
The reason for all this madness being it makes it way easier to make your page responsive (which is one of the main points of Foundation and HTML5Up).
Looking into Foundation's Dev Docs for a more in-depth explanation and additional examples (despite it not necessarily being Foundation) - Here
It's the first time that I see class names like this and I wondered about the meaning. The W3C page says:
In CSS 2.1, a backslash (\) character can indicate one of three types
of character escape. Inside a CSS comment, a backslash stands for
itself, and if a backslash is immediately followed by the end of the
style sheet, it also stands for itself (i.e., a DELIM token). First,
inside a string, a backslash followed by a newline is ignored (i.e.,
the string is deemed not to contain either the backslash or the
newline). Outside a string, a backslash followed by a newline stands
for itself (i.e., a DELIM followed by a newline).
Second, it cancels the meaning of special CSS characters. Any
character (except a hexadecimal digit, linefeed, carriage return, or
form feed) can be escaped with a backslash to remove its special
meaning. For example, "\"" is a string consisting of one double quote.
Style sheet preprocessors must not remove these backslashes from a
style sheet since that would change the style sheet's meaning.
Third, backslash escapes allow authors to refer to characters they
cannot easily put in a document. In this case, the backslash is
followed by at most six hexadecimal digits (0..9A..F), which stand for
the ISO 10646 ([ISO10646]) character with that number, which must not
be zero. (It is undefined in CSS 2.1 what happens if a style sheet
does contain a character with Unicode codepoint zero.) If a character
in the range [0-9a-fA-F] follows the hexadecimal number, the end of
the number needs to be made clear. There are two ways to do that:
with a space (or other white space character): \26 B (&B) [...]
Note: Backslash escapes are always considered to be part of an
identifier or a string (i.e., \7B is not punctuation, even though
{ is, and \32 is allowed at the start of a class name, even though
2 is not). The identifier te\st is exactly the same identifier as
test.
So as far as I can understand, the \3+number+space part here is used to be able to use numbers only as class names.
html5up.net uses a framework called skel to make the layouts responsive.
If your question is "how do I make sense of these classes" rather than "how do these escape sequences work", then the reference you're looking for is at https://github.com/n33/skel/blob/master/docs/skel-layout.md#usage-1.
I think this type of css is used for mobile Compatibility.
I would like to display a number of underscores to the user to let them know how many characters they need to fill in to satisfy the length requirement.
My problem is: underscores aren't separated by spaces.
Q: What font or css style should I use so that my underscores can be easily counted?
You can just use letter-spacing: 0.4em; to separate out the various characters in the element. I'm assuming you're using an input element, so:
input {
letter-spacing: 0.4em; /* or whatever measurement you prefer... */
}
JS Fiddle demo.
References:
letter-spacing at the MDC.
Use the CSS property letter-spacing.
Visual example: http://jsfiddle.net/8w9WY/