當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


CSS visibility屬性用法及代碼示例


此屬性用於指定元素在Web文檔中是否可見,但隱藏的元素會占用Web文檔中的空間。使用display屬性可刪除或顯示屬性以從瀏覽器中隱藏和刪除元素。

用法:

visibility:visible|hidden|collapse|initial|inherit;

屬性值:

  • visible:它是默認值。該元素在網絡文檔中正常顯示或可見。

    用法:

    visibility:hidden;

    例:



    <!DOCTYPE html> 
    <html> 
        <head> 
            <title> 
                CSS | visibility Property 
            </title> 
            <style> 
                h1 { 
                    color:green; 
                } 
                .geeks { 
                    visibility:visible; 
                } 
                body { 
                    text-align:center; 
                } 
            </style> 
        </head> 
        <body> 
            <h1>GeeksforGeeks</h1> 
            <h2>visibility:visible;</h2> 
            <p class="geeks"> 
             A computer science portal for geeks 
            </p> 
        </body> 
    </html>                                

    輸出:

  • hidden:此屬性從頁麵隱藏元素,但會占用文檔中的空間。

    用法:

    visibility:hidden;

    例:

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title> 
                CSS | visibility Property 
            </title> 
            <style> 
                h1 { 
                    color:green; 
                } 
                .geeks { 
                    visibility:hidden; 
                } 
                body { 
                    text-align:center; 
                } 
            </style> 
        </head> 
        <body> 
            <h1>GeeksforGeeks</h1> 
            <h2>visibility:hidden;</h2> 
            <p class="geeks"> 
             A computer science portal for geeks 
            </p> 
        </body> 
    </html>                    

    輸出:

  • collapse:此屬性僅用於表格元素。它用於從表中刪除行和列,但不影響表的布局。但是它們的空間可用於其他內容。
    Note:此屬性不用於表格元素以外的其他元素。

    用法:

    visibility:collapse;

    例:

    <!DOCTYPE html> 
    <html> 
        <head> 
            <title> 
                CSS | visibility Property 
            </title> 
            <style> 
                table.geeks { 
                    visibility:collapse 
                } 
                table, th, td { 
                border:1px solid red; 
                p { 
                color:green; 
                font-size:25px; 
                } 
            </style> 
        </head> 
        <body> 
            <center> 
            <h1 style="color:green;">GeeksForGeeks</h1> 
            <h2>visibility:collapse;</h2> 
            <p>geeksforgeeks</p> 
            <table style="border:1px solid red;" class="geeks"> 
                <tr> 
                    <th>geeks</th> 
                    <th>for</th> 
                    <th>geeks</th> 
                </tr> 
            </table> 
            <p>A computer science portal for geeks</p> 
            </center> 
        </body> 
    </html>                    

    輸出:

支持的瀏覽器:可見性屬性支持的瀏覽器如下:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • Opera
  • Safari




相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 CSS | visibility Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。