使用 CSS 使 Div 可垂直滚动

PHPz
PHPz 转载
2023-09-06 17:29:02 330浏览

使用 CSS 使 Div 可垂直滚动

我们要在网站中使用的内容可能会很大并且可能会占用大量空间,这意味着网站的其他元素可能会被取代,从而影响网站的响应能力。为了避免这种情况的发生,需要为用户提供可滚动的内容,以便如果用户感兴趣,他或她可以向下滚动以阅读整个内容。

在本文中,我们将了解如何使 div 垂直滚动以及我们将使用什么属性来实现此目的。

如何让div垂直滚动?

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

语法

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

我们将使用x轴和y轴,并将x轴的属性设置为隐藏,y轴的属性设置为自动,这将隐藏水平滚动条,只显示垂直滚动条,我们将自动获得所需的div。

示例

在此示例中,我们将声明一个 div,然后编写一个段落,我们将在其中添加溢出属性以使 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>

在上面的代码中,我们使用了溢出属性,并将其 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属性的值从隐藏的x轴和自动的y轴改为了auto,并使用相同的示例来查看我们的输出结果。让我们来看一下这段代码将生成的输出。

您可以查看上述输出,可以看到即使在overflow属性上使用了auto值,我们仍然有滚动条。

结论

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

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

以上就是使用 CSS 使 Div 可垂直滚动的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除