使用 CSS 使 div 垂直可滚动

cssweb developmentfront end technology更新于 2024/10/31 23:08:17

我们网站中使用的内容可能非常庞大,占用大量空间,这意味着网站的其他元素可能会被取代,从而影响网站的响应速度。为了避免这种情况,需要为用户提供可滚动的内容,以便用户在感兴趣时可以向下滚动阅读完整内容。

在本文中,我们将了解如何使 div 垂直可滚动,以及我们将使用哪些属性来实现这一点。

如何使 div 垂直可滚动?

overflow 属性可用于使 DIV 垂直可滚动,因为 overflow 属性有多个可输入的值。例如 hidden 和 auto。我们可以根据使用的值创建水平和垂直可滚动条。如果我们使用 auto 值,就可以获得垂直滚动条。让我们看看 overflow 属性的语法:

语法

overflow:hidden/visible/clip/scroll/auto/inherit;

我们将使用 x 轴和 y 轴,然后将 x 轴的属性设置为 hidden,y 轴的属性设置为 auto,这样水平滚动条就会隐藏,只有垂直滚动条可见,并且我们将自动获取所需的 div。

示例

在此示例中,我们将声明一个 div,然后编写一个段落,并向该段落添加 overflow 属性,使 div 可以垂直滚动。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Example of vertically scrollable div</title>
   <style>
      h2 {
         color: Green;
      }
      .scrl {
         padding: 3px;
         color: white;
         margin: 2px, 2px;
         background-color: black;
         height: 118px;
         overflow-x: hidden;
         color: white;
         overflow-y: auto;
         text-align: justify;
         width: 489px;
      }
   </style>
</head>
<body>
   <center>
      <h2>Hi! This an example of the vertically scrollable div</h2>
      <div class="scrl">This is an example of the vertically scrollable 
         div many beginner coders need the help of some articles or some sort of tutorials
         to write there code. There are many instances in which the coder might need help
         or can be stuck on a particular question. The community of coders is very huge 
         and is ready to help everyone at anywhere and at whatever time. The coding community
         will help the coder to enhance his skills and his fluency in code. The coder can 
         choose a language to write his or her code depending on his interest as every 
         language has its own expertise and functions.
      </div>
   </center>
</body>
</html>

在上面的代码中,我们使用了 overflow 属性,并将其 x 轴设置为隐藏,y 轴设置为可见,这样我们就可以在这段代码中看到一个垂直滚动条。让我们看看执行代码后的输出。

您可以查看上面的输出,可以看到我们有一个垂直滚动条,可以用来向下滚动。

注意 − 使用 overflow 属性时,必须指定元素为"块元素",否则可能无法正常工作。该属性主要用于裁剪内容或添加可滚动条(无论是垂直滚动还是水平滚动),因为所使用的内容太大,无法放入指定区域。

让我们看另一个示例来更好地理解该属性。

示例

在此示例中,我们将该属性的值设置为 auto,而不是更改属性的 x 轴和 y 轴,以使 div 垂直可滚动。以下是该代码。

<!DOCTYPE html>
<html lang="en">
<head>
   <title> Another Example of vertically scrollable div</title>
   <style>
      .scrlr {
         margin: 4px;
         padding: 4px;
         color: white;
         background-color: black;
         width: 488px;
         height: 118px;
         margin: 4px;
         text-align: justify;
         overflow: auto;
         width: 488px;
         text-align: justify;
      }
      h2 {
         color: Red;
      }
   </style>
</head>
<body>
   <center>
      <h2>Hi! This another example of the verticallly scrollable div</h2>
      <div class="scrlr">This is an example of the vertically scrollable div
         many beginner coders need the help of some articles or some sort of tutorials to
         write there code. There are many instances in which the coder might need help or
         can be stuck on a particular question. The community of coders is very huge and
         is ready to help everyone at anywhere and at whatever time. The coding community
         will help the coder to enhance his skills and his fluency in code. The coder can
         choose a language to write his or her code depending on his interest as every
         language has its own expertise and functions.
      </div>
   </center>
</body>
</html>

在上面的代码中,我们将 overflow 属性的值更改为 auto,而不是将 x 轴设置为 hidden,y 轴设置为 auto,并使用相同的示例来查看输出结果。让我们来看看这段代码生成的输出。

您可以查看上面的输出,可以看到即使在 overflow 属性上使用 auto 值后,滚动条仍然可见。

结论

overflow 属性被广泛使用,当内容占用大量空间时,它可以裁剪内容。或者,如果我们想添加一个滚动条,让用户向下滚动,从而减少它在网页上占用的整体空间。

在本文中,我们了解了如何使用 overflow 属性,如何在 div 上添加垂直滚动条,以及有关 overflow 属性中使用的值的更多信息。


相关文章


有用资源