cancelling text-align: justify; for certain parts of text - css

To prevent wrapping of a part of text, surrounding that part in a <span style="white-space: nowrap;"> can be used.
Given a block of justified text (text-align: justify), how do I cancel justification for a part of the text? I want to keep the whole text justified, but not allow to increase distance between two specific words.
Instead of even spacing in the second line like this:
Lorem ipsum dolor sit amet, consectetur |
adipiscing elit. Phasellus et |
ullamcorperenim sed velit fermentum. |
I want to keep the words "adipiscing elit" together, like this:
Lorem ipsum dolor sit amet, consectetur |
adipiscing elit. Phasellus et |
ullamcorperenim sed velit fermentum. |
Is this possible?

Options:
Use FOUR-PER-EM SPACE U+2005 instead of normal space, e.g. adipiscing elit. There is no guarantee that browsers treat it as non-stretchable space, but that’s what they actually do, and it’s a rather natural thing to do. After all, it is one of the fixed-width spaces. Drawback: typically fails on IE 6 (a small box is shown instead of a space), if the primary font of the text is not Arial Unicode MS or some other especially “rich” font.
Wrap the words inside some inline markup (typically span) and set text-justify: none and display: inline-block for it. Drawbacks: does not work old some old browsers, and forces the two words together (due to the latter declaration—and without it, this methods does not work on current browsers).
Use NO-BREAK SPACE instead of normal space. This used to work well, though with the drawback to forcing the two words together on the same line. But e.g. current Firefox treats no-break as stretchable, sadly enough.
Use a technique like the one in Web_Designer’s updated answer. I would suggest making the width em valued, to make it relate to the font size. The typical width of a space is 0.25em.

I wouldn't recommend using the html style attribute. If you use a class, then it's easier to make changes:
Wrap the two words "adipiscing elit." in a span with a class like this:
<span class="unjustify">adipiscing elit.</span>
And then in your CSS:
.unjustify {
word-spacing: 2px; //experiment with differen't values here.
}
Update:
After some testing the above solution doesn't seem to work. ...so I came up with the following (working) solution:
Wrap the space between your two words in a span with a class like this:
adipiscing<span class="unjustify"> </span>elit.
And then in your CSS:
.unjustify {
display: inline-block;
width: 6px;
}

One solution you should look into is to use one of several fixed-width Unicode space characters. See:
https://jkorpela.fi/chars/spaces.html
or
http://en.wikipedia.org/wiki/Space_(punctuation)
Example:
adipiscing elit.
However like Web_Designer's solution these will leave a space at the end of the line, if the line breaks there.

Related

Can I set um a MINIMUM number of lines (forcing a line break in small texts) using only CSS?

I keep getting designs from agencies with texts breaking in different widths for the same templates, so I was wondering if there is a way using only CSS to force a text to break in at least N number of lines no matter it's size. Something similar to a added to the middle of the sentence, but without using br so it ajusts accordingly on multiple screens.
A text like "lorem ipsum dolor sit amet consectetur adipiscing elit" forced to use 2 lines would look like this (assuming the width available is much larger):
lorem ipsum dolor sit amet
consectetur adipiscing elit
And a text like "lorem ipsum" would look like this:
lorem
ipsum
Of course a single word would use only a single line, and large enough texts would use more than 2 lines.
I'm experienced enough to do this using JS and it feels like dirty work. I was wondering if I'm missing some CSS property like "minimum-lines".

Hetrogenous highlight in HTML 5

So I am trying to highlight some text in my html notes, but it fails if that highlight "region" has another tag in it see this.
I just want it to give pink bg to important text just like a real highlighter does, like so
I tried SPAN, MARK & DIV to achieve this, but all fails.
Note: I understand I can enclose important text in appropriate divs again & again but I am looking for some way that would accomplish real-ish highlighting in one tag
HTML noob, please warn before down-voting
this syntax is too bad you can't open a tag and close it inside another tag like so :
<p>
lorem ipsum sit <mark class="hetro"> amet dolor
</p>
<h2> a heading </h2>
<p>
lorem ipsum sit </mark> amet dolor
</p>
what you can do is applying this mark inside the other tag's too like the following :
<p>
lorem ipsum sit <mark class="hetro"> amet dolor</mark>
</p>
<h2> <mark class="hetro">a heading </mark></h2>
<p>
<mark class="hetro">lorem ipsum sit amet dolor</mark>
</p>
LIVE DEMO
according to Wikipedia :
"tag soup" refers to syntactically or structurally incorrect HTML
written for a web page. Because web browsers have historically treated
HTML syntax or structural errors leniently, there has been little
pressure for web developers to follow published standards, and
therefore there is a need for all browser implementations to be able
to treat what looks like HTML as "tag soup", accepting and correcting
for invalid syntax and structure.

Can I force text to break down in exactly word?

For example, I got a title:
"Lorem Ipsum for WordPress"
It will be display like this in my screen:
Lorem Ipsum for
WordPress
But I want to display like this:
Lorem Ipsum
for WordPress
Can I have any solution in CSS? I dont want to use <br>
Thank you.
If you just want to prevent a line break between two words, as in “for WordPress”, then the simplest way is to use a no−break space (U+00A0) instead of a normal space. If you do not know how to type no−break space in your authoring environment, use the entity, as in for WordPress.
Alternatively, wrap the words inside nobr markup, as in <nobr>for WordPress</nobr>, or inside a span element to which the CSS declaration white-space: nowrap has been assigned.
Set width whatever you want.
<p>"Lorem Ipsum for WordPress"</p>
p {
width: 95px;
}
or you can use two different para
<p>"Lorem Ipsum </p><p>for WordPress"</p>
DEMO: FIDDLE
<p><span style="display:block">Lorem Ipsum</span>for Wordpress</p>
May not possible by just using CSS but you can do using span + a CSS Display as block as shown below
<p><span>Lorem Ipsum</span><span>for WordPress</span></p>
CSS:
p span
{
display: block;
}
Thanks
you can also use white-space:pre-line DEMO

Adjusting Kerning in CSS

Is it possible with CSS to adjust kerning in CSS? I'd like to be able to kern a block of text such that it will actually look like a block of text (both left and right edges are aligned).
EDIT: Using http://letteringjs.com/ in conjunction with http://www.kernjs.com/ makes for pretty good kerning. As for the original problem, http://fittextjs.com/ solves this nicely.
As user Typeoneerror answered, letter-spacing does not influence kerning.
See the kerning concept at Wikipedia.
Kerning is not controlled by letter-spacing, and there are no font-kerning for CSS1 or CSS2. The new specification, CSS3, has not been approved as a standard (W3C Recommendation), but there are a property proposed for font-kerning, see 2012 draft,
https://www.w3.org/TR/css-fonts-3/#font-kerning-prop
Only specific fonts, like OpenFonts, have this property.
CSS not "controls kerning", but if using non-zero letter-spacing the "human kerning perception" can be lost. Example: enhance kerning with letter-spacing:-0.1em and lost with letter-spacing:0.5em.
With CSS1 letter-spacing property you can lost or enhance kerning perception, and into a "letter-spaced text" you can simulate kerning:
<div style="font-size:20pt;background-color:#bcd">
VAST <small>(normal)</small>
<br/>
<span style="letter-spacing:-0.1em">VAST</span>
<small>(enhance perception)</small>
<br/>
<span style="letter-spacing:0.5em">VAST</span>
<small>(lost perception)</small>
<br/>
<!-- SIMULATE KERNING AT SPACED TEXT -->
<div style="letter-spacing:6pt">
SIMULATE: <span style="letter-spacing:4pt">VA</span>ST TEXT
</div>
</div>
See the above example here.
EDIT 2014: I not changed the original text above today, I am opening the answer for your changes (Wiki mode), for proofreading and updates. At the moment this is the most voted answer (21 vs 10) and HTML5 will be a recommendation... Please, help to improve this text (and/or the Wikipedia's linked text!).
Update: CSS3 defines a font-kerning property that can be used to enable or disable kerning for specific elements.
Older answer:
Some control on kerning can be achieved in CSS using the letter-spacing attribute.
However, if all you need is to get "both left and right edges aligned", you might want to try using text-align: justify.
Typographic alignment both left and right is called justification. Kerning is more about the adjustment of spaces between letters, and doesn't have much to do with alignment (because justifying text is more adjustment of spaces between words than characters). Anyway, you want to set the text-align property to justify:
<p style="text-align: justify">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse vestibulum tincidunt ante mollis ornare. Nulla id
nulla justo. Mauris quis sapien ac orci consequat accumsan. Quisque iaculis ipsum ac nulla venenatis sagittis. Aliquam
hendrerit mi a turpis malesuada nec dictum est vehicula. Curabitur quis dolor eu metus malesuada dictum adipiscing et
risus. Aliquam erat volutpat. Aenean pharetra aliquam magna, fringilla tempus erat iaculis eu. Suspendisse potenti.
Sed fringilla lobortis viverra.
</p>
Kerning, letter-spacing (aka tracking) and justify blocks are three different typography properties.
The newest link I found about kerning is from TypeKit, with directions on how to enable the embedded kerning data with their webfonts. http://blog.typekit.com/2014/02/05/kerning-on-the-web/#comment-19916
Manual kerning - adjusting the space between each single letter in a word - will always be a matter of taste and preference. And can be achieved thru kern.js or kerning.js
What makes the TypeKit solution stand out to me, is it enables kerning for all body text automatically by utilizing the meta data in OpenType fonts. I just found it, and am pretty excited to use it in my next project!
For easy reference, the new css properties discussed in the link are:
text-rendering: optimizeLegibility;
font-feature-settings: "kern";
font-kerning: normal;
With prefixes for font-feature-settings including:
-webkit-font-feature-settings: "kern";
-moz-font-feature-settings: "kern";
-moz-font-feature-settings: "kern=1";
Check out my project Jerning.com [see below] for kerning text.
Usage is very simple and is based of pairs of characters, for example:
<h1>Hello World</h1>
The W and o will look weird without kerning.
Simply do:
$('h1').jerning("Wo", -0.1);
which will apply kerning between all upper case Ws and lower case os in h1 tags.
I retired the Jerning project but please find the minified source code below for inclusion in your project if you wish:
(function(b){function e(a,c){var d;if(b.isPlainObject(a))d=a;else try{d=b.parseJSON(a)}catch(f){d=b.parseJSON('{"'+a+'":'+c+"}")}return d}function h(a,c){var d="";b(a).replaceWith(b.map(a.nodeValue.split("").reverse(),function(a,e){var g=a;b.each(c,function(c,e){d==c[1]&&a==c[0]&&(g=b.fn.wrapCharacter(a,e))});d=a;return g}).reverse().join(""))}b.fn.wrapCharacter=function(a,b){return'<span style="letter-spacing:'+b+'em">'+a+"</span>"};b.fn.jerning=function(a,c){var d=e(a,c),f=this.contents();b.each(f,
function(a,c){1==c.nodeType&&b(c).jerning(d);3==c.nodeType&&h(c,d)});return this}})(jQuery);
Unfortunately I don't have the non-minified version any more; the above comes with no guarantees of all cases working!
What seems to be required is dynamic kerning, because what the Thread Opener and also I am/is in need is this: justify does block alignment, as good as possible, by adding space between the words, which leaves "holes" in the text block. Now take instead the line with left align (so no added space), calculate it's physical width, compare with block width available, divide the diffrence trough the number of letters in that line, then use this value as the letterspace addition. This will look more like a newspaper layout than only a text-align:justify. I have to say tho, I am not sure whether the lines in a textblock can be accessed "by line number" in a dynamic page layout, can it?
edit: I tried that yesterday. I use innerHTML to read the content of a text block, then I split and copy it to an array of which each entry/line gets its own DIV tag and id. Then I ajust letterspacing accordingly. Only few of the newest browsers support subpixel-letterspacing, so it works only with medium to big sized fonts. The problem I run into is: due to no Subpixelsupport it works only to some degree, although nice looking, so I tried to additionally justify the result with the text-align attribute, but it's not working after I altered letterspacing.. Left and right alignement works, but not "justify", this one works only before the addition of letterspacing. Could be an opera problem. If I can fix this then I'll post the code.
edit 2. it works now but for some reason I cannot post code here. that is from webkit 10.3.3.13 on mobile. Send me a message if you're interested in the code.

Commenting div closing automation

I'm looking for a solution to a rats nest of code I was handed - it's massive in volume, so I'm looking for suggestions to a programmatic approach to commenting what div closes where.
Example:
BEFORE
<div id="wrapper-item">
<div id="outer-item">
<div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
</div>
</div>
</div>
AFTER
<div id="wrapper-item">
<div id="outer-item">
<div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
</div><!-- .inner-item -->
</div><!-- #outer-item -->
</div><!-- #wrapper-item -->
I tried a few Regex attempts with no joy, I'd be curious of what the best approach is..
If it is valid xhtml, then you could just put it in an XML Document and then proceed to process the div tags by finding all of them, then adding a sibling that has the comment node that you want.
A solitary regular expression might work, but it might be better to write a program that is enhanced by regular expressions. Get the text between < div id=" and >, then add that in a list or stack, and link it, in order, with an int index. Continue scanning it and upon finding a '' tag, pop the newest text on the stack, and format it to become a comment.
I just use a script to properly indent the HTML so I can check at a glance if all tags are closed properly -- the indenting correctly comes back to touch the left margin. To check what opening matches what closing I use a folding text editor like SciTe or Komodo Edit so I can browse the properly indented code by opening and collapsing sections.
If anybody is interested in the indenting script (written in tcl) I can upload it somewhere. Alternatively you can try using something like HTMLTidy to do the formatting.
You can use a XML/DOM API for a particular language and play with how it handles Comment Objects.
For example, Python -- http://docs.python.org/library/xml.dom.html#comment-objects
<div>whatever</div>
<!-- this comment is already deprecated -->
Just use a decent editor that highlights the start/end tag. Even NotePad2 does this. Most editors can also select tag and contents, or just contents. Many editors will also reformat the html for you (if you dare).
Commenting the end of the tag is going to get out of sync and be even harder to follow for you or your coworkers.
If you do end up successfully adding redundant comments to every div, span, p, ul, etc tag, I think you'll find the code is even more bloated and unreadable.

Resources