html (+css): denoting a preferred place for a line break

北城余情 提交于 2019-11-26 21:40:39
Eggert Jóhannesson

By using

span.avoidwrap { display:inline-block; }

and wrapping the text I want to be kept together in

<span class="avoidwrap"> Text </span>

it will wrap first in preferred blocks and then in smaller fragments as needed.

Stephan Weinhold

There's a very neat RWD-solution from Dan Mall that I prefer. I'm going to add it here because some other questions regarding responsive line breaks are marked as duplicates of this one.
In your case you'd have:

<span>Honey Nut Cheerios, <br class="rwd-break">Wheat Chex, etc.</span>

And one line of CSS in you media query:

@media screen and (min-width: 768px) {
    .rwd-break { display: none; }
}

An easy answer is to use the zero-width space character &#8203; It is used for making breakspaces inside words at specific points.

Does the exact opposite of the non-breaking space &nbsp; (well, actually the word-joiner &#8288;)(word-joiner is the zero-width version of non-breaking space)

(there are also other non breaking codes like the non-breaking hyphen &#8209;)(here is an extensive answer on different 'variants' of nbsp)

If you want an HTML-only (no CSS/JS) solution you could use a combination of the zero-width space and the non-breaking space, however this would be really messy, and writing a human-readable version requires a little effort.

ctrl + c, ctrl + v helps

example:

   Honey&nbsp;Nut&nbsp;Cheerios,<!---->&#8203;<!--
-->Wheat&nbsp;Chex,<!---->&#8203;<!--
-->Grape&#8209;Nuts,<!---->&#8203;<!--
-->Rice&nbsp;Krispies,<!---->&#8203;<!--
-->Some&nbsp;random&nbsp;cereal&nbsp;with&nbsp;a&nbsp;very&nbsp;long&nbsp;name,<!---->&#8203;<!--
-->Honey&nbsp;Bunches&nbsp;of&nbsp;Oats,<!---->&#8203;<!--
-->Wheaties,<!---->&#8203;<!--
-->Special&nbsp;K,<!---->&#8203;<!--
-->Froot&nbsp;Loops,<!---->&#8203;<!--
-->Apple&nbsp;Jacks

unreadable? this is the same HTML with no comment tags:

   Honey&nbsp;Nut&nbsp;Cheerios,&#8203;Wheat&nbsp;Chex,&#8203;Grape&#8209;Nuts,&#8203;Rice&nbsp;Krispies,&#8203;Some&nbsp;random&nbsp;cereal&nbsp;with&nbsp;a&nbsp;very&nbsp;long&nbsp;name,&#8203;Honey&nbsp;Bunches&nbsp;of&nbsp;Oats,&#8203;Wheaties,&#8203;Special&nbsp;K,&#8203;Froot&nbsp;Loops,&#8203;Apple&nbsp;Jacks

However, since email html rendering is not completely standardized, its good for that kind of use since this solution uses no CSS/JS

Also, if you use this in combination with any of the <span>-based solutions, you will have complete control of the line-breaking algorithm

(editorial note:)

The only problem I could see you having is if you wanted to change the points of preferred breakage dynamically. This would require constant JS manipulation of each of the spans proportionate size, and having to handle those HTML entities in the text.

Gabriele Petrioli

The answer is no (You cannot alter the line breaking algorithm used).

But there are some workarounds (best one is the accepted answer)

You can go near with the non-breaking-space &nbsp; but only between words that go together (what you have in spans, but not after the comma ), or you can use the white-space:nowrap as @Marcel mentioned.

Both solutions do the same thing, and both will not break a group of words if it does not fit on its own.

With your mark-up above use span { white-space:nowrap }. It's as good as you can expect really.

New answer now we have HTML5:

HTML5 introduces the <wbr> tag. (It stands for Word Break Opportunity.)

Adding a <wbr> tells the browser to break there before anywhere else, so it's easy to make words break after commas:

Honey Nut Cheerios,<wbr> Wheat Chex,<wbr> Grape-Nuts,<wbr> Rice Krispies,<wbr> Some random cereal with a very long name,<wbr> Honey Bunches of Oats,<wbr> Wheaties,<wbr> Special K,<wbr> Froot Loops,<wbr> Apple Jacks

It is supported my all major browsers apart from IE.

You can just adjust the margin settings in CSS (margin-right in this case).

text {
    margin-right: 20%;
}

Use <div> instead of <span>, or specify a class for SPAN and give it the display:block attribute.

There’s an HTML element for that™: the (now standardized) <wbr> element.

I’d advise you to use that. It may not work everywhere, but it’s the best you can do without going through hoops.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!