最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談css溢出機(jī)制探究

  發(fā)布時間:2018-09-18 15:16:23   作者:csRyan   我要評論
在實(shí)際開發(fā)的過程中,內(nèi)容溢出是經(jīng)常見到的。這篇文章主要介紹了淺談css溢出機(jī)制探究,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

為什么需要深入學(xué)習(xí)CSS溢出機(jī)制?

在實(shí)際開發(fā)的過程中,內(nèi)容溢出是經(jīng)常見到的。如果不深入了解這個機(jī)制,你經(jīng)常會碰到這樣的問題:為什么這個元素沒有受到祖先元素的overflow:hidden的影響?這里出現(xiàn)的滾動條是哪個元素的?如果消除這個滾動條?如何在指定的元素上增加滾動功能?

在這篇文章,我們將會從CSS標(biāo)準(zhǔn)出發(fā),討論CSS溢出機(jī)制的細(xì)節(jié)。

溢出

當(dāng)一個盒子(block container box)的內(nèi)容(子元素、孫子元素等后裔)超過盒子本身的大小的時候,就會出現(xiàn)溢出。這個時候CSS屬性overflow決定如何處理溢出。這個css屬性大家都知道,在這里不討論了,在這里指出需要注意的幾點(diǎn):

overflow會影響所在元素的所有內(nèi)容的裁剪、滾動,但是有一種情況例外:"It affects the clipping of all of the element's content except any descendant elements (and their respective content and descendants) whose containing block is the viewport or an ancestor of the element." 也就是說,overflow的所在元素必須是內(nèi)容元素的直接或間接containing block,這時overflow屬性才會影響這個內(nèi)容元素。比如<A><B><C><C/><B/><A/>,一般來說,B的overflow會影響C,但是如果C是相對于viewport或者A定位的(比如使用了position:absolute),那么C的顯示就不受B的裁剪、滾動的影響。

當(dāng)需要滾動條的時候,滾動條會放在border與padding之間。父元素產(chǎn)生滾動條以后,它產(chǎn)生的containing block的尺寸會減少,以便給滾動條騰出空間。

在<html>和<body>上的overflow屬性存在冒泡現(xiàn)象: "UAs must apply the 'overflow' property set on the root element to the viewport. When the root element is an HTML "HTML" element or an XHTML "html" element, and that element has an HTML "BODY" element or an XHTML "body" element as a child, user agents must instead apply the 'overflow' property from the first such child element to the viewport, if the value on the root element is 'visible'. The 'visible' value when used for the viewport must be interpreted as 'auto'. The element from which the value is propagated must have a used value for 'overflow' of 'visible'. "

可以推斷出:

一般來說只有元素才能擁有滾動條(更準(zhǔn)確地說,只有產(chǎn)生block container box的元素才能擁有滾動條)。但visual viewport是個例外。它雖然不是一個元素,但是也可以擁有滾動條。如果在<html>和<body>上都沒有設(shè)置overflow屬性而使用默認(rèn)值visible(大部分場景都是這樣),那么,visual viewport的overflow就是auto:當(dāng)網(wǎng)頁中有內(nèi)容超出visual viewport時,visual viewport上會出現(xiàn)滾動條。

<html>的最終overflow永遠(yuǎn)都是visible。也就是說,<html>元素永遠(yuǎn)不可能擁有滾動條。

如果你想要為<body>設(shè)置非visible的overflow,需要先為<html>設(shè)置一個非visible的值來冒泡,從而<body>的overflow不會被冒泡。小練習(xí)

小練習(xí):

利用以上原理,使visual viewport和<body>都擁有橫、豎滾動條,總共4個滾動條。不能使用overflow: scroll(這樣就太簡單了)。

步驟:

  • 使visual viewport和<body>的最終overflow值都為auto,從而可以出現(xiàn)滾動條。
  • 觸發(fā)visual viewport和<body>的溢出。通過【為內(nèi)容設(shè)置一個更大的尺寸】來做到。

代碼+注釋:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <title>test</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    html {
      /* 使html的尺寸始終與visual viewport相同(即使你縮放、調(diào)整瀏覽器窗口的大?。瑥亩鴅ody可以設(shè)置一個比visual viewport還大的尺寸(110%)。
      對于默認(rèn)為block的元素可以省略width: 100%; */
      width: 100%;
      height: 100%;
      /* 非visible的值冒泡到visual viewport上,使visual viewport可以出現(xiàn)滾動條 */
      overflow: auto;
      border: 15px solid red;
    }

    body {
      /* 使得body可以出現(xiàn)滾動條 */
      overflow: auto;
      /* body溢出html,從而溢出initial containning block,從而溢出visual viewport,使得visual viewport出現(xiàn)滾動條。
      當(dāng)然,你也可以通過很多其他的方式來觸發(fā)visual viewport的溢出,比如增大html元素,或者在body中弄一個position: absolute的div */
      width: 110%;
      height: 110%;
      border: 15px solid green;
    }

    main {
      /* main溢出body,使得body出現(xiàn)滾動條 */
      width: 110%;
      height: 110%;
      border: 15px solid blue;
    }
  </style>
</head>

<body>
  <main>
  </main>
</body>

</html>

結(jié)果:

自己在chrome中打開以上代碼,能更加清晰地看出是怎么做到的。

也可以通過absolute的方式來溢出initial containing block:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <title>test</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    html {
      /* 使html的尺寸始終與visual viewport相同(即使你縮放、調(diào)整瀏覽器窗口的大小),從而body可以設(shè)置一個比visual viewport還大的尺寸(110%)。
      對于默認(rèn)為block的元素可以省略width: 100%; */
      width: 100%;
      height: 100%;
      /* 非visible的值冒泡到visual viewport上,使visual viewport可以出現(xiàn)滾動條 */
      overflow: auto;
      border: 15px solid red;
    }

    body {
      /* 使得body可以出現(xiàn)滾動條 */
      overflow: auto;
      /* 為body設(shè)置一個尺寸,從而main可以設(shè)置一個比body還大的尺寸(110%)。
      對于默認(rèn)為block的元素可以省略width: 100%; */
      height: 100%;
      border: 15px solid green;
    }

    main {
      /* main溢出body,使得body出現(xiàn)滾動條 */
      width: 110%;
      height: 110%;
      border: 15px solid blue;
    }

    .abs {
      /* 通過absolute的方式來溢出initial containing block,從而溢出viewport */
      position: absolute;
      width: 100px;
      height: 100px;
      right: -100px;
      bottom: -100px;
      border: 15px solid blueviolet;
    }
  </style>
</head>

<body>
  <main>
  </main>

  <div class="abs"></div>
</body>

</html>

結(jié)果:

自己在chrome中打開以上代碼,能更加清晰地看出是怎么做到的。

如何看出某個滾動條是屬于哪個元素的?

通過Chrome DevTools就可以看出滾動條的所屬元素。

前面已經(jīng)說過,滾動條的位置在元素的border與padding之間。當(dāng)你使用Chrome DevTools選中某個元素,發(fā)現(xiàn)滾動條恰好在高亮區(qū)域(border)內(nèi)部時,滾動條就屬于當(dāng)前元素。

要判斷滾動條是否屬于visual viewport,首先先將右邊、下邊的滾動條分別滾動到最下、最右(這一步很重要,它保證沒有內(nèi)容藏在滾動條下面)。然后,Ctrl+Shift+C選擇右邊或下邊的滾動條,如果高亮的區(qū)域不包含這個滾動條,就說明這個滾動條不屬于任何元素,也就是屬于visual viewport。

參考資料

css2.1標(biāo)準(zhǔn)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

雷波县| 重庆市| 秦安县| 阳江市| 巴楚县| 彰武县| 阿勒泰市| 霍城县| 桂平市| 永顺县| 宝坻区| 桃江县| 额尔古纳市| 普陀区| 千阳县| 封丘县| 新竹县| 乾安县| 彭州市| 鄂伦春自治旗| 滨州市| 中方县| 当雄县| 沁源县| 无为县| 临桂县| 彰化市| 历史| 区。| 油尖旺区| 深州市| 滨海县| 安徽省| 吉水县| 大城县| 平顺县| 昆明市| 于都县| 宁德市| 西乌珠穆沁旗| 扬州市|