Open In App

How to fix CSS underflow & overflow ?

In this article, we’ll see how to manage the overflow issue in CSS. This type of issue generally arises when the content exceeds the container size or when the dimension(i.e. width and height) of the container is fixed. Here, the content can be text or an image and the container can be a <div> tag or another HTML tag that contains the text or image. For instance, the below image illustrates the fixed length of the container size for which the content is overflowing from the container.

 Overflow of the text

The problem of an overflow of the content when the size of the container is fixed can be accomplished with the help of the below 2 methods:



We will explore both these approaches in detail & understand them through the illustrations.

By making container size modifiable: This task can be accomplished with the help of the CSS min-height property which sets the minimum height of the container. The minimum height will only be applicable if the content is within or small in size in comparison to the minimum height. Otherwise, the min-height property has no effect, if the content exceeds the minimum height.



Example 1: This example describes controlling the content overflow with the help of the CSS min-height property.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Fixing the text overflow in CSS
    </title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
         
        .container {
            width: 250px;
            min-height: 200px;
            border: 4px solid rgb(7, 240, 96);
            color: rgb(31, 231, 31);
        }
         
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        Fixing the text overflow in CSS
    </h3><br />
    <div class="container">
         
<p>
            In this article, we'll see how to manage the
            overflow issue in CSS. This type of problem
            arrives when the content of the container
            exceeds the container size, the container
            contents will overflow. Here the content can
            be text or image and the container can be a
            div tag or some other HTML tag. This overflow
            problem arrives only when the dimension of the
            container is fixed. min-height properties set
            the minimum height of the container and whenever
            the content of the container required more space
            then the height of the container also increases
            with the size of the content.
         </p>
 
 
    </div>
</body>
</html>

Output:

 

By implementing CSS overflow-related properties: The overflowing of the content can be controlled & managed by implementing the required overflow property in CSS, which tells whether to clip the content or, to add scroll bars, or simply hide the extra content that is overflowed. To do this, CSS provides some properties, which are described below:

overflow: This CSS property handles the overflow problem in both vertical as well as horizontal without change in the dimension of the container. The overflow property has 4 values:

changes after applying overflow: hidden

overflow: scroll

overflow: auto

overflow: visible

overflow-y: It is used to handle overflows of the content vertically. It also has 4 properties similar to overflow:

overflow-y: visible

overflow-y: hidden

overflow-y: scroll

overflow-y: auto

overflow-x: It is used to handle overflows of the content horizontally. Mostly it is used when we use the image in a container of fixed width and the width of the image exceeds the width of the container. Because the text is always laid out in the vertical direction when it excessed the width of the container. It also has 4 properties similar to overflow:

overflow-x: hidden

overflow-x: scroll

overflow-x: auto

overflow-x: visible

For content underflow, there is no such definition is available in CSS that we can implement. Generally, the problem of this kind happens because of small mistakes.

Example 2: This example describes managing the overflowed content using the overflow property in CSS.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        Fixing the text overflow using overflow Property
    </title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
         
        .container {
            width: 350px;
            height: 200px;
            border: 4px solid red;
            overflow: auto;
            /* hidden , auto , scroll , visible */
        }
         
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        Fixing the text overflow  using overflow Property
    </h3>
    <div class="container">
         
<p>
            In this article, we'll see how to manage the
            overflow issue in CSS. This type of problem
            arrives when the content of the container
            exceeds the container size, the container
            contents will overflow. Here the content can
            be text or image and the container can be a
            div tag or some other HTML tag. This overflow
            problem arrives only when the dimension of the
            container is fixed. min-height properties set
            the minimum height of the container and whenever
            the content of the container required more space then
            the height of the container also increases with the
            size of the content.
        </p>
 
    </div>
</body>
</html>

Output:

 


Article Tags :