programing

html (+ css) : 줄 바꿈을위한 선호 위치 표시

nasanasas 2020. 8. 19. 08:17
반응형

html (+ css) : 줄 바꿈을위한 선호 위치 표시


HTML 표 셀에 표시하려는 텍스트가 있다고 가정 해 보겠습니다.

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

그리고 나는 쉼표 중 하나 다음에 우선적으로 줄을 나누기를 원합니다.

HTML 렌더러에게 지정된 지점에서 중단을 시도하고 중단 되지 않는 공백 사용하지 않고 공백 중 하나를 중단하기 전에 먼저 중단하도록 지시하는 방법이 있습니까? 나누지 않는 공백을 사용하면 무조건 너비가 커집니다. 내가 원하는 라인 배치 알고리즘은 먼저 쉼표로 그것을 시도하고 맞게 라인을 얻을 수없는 경우 줄 바꿈, 공간의 한 이후에 발생할 수 있습니다.

<span>요소 에서 텍스트 조각을 래핑하려고 시도했지만 도움이되지 않는 것 같습니다.

<html>
  <head>
      <style type="text/css">
        div.box { width: 180px; }
        table, table td { 
          border: 1px solid; 
          border-collapse: collapse; 
        }
      </style>
  </head>
  <body>
    <div class="box">
      <table>
      <tr>
          <td>lorem ipsum</td>
          <td>lorem ipsum</td>
          <td>lorem ipsum</td>
      </tr>
      <tr>
          <td>lorem ipsum</td>
          <td>
            <span>Honey Nut Cheerios,</span>
            <span>Wheat Chex,</span>
            <span>Grape-Nuts,</span>
            <span>Rice Krispies,</span>
            <span>Some random cereal with a very long name,</span>
            <span>Honey Bunches of Oats,</span>
            <span>Wheaties,</span>
            <span>Special K,</span>
            <span>Froot Loops,</span>
            <span>Apple Jacks</span>
          </td>
          <td>lorem ipsum</td>
      </tr>
      </table>
    </div>
  </body>
</html>

참고 : CSS3text-wrap: avoid 동작이 내가 원하는 것처럼 보이지만 제대로 작동하지 않는 것 같습니다.


사용하여

span.avoidwrap { display:inline-block; }

함께 보관하고 싶은 텍스트를 감싸서

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

먼저 선호하는 블록으로 래핑 한 다음 필요에 따라 작은 조각으로 래핑합니다.


내가 선호하는 Dan Mall 의 매우 깔끔한 RWD 솔루션이 있습니다 . 반응 형 줄 바꿈에 관한 다른 질문이이 질문의 중복으로 표시되기 때문에 여기에 추가하겠습니다.
귀하의 경우에는 다음이 필요합니다.

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

미디어 쿼리의 CSS 한 줄 :

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

쉬운 대답은 너비 0 인 공백 문자&#8203; 를 사용하는 것입니다 . 특정 지점에서 단어 내부에 브레이크 스페이스 를 만드는 데 사용됩니다 .

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.


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.

참고URL : https://stackoverflow.com/questions/5392853/html-css-denoting-a-preferred-place-for-a-line-break

반응형