Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to set div width to fit content using CSS ?

  • Difficulty Level : Basic
  • Last Updated : 30 Jul, 2021

There are three ways to solve this problem which are listed below:

  1. By default case
  2. Using inline-block property
  3. Using fit-content property in width and height

Default Case: HTML div is by default fit to content inside it. The example goes like this:
Example 1:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.




<!DOCTYPE html>
<html lang = "en" dir = "ltr">
      
<head>
    <meta charset = "utf-8">
    <title>GeeksforGeeks Example</title>
      
    <!--CSS Code-->
    <style media = "screen">
        body {
            background: orange;
            overflow: hidden;
            color: white;
        }
        .GeeksForGeeks {
            text-align: center;
            background: dodgerblue;
            position: absolute;
            top: 50%;
            left: 1%;
            right: 1%;
        }
    </style>
</head>
  
<body>
      
    <!-- HTML Code -->
    <h1 style = "color:forestgreen; top:35%;
            left: 35%; position:absolute; ">
        Geeks For Geeks
    </h1>
  
    <div class = "GeeksForGeeks">
        Cascading Style Sheets, fondly referred
        to as CSS, is a simply designed language
        intended to simplify the process of
        making web pages presentable. CSS allows
        you to apply styles to web pages. More
        importantly, CSS enables you to do this
        independent of the HTML that makes up
        each web page.
    </div>
</body>
  
</html>

Output:

Using inline-block property: Use display: inline-block property to set a div size according to its content.



Example 2:




<!DOCTYPE html>
<html lang = "en">
  
<head>
    <meta charset = "utf-8">
      
    <title>GeeksforGeeks Example</title>
      
    <!--CSS Code-->
    <style media = "screen">
        body {
            background: violet;
            overflow: auto;
            color: white;
        }
        .GeeksForGeeks {
            text-align: center;
            background: dodgerblue;
            position: absolute;
            display: inline-block;
            left: 1%;
            right: 1%;
            top: 50%;
        }
    </style>
</head>
  
<body>
    <!-- HTML Code -->
    <h1 style="color: forestgreen; 
        top: 35%; left: 35%; position: absolute;">
        Geeks For Geeks
    </h1>
  
    <div class="GeeksForGeeks">
        Cascading Style Sheets, fondly referred
        to as CSS, is a simply designed language
        intended to simplify the process of
        making web pages presentable. CSS allows
        you to apply styles to web pages. More
        importantly, CSS enables you to do this
        independent of the HTML that makes up
        each web page.
    </div>
</body>
  
</html>

Output:

Using fit-content property in width and height: In this method, we set the width and height property to fit-content value.
Example 3:




<!DOCTYPE html>
<html lang = "en" dir = "ltr">
      
<head>
    <meta charset = "utf-8">
    <title>Geeks for Geeks Example</title>
      
    <!--CSS Code-->
    <style media = "screen">
        body {
            background: tomato;
            overflow: hidden;
            color: white;
        }
        .GeeksForGeeks {
            background: crimson;
            position: absolute;
            width:fit-content;
            height:fit-content;
            left: 0;
            top: 50%;
        }
    </style>
</head>
  
<body>
    <!-- HTML Code -->
    <h1 style = "color: lime; top: 35%;
        left: 35%; position: absolute;">
        Geeks For Geeks
    </h1>
          
    <div class = "GeeksForGeeks">
        Cascading Style Sheets, fondly referred
        to as CSS, is a simply designed language
        intended to simplify the process of 
        making web pages presentable. CSS allows
        you to apply styles to web pages. More
        importantly, CSS enables you to do this
        independent of the HTML that makes up
        each web page.
    </div>
</body>
  
</html>

Output:

HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.




My Personal Notes arrow_drop_up
Recommended Articles
Page :