css中的white-space属性

本文详细介绍了CSS中white-space属性的功能及用法,包括如何处理元素内的空白符、换行符,以及不同属性值(如normal、pre、nowrap等)的效果对比。

css中的white-space用来处理布局过程中元素内的空白符。

white-sapce:

normol: 默认值,空白会被浏览器忽略。

<html>
    <head>
        <title>WhiteSpace</title>
        <style type = "text/css">
        span {
            white-space: normal;
            background: silver;
        }
        </style>
    </head>
    <body>
        <span>      
                Some text。
            Some text。
        Some text。
        </span>
    </body>
</html>

运行结果
元素标签<span>两端的空白符被忽略,但字符之间的空白保留。

pre: 元素之间的空白符不被忽略。类似HTML标签中的<pre>

<html>
    <head>
        <title>WhiteSpace</title>
        <style type = "text/css">
        span {
            white-space: normal;
            background: silver;
        }
        </style>
    </head>
    <body>
        <span>      
                Some text。
            Some text。
        Some text。
        </span>
    </body>
</html>

运行结果(火狐):

可以看到标签元素<span>之间的空白符被保留了。

nowrap: 文本不会换行,文本会在一行之间继续,直到遇到<br />标签

<html>
    <head>
        <title>WhiteSpace</title>
        <style type = "text/css">
        span {
            white-space: nowrap;
            background: silver;
        }
        div {
            width: 200px;
            background: gold;
            padding: 10px;
        }
        </style>
    </head>
    <body>
    <div>
        <span>      
                    Some text。
            Some text。
        Some text。
        Some text。
        Some text。
        Some text。
        Some text。
        Some text。
        </span>
    </div>
    </body>
</html>

 

运行结果:

元素标签<span>之间的空格以及每行换行符被忽略。文本但是文本不会自动换行,一直溢出了div框。

<html>
    <head>
        <title>WhiteSpace</title>
        <style type = "text/css">
        span {
            white-space: pre-wrap;
            background: silver;
        }
        </style>
    </head>
    <body>
        <span>      
                    Some text。
            Some text。
        Some text。
        </span>
    </body>
</html>

运行结果(火狐):

 

元素标签<span>之间的空白被保留,换行符也被保留。

pre-line: 合并空白符序,但是保留换行符。

<html>
    <head>
        <title>WhiteSpace</title>
        <style type = "text/css">
        span {
            white-space: pre-line;
            background: silver;
        }
        </style>
    </head>
    <body>
        <span>      
                    Some text。
            Some text。
        Some text。
        </span>
    </body>
</html>

运行结果:

元素标签<span>之间的空白符被忽略,但是换行符被保留。

总之,white-space控制的就是表亲两端的空白符以及换行符的显示。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值