搜尋
首頁web前端css教學CSS隱藏頁面元素的5種方法說明

用 CSS 隱藏頁面元素有許多種方法。你可以將 opacity 設為 0、將 visibility 設為 hidden、將 display 設為 none 或將 position 設為 absolute 然後將位置設為不可見區域。

你有沒有想過,為什麼我們要有這麼多技術來隱藏元素,而它們看起來都實現的是同樣的效果?每一種方法實際上與其他方法之間都有一些細微的不同,這些方法決定了在一個特定的場合下使用哪一個方法。這篇教學將涵蓋到那些你需要記住的細小不同點,讓你根據不同情況選擇上面這些方法中適合的方法來隱藏元素。

Opacity

opacity 屬性的意思是設定一個元素的透明度。它不是為改變元素的邊界框(bounding box)而設計的。這意味著將 opacity 設為 0 只能從視覺上隱藏元素。而元素本身依然佔據它自己的位置並對網頁的佈局起作用。它也將響應用戶互動。

.hide {
  opacity: 0;
}

如果你打算使用opacity 屬性在讀取螢幕軟體中隱藏元素,很不幸,你並不能如願。元素和它所有的內容會被讀屏軟體閱讀,就像網頁上的其他元素一樣。換句話說,元素的行為就和它們不透明時一致。

我還要提醒一句,opacity 屬性可以用來實現一些效果很棒的動畫。任何 opacity 屬性值小於 1 的元素也會建立一個新的堆疊上下文(stacking context)。

當你的滑鼠移到被隱藏的第 2 個的區塊上,元素狀態平滑地從完全透明過渡到完全不透明。區塊也將 cursor 屬性設定為了 pointer,這說明了用戶可以與它互動。

Visibility

第二個要說的屬性是 visibility。將它的值設為 hidden 將隱藏我們的元素。如同 opacity 屬性,被隱藏的元素仍然會對我們的網頁佈局起作用。與 opacity 唯一不同的是它不會響應任何用戶互動。此外,元素在讀取螢幕軟體中也會被隱藏。

這個屬性也能夠實現動畫效果,只要它的初始和結束狀態不一樣。這確保了 visibility 狀態切換之間的過渡動畫可以是時間平滑的(事實上可以用這一點來用 hidden 實現元素的延遲顯示和隱藏——譯者註)。

.hide {
   visibility: hidden;
}

注意,如果一個元素的visibility 被設定為hidden,同時想要顯示它的某個子孫元素,只要將那個元素的visibility 明確設定為visible 即可(就如例子裡面的.o-hide p-譯者註)。試著只 hover 在隱藏元素上,不要 hover 在 p 標籤裡的數字上,你會發現你的滑鼠遊標沒有變成手指頭的樣子。此時,你點擊滑鼠,你的 click 事件也不會被觸發。

而在

標籤裡面的

標籤則依然可以捕捉所有的滑鼠事件。一旦你的滑鼠移動到文字上,

本身變得可見並且事件註冊也隨之生效。

Display

display 屬性依照詞義真正隱藏元素。將 display 屬性設為 none 確保元素不可見且連盒模型也不產生。使用這個屬性,被隱藏的元素不佔據任何空間。不僅如此,一旦 display 設為 none 任何對該元素直接打使用者互動操作都不可能生效。此外,讀螢幕軟體也不會讀到元素的內容。這種方式產生的效果就像元素完全不存在。

任何這個元素的子孫元素也會被同時隱藏。為這個屬性新增過渡動畫是無效的,它的任何不同狀態值之間的切換總是會立即生效。

不過請注意,透過 DOM 依然可以存取到這個元素。因此你可以透過 DOM 來操作它,就像操作其他的元素。

.hide {
   display: none;
}

你將看到第二個區塊元素內有一個

元素,它自己的display 屬性被設定成block,但它仍然不可見。這是 visibility:hidden 和 display:none 的另一個不同之處。在前一個例子裡,將任何子孫元素visibility 明確設定成visible 可以讓它變得可見,但是display 不吃這套,不管自身的display 值是什麼,只要祖先元素的display 是none,它們就都不可見。

現在,將滑鼠移到第一個區塊元素上面幾次,然後點擊它。這個運算會讓第二個區塊元素顯現出來,它其中的數字將會是一個大於 0 的數。這是因為,元素即使被這樣設定成對使用者隱藏,還是可以透過 JavaScript 來進行操作。

Position

Suppose there is an element that you want to interact with, but you don’t want it to affect the layout of your web page. There are no suitable attributes to handle this situation (opacity and visibility affect the layout, display does not affect the layout but cannot directly Interaction - translator's note). In this case, you can only consider moving the element out of the visible area. This method does not affect the layout and keeps the elements operable. Here is the CSS using this approach:

.hide {
position: absolute;
top: -9999px;
left: -9999px;
}

The main principle of this method is to set the top and left of the element to a large enough negative number to make it invisible on the screen. One benefit (or potential drawback) of using this technique is that the content of elements it hides can be read by screen-reading software. This is completely understandable, since you are simply moving the element outside of the visible area so that the user cannot see it.

You should avoid using this method to hide any focusable element, because doing so will cause an unpredictable focus switch when the user gives that element focus. This method is often used when creating custom checkboxes and radio buttons. (Use DOM to simulate checkboxes and radio buttons, but use this method to hide the real checkbox and radio elements to "receive" the focus switch - Translator's Note)

Clip-path

Another way to hide elements is by clipping them. Previously, this could be achieved via the clip property, but this property has been deprecated in favor of a better property called clip-path. Read Nitish Kumar's recent SitePoint article "Introducing the clicp-path attribute" to learn more advanced uses of this attribute.

Remember, the clip-path property is not yet fully supported in IE or Edge. If you want to use external SVG files in your clip-path, browser support is even lower. The code to hide an element using the clip-path attribute looks like this:

.hide {
clip-path: polygon(0px 0px,0px ​​0px,0px ​​0px,0px ​​0px);
}

If you hover the mouse over the first element, it can still affect the second element, even though the second element is hidden via clip-path. If you click on it, it will remove the hidden class and make our element visible from that position. Text in hidden elements can still be read by screen reading software. Many WordPress sites use clip-path or the previous clip to implement text specifically provided for screen reading software.

Although our element itself is no longer visible, it still occupies the size of the rectangle it should occupy, and the elements around it behave as if it were visible. Remember that user interactions such as mouseovers or clicks will not take effect outside the clipping area. In our case, the clipping area size is zero, which means the user will not be able to interact directly with the hidden element. Additionally, this property enables the use of various transition animations to achieve different effects.

Conclusion

In this tutorial, we looked at 5 different ways to hide elements with CSS. Each method is a little different from the others. Knowing what you want to achieve will help you decide which attribute to use, and over time you'll be able to instinctively choose the best approach based on your actual needs.


以上是CSS隱藏頁面元素的5種方法說明的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
什麼是CSS網格?什麼是CSS網格?Apr 30, 2025 pm 03:21 PM

CSS網格是創建複雜,響應式Web佈局的強大工具。它簡化了設計,提高可訪問性並提供了比舊方法更多的控制權。

什麼是CSS Flexbox?什麼是CSS Flexbox?Apr 30, 2025 pm 03:20 PM

文章討論了CSS FlexBox,這是一種佈局方法,用於有效地對齊和分佈響應設計中的空間。它說明了FlexBox用法,將其與CSS網格進行了比較,並詳細瀏覽了瀏覽器支持。

我們如何使用CSS使網站迅速響應?我們如何使用CSS使網站迅速響應?Apr 30, 2025 pm 03:19 PM

本文討論了使用CSS創建響應網站的技術,包括視口元標籤,靈活的網格,流體媒體,媒體查詢和相對單元。它還涵蓋了使用CSS網格和Flexbox一起使用,並推薦CSS框架

CSS盒裝屬性有什麼作用?CSS盒裝屬性有什麼作用?Apr 30, 2025 pm 03:18 PM

本文討論了CSS盒裝屬性,該屬性控制了元素維度的計算方式。它解釋了諸如Content-Box,Border-Box和Padding-Box之類的值,以及它們對佈局設計和形式對齊的影響。

我們如何使用CSS動畫?我們如何使用CSS動畫?Apr 30, 2025 pm 03:17 PM

文章討論使用CSS,關鍵屬性並與JavaScript結合創建動畫。主要問題是瀏覽器兼容性。

我們可以使用CSS向我們的項目添加3D轉換嗎?我們可以使用CSS向我們的項目添加3D轉換嗎?Apr 30, 2025 pm 03:16 PM

文章討論了Web項目的3D轉換,關鍵屬性,瀏覽器兼容性和性能注意事項的討論。 (角色計數:159)

我們如何在CSS中添加梯度?我們如何在CSS中添加梯度?Apr 30, 2025 pm 03:15 PM

文章討論了使用CSS梯度(線性,徑向,重複)來增強網站視覺效果,添加深度,焦點和現代美學。

CSS中的偽元素是什麼?CSS中的偽元素是什麼?Apr 30, 2025 pm 03:14 PM

文章討論了CSS中的偽元素,它們在增強HTML樣式方面的使用以及與偽級的差異。提供實用的例子。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中