📜  如何使用 CSS 创建可滚动的内容区域而不是页面?

📅  最后修改于: 2021-11-08 05:59:31             🧑  作者: Mango

使特定内容区域可滚动是通过使用 CSS溢出属性完成的。下面列出了溢出属性中的不同值。

  • 可见:该属性表示可以在块框外渲染,不剪裁。
  • hidden:该属性表示溢出被裁剪。其余内容将不可见。
  • auto:如果溢出被剪裁,则自动为其余内容添加滚动条。
  • scroll:此属性表示添加滚动条以查看被剪切的其余内容。
  • 初始:此属性设置为其默认值。
  • 继承:此属性从其父元素继承该属性。

我们可以通过将 body溢出属性设置为 hidden来禁用页面滚动。

在这两个示例中,我们将使用此属性来禁用页面滚动。

示例 1:在此示例中,我们使用overflow: scroll属性使“div”可垂直和水平滚动。

HTML


  

    
  
    

  

  

    

GeeksforGeeks

       

        Making content area scrollable          instead of the page     

       
        It is a good platform to learn programming.          It is an educational website. Prepare for          the Recruitment drive of product based          companies like Microsoft, Amazon, Adobe etc          with a free online placement preparation          course. The course focuses on various MCQ         & Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are
          Articles that need little modification or         improvement from reviewers are published          first. To quickly get your articles reviewed,         please refer existing articles, their          formatting style, coding style, and try to          make you are close to them. In case you are          a beginner, you may refer Guidelines to write         an Article. It is a good platform to learn          programming. It is an educational website.          Prepare for the Recruitment drive of product          based companies like Microsoft, Amazon, Adobe          etc with
a free online placement preparation          course. The course focuses on various MCQ's &          Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by
writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are Articles          that need little modification or improvement          from reviewers are published first. To quickly          get your articles reviewed, please refer          existing articles, their formatting style,          coding style, and try to make you are close          to them. In case you are a beginner, you may          refer Guidelines to write an Article.     
  


HTML


  

    
  
    
  
    

  

    

GeeksforGeeks

       

        Making content area scrollable          instead of the page     

       
        It is a good platform to learn programming.          It is an educational website. Prepare for          the Recruitment drive of product based          companies like Microsoft, Amazon, Adobe etc          with a free online placement preparation          course. The course focuses on various MCQ         & Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are
          Articles that need little modification or         improvement from reviewers are published          first. To quickly get your articles reviewed,         please refer existing articles, their          formatting style, coding style, and try to          make you are close to them. In case you are          a beginner, you may refer Guidelines to write         an Article. It is a good platform to learn          programming. It is an educational website.          Prepare for the Recruitment drive of product          based companies like Microsoft, Amazon, Adobe          etc with
a free online placement preparation          course. The course focuses on various MCQ's &          Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by
writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are Articles          that need little modification or improvement          from reviewers are published first. To quickly          get your articles reviewed, please refer          existing articles, their formatting style,          coding style, and try to make you are close          to them. In case you are a beginner, you may          refer Guidelines to write an Article.     
  


HTML


  

    
  
    
  
    

  

    

GeeksforGeeks

       

        Making content area scrollable          instead of the page     

       
        It is a good platform to learn programming.          It is an educational website. Prepare for          the Recruitment drive of product based          companies like Microsoft, Amazon, Adobe etc          with a free online placement preparation          course. The course focuses on various MCQ         & Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are
          Articles that need little modification or         improvement from reviewers are published          first. To quickly get your articles reviewed,         please refer existing articles, their          formatting style, coding style, and try to          make you are close to them. In case you are          a beginner, you may refer Guidelines to write         an Article. It is a good platform to learn          programming. It is an educational website.          Prepare for the Recruitment drive of product          based companies like Microsoft, Amazon, Adobe          etc with
a free online placement preparation          course. The course focuses on various MCQ's &          Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by
writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are Articles          that need little modification or improvement          from reviewers are published first. To quickly          get your articles reviewed, please refer          existing articles, their formatting style,          coding style, and try to make you are close          to them. In case you are a beginner, you may          refer Guidelines to write an Article.     
  


输出:

示例 2:在本示例中,使用overflow:auto;使“div”垂直和水平滚动。

HTML



  

    
  
    
  
    

  

    

GeeksforGeeks

       

        Making content area scrollable          instead of the page     

       
        It is a good platform to learn programming.          It is an educational website. Prepare for          the Recruitment drive of product based          companies like Microsoft, Amazon, Adobe etc          with a free online placement preparation          course. The course focuses on various MCQ         & Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are
          Articles that need little modification or         improvement from reviewers are published          first. To quickly get your articles reviewed,         please refer existing articles, their          formatting style, coding style, and try to          make you are close to them. In case you are          a beginner, you may refer Guidelines to write         an Article. It is a good platform to learn          programming. It is an educational website.          Prepare for the Recruitment drive of product          based companies like Microsoft, Amazon, Adobe          etc with
a free online placement preparation          course. The course focuses on various MCQ's &          Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by
writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are Articles          that need little modification or improvement          from reviewers are published first. To quickly          get your articles reviewed, please refer          existing articles, their formatting style,          coding style, and try to make you are close          to them. In case you are a beginner, you may          refer Guidelines to write an Article.     
  

输出:

注意:您可以通过将overflow-y设置为scroll并将autooverflow-x 设置hidden来仅启用垂直滚动。

同样对于水平滚动,将overflow-x设置为scrollauto并将overflow-y 设置hidden

示例 3:此示例仅用于内容区域的垂直滚动。

HTML



  

    
  
    
  
    

  

    

GeeksforGeeks

       

        Making content area scrollable          instead of the page     

       
        It is a good platform to learn programming.          It is an educational website. Prepare for          the Recruitment drive of product based          companies like Microsoft, Amazon, Adobe etc          with a free online placement preparation          course. The course focuses on various MCQ         & Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are
          Articles that need little modification or         improvement from reviewers are published          first. To quickly get your articles reviewed,         please refer existing articles, their          formatting style, coding style, and try to          make you are close to them. In case you are          a beginner, you may refer Guidelines to write         an Article. It is a good platform to learn          programming. It is an educational website.          Prepare for the Recruitment drive of product          based companies like Microsoft, Amazon, Adobe          etc with
a free online placement preparation          course. The course focuses on various MCQ's &          Coding question likely to be asked in the          interviews & make your upcoming placement          season efficient and successful. Also, any          geeks can help other geeks by
writing          articles on the GeeksforGeeks, publishing          articles follow few steps that are Articles          that need little modification or improvement          from reviewers are published first. To quickly          get your articles reviewed, please refer          existing articles, their formatting style,          coding style, and try to make you are close          to them. In case you are a beginner, you may          refer Guidelines to write an Article.     
  

输出: