css中white-space 属性:“pre”, “pre-line”, “pre-wrap” “nowrap”的区别

在css中white-space属性用来控制容器的文本中带有空白符、制表符、换行符等的显示,取值有:

  • normal:默认,忽略文本中所有的空白、换行符;只有文本存在 <br> 或文本达到框的约束时,文本才会换行
  • nowrap:和normal类似,忽略文本中所有的空白、换行符;遇到框的宽度约束时不会自动换行,文本只有在有 br 时才会换行
  • pre:保留文本中的空白、换行符;遇到框的宽度约束时不会自动换行,文本存在 <br> 或文本中有换行符时,文本才会换行
  • pre-wrap:和pre类似,保留文本中的空白、换行符;文本存在 <br> 或文本中有换行符时,或者遇到框的宽度约束时,文本都才会换行
  • pre-line:会略文本中的空白符;文本存在 <br> 或文本中有换行符时,或者遇到框的宽度约束时,文本都才会换行

示例:

.container {
  background-color: lightgreen;
  width: 300px;
}
.normal {
  white-space: normal;
}
.no-wrap {
  white-space: nowrap;
}
.pre {
  white-space: pre;
}
.pre-line {
  white-space: pre-line;
}
.pre-wrap {
  white-space: pre-wrap;
}

html模板:

<div class="container">

   Lorem         ipsum      dolor sit amet consectetur adipisicing elit.
   Unde velit       ullam iste labore earum.
   Nam    ea exercitationem <br> aspernatur <br> ipsum
</div>

1、white-space: normal

white-spaces: normal is the default value. All the source line break, extra white space/tab is collapsed. The text will only break into new line when there is <br> or if the text hit the constraint of the box.

效果:

2、white-space: nowrap

All the extra spacing/line break is collapsed.
Do not automatically break into new line when hit the width constraint of the box. Text will only break into new line when it has br

效果

3、white-space: pre

Sequences of white space are preserved. Lines are only broken at newline characters in the source and at <br> elements.

The text do not break into new line even when it hit the width constraint of the box. It will only break into new line if the source has new line or when it has <br>

效果

 4、white-space: pre-wrap

Sequences of white space are preserved. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

pre-wrap is very similar to pre The only differences is the text will automatically break into a new line when it hit the width constraint of the box.

效果

5、white-space: pre-line

Sequences of white space are collapsed. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

Source line break is preserved line break 1 – 6 xtra spacing is collapsed into single spacing. Text will automatically break into new line when hit the width constraint of the box , it has<br> and the source has new line.

效果

https://medium.com/@tohbansoon/differences-between-pre-pre-line-pre-wrap-and-no-wrap-in-css-1fe4b0f72699

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值