CSS / Reference / white-space

CSS의 white-space는 연속 공백과 원문 줄바꿈을 유지할지, 상자 너비에 맞춰 자동으로 줄을 바꿀지 정합니다. 일반 문단에는 normal, 줄바꿈과 들여쓰기를 유지할 로그나 입력 메시지에는 pre-wrap을 사용합니다. 줄바꿈만 남기고 연속 공백을 줄이려면 pre-line이 맞습니다.

공백과 자동 줄바꿈에 맞는 값 고르기

기본값은 normal이며 부모에서 자식으로 상속됩니다. 원문 줄바꿈은 HTML 소스의 텍스트에 들어 있는 개행을 뜻합니다. <br>처럼 명시한 줄바꿈은 nowrap에서도 동작합니다. 각 값은 CSS white-space 명세에 정의돼 있습니다.

값 연속 공백과 탭 원문 줄바꿈 자동 줄바꿈
normal 병합 병합 허용
nowrap 병합 병합 안 함
pre 보존 보존 안 함
pre-wrap 보존 보존 허용
pre-line 병합 보존 허용
break-spaces 보존하며 줄 끝에서도 너비 차지 보존 보존된 각 공백 뒤에서도 가능
.copy { white-space: normal; }
.log { white-space: pre-wrap; }
.code-line { white-space: pre; overflow-x: auto; }

짧은 버튼 라벨을 한 줄로 유지하고 싶다면 대상에만 nowrap을 지정하고 좁은 화면에서 글자가 넘치는지 확인하세요. 코드처럼 원래 줄 길이까지 유지할 영역에는 위의 .code-line처럼 내부 가로 스크롤을 허용합니다.

같은 텍스트로 여섯 값 비교하기

아래 전체 코드를 UTF-8로 white-space-example.html에 저장해 브라우저로 여세요. 첫 번째 구역의 각 상자에는 A와 B 사이에 공백 네 개, B 뒤에 개행, C와 D 사이에 탭이 들어 있습니다. 세 구역 모두 외부 CSS나 JavaScript 없이 작동합니다.

<!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS white-space 비교</title>
  <style>
    body { margin: 1rem; color: #20242b; background: #fff;
      font: 1rem/1.55 system-ui, sans-serif; }
    main { max-width: 58rem; margin-inline: auto; }
    .samples { display: grid; gap: 1rem;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr)); }
    figure { margin: 0; min-width: 0; }
    figcaption { margin-bottom: .35rem; overflow-wrap: anywhere; }
    .sample { width: 22ch; max-width: 100%; overflow: auto;
      font: 1rem/24px ui-monospace, monospace; tab-size: 4;
      background: #edf4f8; outline: 1px solid #60727b; }
    .sample:focus-visible { outline: 3px solid #005fcc; }
    .narrow { width: 4ch; }
    .anywhere { overflow-wrap: anywhere; }
    .normal { white-space: normal; }
    .nowrap { white-space: nowrap; }
    .pre { white-space: pre; }
    .pre-wrap { white-space: pre-wrap; }
    .pre-line { white-space: pre-line; }
    .break-spaces { white-space: break-spaces; }
  </style>
</head>
<body>
  <main>
    <h1>CSS white-space 비교</h1>
    <h2>같은 원문으로 여섯 값 비교</h2>
    <div class="samples">
      <figure>
        <figcaption id="mode-normal-label"><code>normal</code></figcaption>
        <div id="mode-normal" class="sample normal">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-nowrap-label"><code>nowrap</code></figcaption>
        <div id="mode-nowrap" class="sample nowrap" tabindex="0" role="region" aria-labelledby="mode-nowrap-label">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-pre-label"><code>pre</code></figcaption>
        <div id="mode-pre" class="sample pre" tabindex="0" role="region" aria-labelledby="mode-pre-label">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-pre-wrap-label"><code>pre-wrap</code></figcaption>
        <div id="mode-pre-wrap" class="sample pre-wrap">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-pre-line-label"><code>pre-line</code></figcaption>
        <div id="mode-pre-line" class="sample pre-line">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-break-spaces-label"><code>break-spaces</code></figcaption>
        <div id="mode-break-spaces" class="sample break-spaces">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
    </div>
    <h2>줄 끝 공백 여섯 개 비교</h2>
    <div class="samples">
      <figure>
        <figcaption id="end-pre-wrap-label"><code>pre-wrap</code></figcaption>
        <div id="end-pre-wrap" class="sample narrow pre-wrap">AB&#32;&#32;&#32;&#32;&#32;&#32;</div>
      </figure>
      <figure>
        <figcaption id="end-break-spaces-label"><code>break-spaces</code></figcaption>
        <div id="end-break-spaces" class="sample narrow break-spaces">AB&#32;&#32;&#32;&#32;&#32;&#32;</div>
      </figure>
    </div>
    <h2>공백 없는 긴 문자열</h2>
    <div class="samples">
      <figure>
        <figcaption id="token-0-label"><code>overflow-wrap: normal</code></figcaption>
        <div id="token-0" class="sample normal" tabindex="0" role="region" aria-labelledby="token-0-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
      </figure>
      <figure>
        <figcaption id="token-1-label"><code>overflow-wrap: anywhere</code></figcaption>
        <div id="token-1" class="sample normal anywhere">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
      </figure>
      <figure>
        <figcaption id="token-2-label"><code>nowrap + anywhere</code></figcaption>
        <div id="token-2" class="sample nowrap anywhere" tabindex="0" role="region" aria-labelledby="token-2-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
      </figure>
    </div>
  </main>
</body>
</html>

normal에서는 간격이 좁아지고 C가 원문 개행으로 새 줄에 들어가지 않습니다. nowrap은 자동 줄바꿈까지 막습니다. pre-line에서는 C가 새 줄에 나오지만 반복 공백과 탭은 접힙니다. pre와 pre-wrap은 모두 간격과 개행을 유지하고, 긴 줄이 너비에 맞춰 감싸지는 쪽은 pre-wrap입니다.

pre-wrap과 break-spaces의 끝 공백 비교하기

pre-wrap에서는 보존된 공백이 줄 끝에서 가용 너비 바깥으로 걸릴 수 있습니다. break-spaces에서는 줄 끝의 공백도 글자처럼 공간을 차지하며 각 공백 뒤에 줄바꿈 기회가 생깁니다. 세부 규칙은 CSS 공백 처리 규칙를 참고하세요.

두 번째 구역은 폭을 글자 약 네 개만큼 고정하고 AB 뒤에 공백 여섯 개를 넣습니다. &#32;는 공백 한 칸을 넣는 HTML 문자 참조입니다. pre-wrap 상자는 한 줄이고 break-spaces 상자는 뒤의 공백 때문에 두 줄이 됩니다. 공백을 한두 개씩 지웠다가 되돌리며 줄 수가 달라지는 지점을 확인하세요.

값 줄 수 미리보기
pre-wrap 1
break-spaces 2

긴 문자열이 컨테이너를 넘을 때 처리하기

감싸기가 허용돼도 공백 없는 긴 식별자는 정상적으로 끊을 곳이 없으면 넘칠 수 있습니다. 세 번째 구역에서 overflow-wrap: normal은 긴 문자열을 한 줄에 두고, anywhere는 필요할 때 문자열 중간도 줄을 바꿉니다. nowrap과 함께 쓰면 anywhere를 추가해도 자동 줄바꿈이 생기지 않습니다. word-wrap은 overflow-wrap의 이전 이름입니다. 단어 내부의 일반적인 분리 규칙을 바꿔야 할 때는 word-break도 살펴보세요.

.message {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  min-width: 0;
}

위 코드의 .message에 해당하는 메시지 본문 요소에 적용합니다. 플렉스나 그리드의 항목이 줄어들지 않는다면 min-width: 0을 그 항목에 지정해 보세요. 넘치는 상자의 폭과 스크롤은 넘치는 콘텐츠와 긴 문자열 처리하기에서 확인할 수 있습니다. 이전 이름을 사용하는 코드는 word-wrap을 참고하세요.

실제 공백과 적용된 CSS 확인하기

개발자 도구에서 텍스트 노드에 공백과 개행이 실제로 남아 있는지 먼저 확인합니다. 이 속성은 표시만 바꾸므로 편집기에서 이미 제거한 공백을 되살릴 수 없습니다. 다음으로 계산된 스타일에서 white-space와 overflow-wrap을 확인하고, 상위 요소에서 상속된 nowrap이 있는지 살펴보세요.

두 키워드 문법을 지원하는 브라우저에서는 white-space: preserve wrap이 pre-wrap과 같습니다. preserve는 white-space-collapse의 값이고, wrap은 text-wrap-mode의 값입니다. 기존 브라우저까지 지원할 때는 앞의 단일 키워드로 먼저 확인할 수 있습니다.