溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CSS中overflow屬性如何使用

發(fā)布時間:2020-09-26 10:37:31 來源:億速云 閱讀:164 作者:小新 欄目:web開發(fā)

小編給大家分享一下CSS中overflow屬性如何使用,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

 CSS中overflow屬性是經(jīng)常可以用到的屬性,接下來的這篇文章我們就來看看CSS中overflow屬性的具體用法。

CSS中overflow屬性如何使用

我們首先來看一下overflow屬性的值有哪些?

overflow 屬性規(guī)定當(dāng)內(nèi)容溢出元素框時發(fā)生的事情。

overflow有以下四個屬性值

visible:初始值,內(nèi)容不會被修剪,會呈現(xiàn)在元素框之外。

scroll:內(nèi)容會被修剪,但是瀏覽器會顯示滾動條以便查看其余的內(nèi)容。

hidden:內(nèi)容會被修剪,并且其余內(nèi)容是不可見的。

auto:如果內(nèi)容被修剪,則瀏覽器會顯示滾動條以便查看其余的內(nèi)容。

下面我們來詳細(xì)說一說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;
}

在瀏覽器上顯示如下結(jié)果

當(dāng)overflow的屬性值是hidden時,效果如下

CSS中overflow屬性如何使用

在hidden的情況下,不會顯示出剩下的部分,也不能滾動顯示剩下的內(nèi)容。

當(dāng)overflow的屬性值是scroll時,效果如下

CSS中overflow屬性如何使用

在scroll的情況下,沒有顯示的內(nèi)容可以滾動。默認(rèn)的情況下,文字在橫向上會折回,垂直方向顯示滾動欄。

在css中設(shè)置white-space : nowrap(不自動改行的意思),也可以向橫向滾動。

CSS代碼

div.scr{
  width: 200px;
  height: 100px;
  white-space:nowrap;
  overflow: scroll;
  background-color: #99FF99;
}

效果如下

CSS中overflow屬性如何使用

此外,也可以使用overflow-x和overflow-y屬性對垂直和橫向的滾動條的顯示進(jìn)行更為細(xì)致的設(shè)置。

當(dāng)overflow的屬性值是visible時,效果如下

CSS中overflow屬性如何使用

visible的情況下,從盒子中溢出顯示。紫色的部分是div盒子。默認(rèn)的情況下,文字在div的橫向width中被折回,并在縱向方向上顯示。

這個也和scroll屬性值一樣,在css中設(shè)置white-space : nowrap,也可以橫向滾動。

另外,如果不設(shè)置盒子的高度,就會自動改變盒子的高度。

div.vis{
  width: 200px;
  /* height: 100px;  */
  overflow: visible;
  background-color: #9999FF;
}

效果如下

CSS中overflow屬性如何使用

最后,我們來看看當(dāng)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;
}

在瀏覽器上運(yùn)行的效果和scroll相似

CSS中overflow屬性如何使用

看完了這篇文章,相信你對CSS中overflow屬性如何使用有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI