CSS - overflow-y 属性



CSS overflow-y 属性决定当块级元素的内容超过元素顶部和底部边缘时如何显示。

可能的值

  • visible − 溢出的内容显示在元素边界之外。

  • hidden − 内容被裁剪,任何溢出内容都不可见。

  • clip − 元素的内容被裁剪,不会溢出元素的顶部和底部边缘。

  • scroll − 通过添加滚动条使元素可滚动。

  • auto − 当元素内容溢出其顶部和底部边缘时,会向元素添加滚动条。

应用于

所有 HTML 元素。

DOM 语法

object.style.overflowY = "visible|hidden|clip|scroll|auto";

CSS overflow-y - 使用 visible 和 hidden 值

以下示例演示了当overflow-y 属性设置为visible时,它允许内容在其顶部和底部边缘的内边距框处溢出;或者当设置为hidden时,它隐藏任何溢出的内容。

<html>
<head>
<style>
   .container {
      display:flex;
   }
   div.overflow-y-visible {
      background-color: #2fe262;
      border: 2px solid #000000;
      width: 170px;
      height: 160px;
      overflow-y: visible;
      margin-right: 100px;
   }
   h4 {
      text-align: center;
      color: #D90F0F;
   }
   div.overflow-y-hidden {
      background-color: #2fe262;
      border: 2px solid #000000;
      width: 170px;
      height: 160px;
      overflow-y: hidden;
   }
</style>
</head>
<body>
   <div class="container">
      <div class="overflow-y-visible">
         <h4>Tutorialspoint CSS Overflow-y Visible</h4>
         Lorem Ipsum is simply dummy text of the printing and typesetting industry. omnis dolor repellendus. non-characteristic words, Temporibus autem quibusdam et
      </div>
      <div class="overflow-y-hidden">
         <h4>Tutorialspoint CSS Overflow-y Hidden</h4>
         Lorem Ipsum is simply dummy text of the printing and typesetting industry. omnis dolor repellendus. non-characteristic words, Temporibus autem quibusdam et
      </div>
   </div>
</body>
</html>

CSS overflow-y - clip 值

overflow-y: clip 属性隐藏任何超过元素内边距框的垂直溢出内容。不会添加滚动条。

示例

这是一个示例:

<html>
<head>
<style>
   div.overflow-y-clip {
      background-color: #2fe262;
      border: 2px solid #000000;
      width: 170px;
      height: 160px;
      overflow-y: clip;
   }
   h4 {
      text-align: center;
      color: #D90F0F;
   }
</style>
</head>
<body>
   <div class="overflow-y-clip">
      <h4>Tutorialspoint CSS Overflow-y Clip</h4>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. omnis dolor repellendus. non-characteristic words, Temporibus autem quibusdam et
   </div>
</body>
</html>

CSS overflow-y - 使用 scroll 和 auto 值

overflow-y 属性可以设置为scrollautoscroll 值会在其内容溢出顶部和底部边缘时向元素添加滚动条。

auto 值仅在其内容溢出顶部和底部边缘时向元素添加滚动条。

<html>
<head>
<style>
   .container {
      display:flex;
   }
   div.overflow-y-scroll {
      background-color: #2fe262;
      border: 2px solid #000000;
      width: 170px;
      height: 160px;
      overflow-y: scroll;
      margin-right: 100px;
   }
   h4 {
      text-align: center;
      color: #D90F0F;
   }
   div.overflow-y-auto {
      background-color: #2fe262;
      border: 2px solid #000000;
      width: 170px;
      height: 160px;
      overflow-y: auto;
   }
</style>
</head>
<body>
   <div class="container">
      <div class="overflow-y-scroll">
         <h4>Tutorialspoint CSS Overflow-y Scroll</h4>
         Lorem Ipsum is simply dummy text of the printing and typesetting industry. omnis dolor repellendus. non-characteristic words, Temporibus autem quibusdam et
      </div>
      <div class="overflow-y-auto">
         <h4>Tutorialspoint CSS Overflow-y Auto</h4>
         Lorem Ipsum is simply dummy text of the printing and typesetting industry. omnis dolor repellendus. non-characteristic words, Temporibus autem quibusdam et
      </div>
   </div>
</body>
</html>
广告