小編給大家分享一下CSS中overflow屬性如何使用,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
CSS中overflow屬性是經??梢杂玫降膶傩?,接下來的這篇文章我們就來看看CSS中overflow屬性的具體用法。
我們首先來看一下overflow屬性的值有哪些?
overflow 屬性規定當內容溢出元素框時發生的事情。
overflow有以下四個屬性值
visible:初始值,內容不會被修剪,會呈現在元素框之外。
scroll:內容會被修剪,但是瀏覽器會顯示滾動條以便查看其余的內容。
hidden:內容會被修剪,并且其余內容是不可見的。
auto:如果內容被修剪,則瀏覽器會顯示滾動條以便查看其余的內容。
下面我們來詳細說一說overflow屬性的這四個值
我們來看具體的示例
代碼如下
HTML代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS overflow</title> <link rel="stylesheet" type="text/css" href="sample.css"> </head> <body> <div class="hid"> <p> The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. </p> </div> <br> <div class="scr"> <p> The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. </p> </div> <br> <div class="vis"> <p> The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. </p> </div> </body> </html>
CSS代碼
/*hidden*/ div.hid{ width: 200px; height: 100px; overflow: hidden; background-color: #FF9999; } /*scroll*/ div.scr{ width: 200px; height: 100px; overflow: scroll; background-color: #99FF99; } /*visible*/ div.vis{ width: 200px; height: 100px; overflow: visible; background-color: #9999FF; }
在瀏覽器上顯示如下結果
當overflow的屬性值是hidden時,效果如下
在hidden的情況下,不會顯示出剩下的部分,也不能滾動顯示剩下的內容。
當overflow的屬性值是scroll時,效果如下
在scroll的情況下,沒有顯示的內容可以滾動。默認的情況下,文字在橫向上會折回,垂直方向顯示滾動欄。
在css中設置white-space : nowrap(不自動改行的意思),也可以向橫向滾動。
CSS代碼
div.scr{ width: 200px; height: 100px; white-space:nowrap; overflow: scroll; background-color: #99FF99; }
效果如下
此外,也可以使用overflow-x和overflow-y屬性對垂直和橫向的滾動條的顯示進行更為細致的設置。
當overflow的屬性值是visible時,效果如下
visible的情況下,從盒子中溢出顯示。紫色的部分是div盒子。默認的情況下,文字在div的橫向width中被折回,并在縱向方向上顯示。
這個也和scroll屬性值一樣,在css中設置white-space : nowrap,也可以橫向滾動。
另外,如果不設置盒子的高度,就會自動改變盒子的高度。
div.vis{ width: 200px; /* height: 100px; */ overflow: visible; background-color: #9999FF; }
效果如下
最后,我們來看看當overflow屬性值是auto時的具體情況
HTML代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS overflow</title> <link rel="stylesheet" type="text/css" href="sample.css"> </head> <body> <div class="aut"> <p> The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. </p> </div> </body> </html>
CSS代碼
div.aut{ width: 200px; height: 100px; overflow: auto; background-color: red; }
在瀏覽器上運行的效果和scroll相似
看完了這篇文章,相信你對CSS中overflow屬性如何使用有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。