Why doesn't word-wrap work properly in CSS3? - html

Why doesn't word wrap property work properly in the example below?
http://jsfiddle.net/k5VET/739/
What can I do to ensure that part of the word 'consectetur' fits in the first line itself? I want maximum number of characters to fit in each line.
The css is :
#fos { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size:14px;
font-weight: normal;
line-height: 18px;
width: 238px;
height:38px;
cursor: pointer;
word-wrap:break-word;
border:2px solid; }

If word-wrap: break-all don't work, try also add this:
white-space:normal;
work for me with the .btn class in Bootstrap 3

Use word-break: break-all;
#fos { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size:14px;
font-weight: normal;
line-height: 18px;
width: 238px;
height:38px;
cursor: pointer;
word-break: break-all;
border:2px solid; }
LIVE DEMO

Okay so I know this is an old question but I seriously think there could be some useful clarification of when to use which property. Looking at the existing answers, I feel they offer solutions without explanation so here is my attempt to help with that. For context, by default, the container will not try to break a single long word up because it wants to preserve the word in it's original form.
With that said, 3 main ways to break up words there is overflow-wrap, word-break, and hypens. I won't dive too deep into hypens though.
So first off. I see word-wrap is being used in the question but reading the documentation, that is deprecated and some browsers have decided to simply alias it to overflow-wrap. It's probably preferred not to use that.
The distinction between the two properties is pretty clear in the documentation but for clarity, overflow-wrap really only has two possible values and will detect if a word has been overflowed. If it does, by default it moves the entire word to the next line. If break-word is set, it will only break up the word if the entire word couldn't be placed on that line.
#fos { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size:14px;
font-weight: normal;
line-height: 18px;
width: 238px;
height:38px;
cursor: pointer;
overflow-wrap: break-word;
border:2px solid; }
#sp { }
<div id="fos">
<span id="sp">The longest word ever: pneumonoultramicroscopicsilicovolcanoconiosis and then some.</span>
</div>
You can see the pneumonoul... long word breaks exactly where it needs to on the next line after it attempts to separate the long word.
Now in contrast, we have word-break. This has more granular properties and actually also has a break-word option. This is synonymous to the above snippet so when to use one or the other I think is pretty trivial.
However, word-break also offers a break-all value which will break the word whenever possible to fit word where it was originally intended. It doesn't try to figure out if it needs the word to be on a new line or not -- it simply shoves the line break where it overflows. The biggest distinction here from the above is that long word we use is now still on the top.
#fos { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size:14px;
font-weight: normal;
line-height: 18px;
width: 238px;
height:38px;
cursor: pointer;
word-break: break-all;
border:2px solid; }
#sp { }
<div id="fos">
<span id="sp">The longest word ever: pneumonoultramicroscopicsilicovolcanoconiosis and then some.</span>
</div>
And then finally for hyphens have their own special way of breaking up words. For more info take a look at the documentation. Also worth noting that different languages have different ways of breaking up words but fortunately the hyphens property hooks into the lang attribute and understands what to do.
Another option people have suggested is using the white-space property which attempts to break up text through spaces. In the example I used, this doesn't help our situation because the word itself is longer than the container. I think the important thing to note about this CSS property is that this doesn't attempt to break up words within themselves and rather tries to do it via spaces whenever appropriate.
#fos { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size:14px;
font-weight: normal;
line-height: 18px;
width: 238px;
height:38px;
cursor: pointer;
white-space: normal;
border:2px solid; }
#sp { }
<div id="fos">
<span id="sp">The longest word ever: pneumonoultramicroscopicsilicovolcanoconiosis and then some.</span>
</div>
Hope this helps.

This is useful for wrapping text in one line:
#fos {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 100%;
}

It does not wrap properly, because browsers do not apply automatic hyphenation (which would be the way to cause proper wrapping) by default. You need to use CSS hyphenation or JavaScipt-based hyphenation. The setting word-wrap:break-word, as well as word-break: break-all, by definition breaks words (splits th em to piece s at arbit rary poin ts), instead of proper wrapping.

Because the css word-wrap doesn't work in all browsers, use JavaScript instead! It should give the same result.
The function below requires JQuery and it will run each time window re-size.
The function checks if the element has more width then its parent, and if it has, it breaks all instead of words only. We don't want the children to grow bigger than it's parents, right?
I only needed it for tables, so if you want it in some other element, just change "table" to "#yourId" or ".yourClass". Make sure there is a parent-div or change "div" to "body" or something?
If it will go to else, it changes to word-break and then check if it should change back, that's why there is so much code.. :'/
$(window).on('resize',function() {
$('table').each(function(){
if($(this).width() > $(this).parent('div').width()){
$(this).css('word-break', 'break-all');
}
else{
$(this).css('word-break', 'break-word');
if($(this).width() > $(this).parent('div').width()){
$(this).css('word-break', 'break-all');
}
}
});
}).trigger('resize');
I hope it works for you!

To break only long words I used word-break: break-word;.
It seems to be deprecated for some reason, but it is the only thing that worked for me.
What I need is to break only the words that where too long and needed to be broke.

Related

Cause of extra wordspace around inline code

My Sphinx project generates HTML for a document that uses code font inline. The text font I'm using is Lato; the code font is Consolas.
The problem is that wherever the document shifts from the text font to inline code, Sphinx inserts extra space. Here's a snapshot of how the text should look (simulated in another application) and how it actually looks:
The problem is particularly obvious with code at the beginning of a line. The extra space makes that line appear to be indented:
It looks like Sphinx is generating HTML that composes the preceding and following words in code font, but it's not:
Another composing tool I use does not do this, although it generates similar HTML:
I assume that the project's style sheet is giving the Consolas font some property that adds space before and after a font shift, but I can't find it in the style sheet or the browser's HTML inspector. Actually, I can't find a property in HTML that could do that. What should I look for?
Later -- this is a response to Steve Piercy's comment. I'm editing the original message because I can't insert graphics in a comment.
Steve asked me to attach a reproducible sample. I'm afraid that will be difficult because some parts of the project (the theme, i.e. the infrastructure, not just the content) are proprietary. I should be able to separate the style sheets from the theme and use them to demonstrate the problem in a "clean" theme, but to do that I'll need to learn more about themes. It's likely to take a while just to find time for that.
As an alternate approach, I'm attaching snapshots of the output, the HTML, and the entire set of applicable styles from the browser's HTML inspector. All of the relevant information should be there.
The display:
The HTML that generated it:
The styles (1 of 5)
(2 of 5)
(3 of 5)
(4 of 5)
(5 of 5)
Response to Steve Piercy, 11/29:
I followed Steve's 11/29 suggestion and saw the following.
For a piece of inline code:
border-collapse: collapse;
border-spacing: 0px 0px;
box-sizing: border-box;
color: rgb(0, 0, 0);
empty-cells: show;
font-family: SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, Courier, monospace;
font-size: 13.9333px;
font-weight: 500;
line-height: 17.6px;
text-align: left;
white-space: nowrap;
For the enclosing body type:
border-collapse: collapse;
border-spacing: 0px 0px;
box-sizing: border-box;
color: rgb(0, 0, 0);
empty-cells: show;
font-family: "Helvetica Neue", Arial, sans-serif;
font-size: 14.6667px;
font-weight: 400;
line-height: 17.6px;
margin-bottom: 5.86667px;
margin-left: 0px;
margin-right: 5px;
margin-top: 10.2667px;
overflow: visible;
overflow-x: visible;
overflow-y: visible;
padding-bottom: 0px;
padding-top: 0px;
text-align: left;
white-space: normal;
The only possibly relevant properties I see are box-sizing in the inline code, and margin-right in the body type. I tried changing both (to content-box and 0 respectively), but that had no effect.
December 5: Steve Piercy wrote, "You're getting closer. I see 4 margin-* attributes that you can inspect and override."
With respect, I don't see anything of the sort. I selected a piece of inline code and scrolled up and down the attribute stack several times, and when I couldn't find the margin attributes I copied and pasted the whole thing into a text editor and searched for "margin". There were no instances.
Here is the entire contents of the code inspector's right hand column (still with a piece of inline code selected):
Just a note to let you know that I found the problem. The <span> tag that wraps inline code does not have padding, but another tag -- two levels further up -- did! I changed that tag from "padding: 2px 5px;" to "padding: 2px 0;" and the problem resolved.
Thanks to everyone for your efforts to help. You really did help; I only found the problem while trying to simplify a complete HTML sample that i could upload without proprietary content.

Define global word-break rule with css

i currently have a problem with line breaks in my header element. The headline shouldn't wrap into the image, but I also don't want it to wrap without a hyphen. I don't want the copy editors to make the word-breaks themselves with html. I would like to have an overall solution built with css.
Here is a codepen of my code
I already tried these:
line-break: anywhere;
line-break: normal;
line-break: loose;
line-break: strict;
word-break: break-all;
word-wrap: break-word;
Thank you so much in advance! :-)
so as there is sadly no way to implement the automatic CSS hyphenisation when you want your Site to be used with Chrome Browsers, i recommend using viewports to change the Font-Size or make the Font-Size somewhat responsive.
Easy solution with responsive Font-Size (it's a workaround that is a bit weird in my eyes but it does keep the Text in the same format):
HTML:
<!--Set the breakpoint in html-->
<h1>
<span class="line">You are</span>
<span class="line">remembered</span>
</h1>
CSS:
/*add css for the intended break*/
.line{
display: inline-block;
margin-bottom: 0.3vw;
}
h1 {
font-family: "Space Grotesk", sans-serif;
color: $color-highlight;
margin-bottom: 0;
/*Set a responsive Font-Size*/
font-size: 6vw;
line-height: 80px;
}
For the viewport solution just check a basic tutorial for responsive design like THIS
There you can define the behavior depending on the screen size.

pasted text in textarea overflows even though spaces are existent

When someone manually types in the text area the line breaks happen where I want them. However, if someone pastes a wall of text from somewhere else the linebreaks don't occur and the autoscroll kicks in leaving my lovely blog with a crappy horizontal scroll bar in the blog-subject field.
Blog here so you can see what I'm talking about (link to post entries at bottom of page if you want to try...).
Thanks in advance for your help
Just specify a width for .blog-content and then use word-wrap: normal;.
.blog-content {
font-family:monospace;
font-size:12px;
overflow:auto;
width: 100%;
word-wrap: normal;
}
The blog posts are currently wrapped in <pre> tags. The point of pre tags is that they preserve line breaks and white space. This is probably not what you want for this situation.
Change the pre tags to divs and your problem is solved.
Try adding white-space: normal;
.blog-content {
font-family: monospace;
font-size: 12px;
overflow: auto;
white-space: normal;
}

Psuedo Class Selectors Within ID

I would appreciate a little assistance working with Psuedo Selectors for an ID within CSS. I cannot seem to display the first line of an ID in small caps. A snippet of both my CSS and the corresponding HTML is included for reference.
#introtext {
margin: 0px 0px 20px 0px;
background: transparent;
font-face: "arial black";
font-size: 22px;
font-weight: bold;
letter-spacing: .1em;
line-height: 1;
text-align: center;
}
#introtext:first-line {
font-variant: small-caps;
}
HTML:
<div id="introtext"><span id="introcap">H</span><span id="introtext.first">ere you will
find links to Scouting related websites and forms.</div>
Perhaps...
...your expectations are incorrect (and I know your code has an error). While I agree with the comments here that your code is working as expected, I am speculating you are attempting something different.
Did you want the first sentence in small caps?
I noticed in your code you have the "opening" tag of <span id="introtext.first"> but you failed to put the closing tag for that </span>. This is an error. However, if your intent is to have the whole first sentence to become small-caps, your code will still not work because the :first-line pseudo-element does not look at the first sentence, but the first-line (which varies based off of the container width).
Additionally
It is best to not use a . in an id, since the . is used for designating classes. And perhaps a class is what you really want anyway.
Finally, A Solution?
If my speculations about your goals are correct, then I recommend doing the following (keeping your #introtext css the same, but changing the html and :first-line code), as demonstrated in this FIDDLE.
HTML
<div id="introtext">
<span id="introcap">H</span><span class="first-sentence">ere you will find
links to Scouting related websites and forms.</span> More text to follow
in a second sentence. And a third. Blah. Blah.
</div>
CSS
.first-sentence {
font-variant: small-caps;
}
Although
It almost appears as though you intend this to be a header (since the text is centered), so maybe something like this is more semantic and also eliminates the need for your leading "H" to be separate (unless you want OLD browser support). Here is its demo FIDDLE.
HTML
<div>
<h2 id="introtext">Here you will find links to Scouting related websites
and forms.</h2> More text to follow in a second sentence. And a third.
Blah. Blah.
</div>
CSS
#introtext {
margin: 0px 0px 20px 0px;
background: transparent;
font-face: "arial black";
font-size: 22px;
font-weight: bold;
letter-spacing: .1em;
line-height: 1;
text-align: center;
font-variant: small-caps; /*<-- moved this to here*/
}
#introtext::first-letter {
font-variant: none; /*<-- reset the pseudo-element to normal */
}

Controlling the line-height of a <br> tag within a heading tag?

I have a heading (<h1>) that has a sort of tagline below it. (I used a <small> tag and set font-size to a certain percent so it lines up perfectly when I change font-size of the heading for smaller screens. I'm using em units, if that matters.)
At first, the <small> tag sat nicely underneath the main heading, but I realized I forgot the HTML5 DOCTYPE declaration. So, after I discovered this omission and corrected it, the spacing was all wrong.
Here's my code:
HTML:
<h1 class="banner">Justin Wilson<br /><small>WEB + GRAPHIC DESIGNER</small></h1>
CSS:
h1.banner {
text-align: center;
display: block;
font-family: 'arvil';
font-size: 6.5em;
color: #94babd; }
h1.banner > small {
font-family: Helvetica, Arial, sans-serif;
font-size: 27%;
color: #888;
letter-spacing: 1px;
font-weight: 100; }
And here's the before and after:
I have searched through StackOverflow, but I'm not sure how to proceed. I've read that a <br /> tag simply line breaks, but it inherits the line-spacing, and line-spacing: (value) does not work, nor do margins or padding.
What I need is a simple, cross-browser solution. I used Chrome for the screenshot. Support for IE6-7 is not needed, though support for IE8 would be nice.
The problem is caused by the default line height for the heading element. The default depends on the browser and on the font, but it tends to be about 1.1 to 1.3 times the font size. In any case, with a very large font size set, this creates a problem, because the line height value also sets the height of the second line. By CSS specifications, for a block element, line-height sets the minimum height of line boxes.
There are various ways around this. Setting display: block on the small element is one way, since then its default line height will be determined according to its own font size. Another way is to set a line height that is considerably smaller than the font size, e.g.
h1.banner { line-height: 0.5; }
You need to control the line-height css property (see W3 Schools) to make sure all browsers set the same height for each line.
It's actually advisable to do this to pretty much all elements containing text, which is why most people use CSS resets for production, which sets a default line-height across all elements.
In this case, the <span> and <h1> will likely have different line heights.
I'm sure the <br /> tag is doing nothing wrong, unless you've altered its properties with CSS which I would not advise.
There's also a shorthand version in case you're setting other font properties for the same element(s):
font: <font weight> <font size>/<line height> <font face>;
For example:
font: bold 12px/18px sans-serif;
Hope this helps.
Drop the <br /> and set the display of the <small> element to block.
http://cssdeck.com/labs/uoqfo4xw
<h1 class="banner">Justin Wilson <small>WEB + GRAPHIC DESIGNER</small></h1>
h1.banner {
text-align: center;
display: block;
font-family: 'arvil';
font-size: 6.5em;
color: #94babd; }
h1.banner > small {
font-family: Helvetica, Arial, sans-serif;
font-size: 27%;
color: #888;
letter-spacing: 1px;
font-weight: 100;
display: block; }
An alternative is to set the span to display: block; and then adjust the line-height of the <h2> tag.
I would do this, instead of using a <br /> tag.
Ultimately the answer that works as the best solution is found here (3rd example):
http://www.w3.org/html/wg/drafts/html/master/common-idioms.html#sub-head
#cimmanon posted a very helpful solution, but to be safe, I'll stick with what Steve Faulkner said about this being a more semantic and SEO-friendly solution over using an <h1> tag and <h2> tag for a subheading, or inline elements inside a heading tag, regardless of styling.
Here's the solution:
HTML:
<header>
<h1 class="banner">Justin Wilson</h1>
<p>WEB + GRAPHIC DESIGNER</p>
</header>
CSS:
h1.banner {
text-align: center;
display: block;
font-family: 'arvil';
font-size: 6.5em;
color: #94babd;
font-weight: normal;
margin: 1em 0 0 0;/*purely for display purposes, really*/ }
header > p {font-family: Helvetica, sans-serif;
font-size: 1.75em;
color: #888;
letter-spacing: 1px;
font-weight: 100;
text-align: center;
margin:0; }
And the live demo (might not look good to you without the free Arvil font, from Lost Fonts).
http://cssdeck.com/labs/xysgkffs
Thanks for all the answers thus far.
UPDATED....
Change <small> to <p> in HTML and CSS and add line to the h1.banner > p
margin: 0 auto;
FIDDLE