📜  CSS |空白属性

📅  最后修改于: 2021-08-30 10:31:46             🧑  作者: Mango

CSS 中的white-space 属性用于控制文本换行和 white-spacing。此属性中有多种类型的值可供使用。

句法:

white-space: normal|nowrap|pre|pre-line|pre-wrap|initial|inherit;

属性值:

  • normal:这是该属性的默认值。当 CSS 的 white-space 属性设置为 normal 时,每个两个或多个空格的序列都将显示为单个空格。元素中的内容将在必要时换行。
    句法:
    white-space : normal; 

    例子 :

    
    
      
    
        
            CSS | white-space Property
        </tile>
        <style>
            div {
                width: 500px;
                height: 500px;
                white-space: normal;
                background-color: limegreen;
                color: white;
                font-size: 80px;
            }
        </style>
    </head>
      
    <body>
        <center>
            <div>
                Geeks For Geeks:
                <br>
                A Computer Science Portal For Geeks.
            </div>
        </center>
    </body>
      
    </html>
    </code></pre>
    </div>
    <p><strong>输出 :</strong> </p>
    <div class="wp-caption aligncenter" id="attachment_820816" style="width:310px"><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/CSS_%7C_white-space_Property_0.jpg"/>
    <p class="wp-caption-text">具有正常值的空白属性 css</p>
    </div>
    </li>
    <li> <strong>nowrap :</strong>当 CSS 的 white-space 属性设置为 nowrap 时,两个或多个空格的每个序列都将显示为单个空格。除非明确指定,否则元素中的内容不会换行。<br/><strong>句法:</strong>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">white-space : nowrap; </code></pre>
    </div>
    <p><strong>例子 :</strong> </p>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace"><!DOCTYPE html>
    <html>
      
    <head>
        <title>
            CSS | white-space Property
        </tile>
        <style>
            div {
                width: 300px;
                height: 300px;
                white-space: nowrap;
                background-color: lightgreen;
                color: black;
                font-size: 25px;
            }
        </style>
    </head>
      
    <body>
        <center>
            <div>
                Geeks For Geeks: 
                A Computer Science Portal For Geeks.
            </div>
        </center>
    </body>
      
    </html>
    </code></pre>
    </div>
    <p><strong>输出 :</strong> </p>
    <div class="wp-caption aligncenter" id="attachment_820964" style="width:310px"><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/CSS_%7C_white-space_Property_1.jpg"/>
    <p class="wp-caption-text">具有 nowrap 值的空白属性</p>
    </div>
    </li>
    <li><strong>pre:</strong>该值使空白与 HTML 中的 <pre> 标签具有相同的效果。元素中的内容仅在使用换行符指定时才会换行。<br/><strong>句法:</strong>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">white-space : pre; </code></pre>
    </div>
    <p><strong>例子:</strong> </p>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace"><!DOCTYPE html>
    <html>
      
    <head>
        <title>
            CSS | white-space Property
        </tile>
        <style>
            div {
                width: 300px;
                height: 300px;
                white-space: pre;
                background-color: lightgreen;
                color: black;
                font-size: 25px;
            }
        </style>
    </head>
      
    <body>
        <center>
            <div>
                Geeks For Geeks:
                A Computer   science portal    for geeks.
            </div>
        </center>
    </body>
      
    </html>
    </code></pre>
    </div>
    <p><strong>输出 :</strong> </p>
    <div class="wp-caption aligncenter" id="attachment_820970" style="width:310px"><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/CSS_%7C_white-space_Property_2.jpg"/>
    <p class="wp-caption-text">具有预值的空白属性</p>
    </div>
    </li>
    <li><strong>pre-line:</strong>当 CSS 的 white-space 属性设置为 pre-line 值时,每两个或多个空格的序列将显示为单个空格。元素中的内容将在需要和明确指定时被包装。<br/><strong>句法:</strong>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">white-space : pre-line; </code></pre>
    </div>
    <p><strong>例子 :</strong> </p>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace"><!DOCTYPE html>
    <html>
      
    <head>
        <title>
            CSS | white-space Property
        </tile>
        <style>
            div {
                width: 300px;
                height: 300px;
                white-space: pre-line;
                background-color: lightgreen;
                color: black;
                font-size: 25px;
            }
        </style>
    </head>
      
    <body>
        <center>
            <div>
                Geeks For Geeks:    A science portal for geeks.
            </div>
        </center>
    </body>
      
    </html>
    </code></pre>
    </div>
    <p><strong>输出 :</strong> </p>
    <div class="wp-caption aligncenter" id="attachment_820986" style="width:310px"><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/CSS_%7C_white-space_Property_3.jpg"/>
    <p class="wp-caption-text">空白属性行前值</p>
    </div>
    </li>
    <li><strong>pre-wrap:</strong>当 CSS 的 white-space 属性设置为 pre-line 值时,每个空格序列都会按原样显示。元素中的内容将在需要和明确指定时被包装。<br/><strong>句法:</strong>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">white-space : pre-wrap; </code></pre>
    </div>
    <p><strong>例子 :</strong> </p>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace"><!DOCTYPE html>
    <html>
      
    <head>
        <title>
            CSS | white-space Property
        </tile>
        <style>
            div {
                width: 300px;
                height: 300px;
                white-space: pre-wrap;
                background-color: lightgreen;
                color: black;
                font-size: 25px;
            }
        </style>
    </head>
      
    <body>
        <center>
            <div>
                Geeks For Geeks:    A science portal for geeks.
            </div>
        </center>
    </body>
      
    </html>
    </code></pre>
    </div>
    <p><strong>输出 :</strong> </p>
    <div class="wp-caption aligncenter" id="attachment_820995" style="width:310px"><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/CSS_%7C_white-space_Property_4.jpg"/>
    <p class="wp-caption-text">空白预包装值</p>
    </div>
    </li>
    <li><strong>初始</strong>值:此值将空白属性设置为默认值。<br/><strong>句法:</strong>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">white-space : initial; </code></pre>
    </div>
    </li>
    <li><strong>继承:</strong>此值将空白属性设置为父元素的值。<br/><strong>句法:</strong>
    <div class="hcb_wrap">
    <pre class="prism line-numbers lang-css" data-lang="CSS"><code class="replace">white-space : inherit; </code></pre>
    </div>
    </li>
    </ul>
    <p><strong>支持的浏览器:</strong> <strong>white-space 属性</strong>支持的浏览器如下:</p>
    <ul>
    <li>铬:1.0</li>
    <li>边缘:8.0</li>
    <li>火狐:3.5</li>
    <li>歌剧:9.5</li>
    <li> Safari:3.0 </li>
    </ul>
    <div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7"></div>
    <p></p></div>
    
                    </div>
    
                </div>
            </div>
        </div>
    </div>
    
    
      
    <footer>
        <div class="bg-white text-center pb-1">
            <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有
                <br>
    
                <a href="https://beian.miit.gov.cn/" target="_blank"
                    class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a>
    
                <br>
                Made with ❤️ in Chengdu
            </p>
        </div>
    </footer>
    
    
      <!-- 引入Bootstrap JavaScript库 -->
      <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
        crossorigin="anonymous"></script>
    
      <!-- 引入Meilisearch搜索相关的JavaScript库 -->
      <script
        src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
      <script src="https://www.imangodoc.com/static/javascript/meili_custom.js"></script>
    
      <!-- 引入prismjs代码高亮相关的JavaScript库 -->
      <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
      <script
        src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
    
    </body>
    
    
    </html>