CSS Make Div ScrollableWe will understand the CSS Make div scrollable in this article. DivThe "div" is the HTML element that defines the division in the HTML document. Syntax:The <div> and </div> are the opening tag and closing tag in the above-given syntax. The content is written inside the opening tag and closing tag. We can make the <div> element scrollable by setting the height and width of the div element and utilizing the CSS overflow property. CSS OverflowIt is the CSS property that is utilized to prevent the content from overflowing from the container. Syntax:We can set the value of the CSS overflow property to "auto" or "scroll". Although the CSS overflow property can take various values to make the <div> element scrollable, we can only give values "auto" or "scroll". Following are the descriptions of the overflow values:
Vertical Scroll BarWe can create the vertical scroll bar by utilizing the CSS overflow-x and CSS overflow-y properties. Syntax:The overflow-x property is given the value of "hidden," which hides the scroll bar of the x-axis, and the overflow-y property is given the value of "auto," which creates a scroll bar on the vertical side. Horizontal Scroll BarWe can create the horizontal scroll bar by utilizing the CSS overflow-x and CSS overflow-y properties. Syntax:The overflow-y property is given the value of "hidden," which hides the scroll bar of the x-axis, and the overflow-y property is given the value of "auto," which creates a scroll bar on the horizontal side. Demonstrations of Div ScrollableLet us understand how to make div scrollable with the help of the demonstrations. Demo 1:We will construct a div element with a class of "container" in the following demonstration. We will make the div scrollable by giving the value of "scroll" to the CSS overflow property. Code: Output: Here is the outcome below, where we can witness that the scrollbar emerges on the <div> element with a class of "container". Demo 2:In the following demonstration, we will construct a div element and make it scrollable by setting the CSS overflow property to auto. The content that is written in the class "div1" fits into it, so the scroll bar does not appear utilizing the "auto" value to overflow property. The content inside the class "div2" does not fit inside it, so the scroll bar emerges with the help of the "auto" value to overflow property. Code: Output: Here is the outcome below, where we can witness that the content in class div1 does not overflow, which is why the scrollbar is not observable. The content written inside the class div2 overflows, so the scrollbar emerges on the element of class div2. Demo 3:We will put a vertical scroll bar on the <div> HTML element with the help of the CSS overflow-x property and CSS overflow-y property. We will put the "hidden" value to the CSS overflow-x property and the "auto" value to the CSS overflow-y property. Code: Output: Here, in the outcome, we can witness that when the content in the <div> HTML element overflows, then the vertical scrollbar is visible on the <div> HTML element. Demo 4:We will put a horizontal scroll bar on the <div> HTML element with the help of the CSS overflow-x property and CSS overflow-y property. We will assign the value "auto" to the CSS overflow-x property and the value "hidden" to the CSS overflow-y property. Code: Output: Here, in the outcome below, we can witness that the content under the <div> element does not fit. The scrollbar is visible on the <div> element on the horizontal side. Browser SupportThe browsers that support the overflow, overflow-x, and overflow-y CSS properties are as follows:
ConclusionWe have understood how to create a CSS div scrollable in this article. Some points to remember are given downwards:
|