There are three ways to achieve this problem:
Default Case:
In HTML div is by default fit to content inside it. The example goes like this:
Example 1:
<!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: orange; overflow: hidden; color: white; } .GeeksForGeeks { background: dodgerblue; position: absolute; top: 50%; left: 1%; right: 1%; } </style> </head> <body> <!-- HTML Code --> <center><h1 style = "color:forestgreen ; top:35% ; left: 35% ; position:absolute; ">Geeks For Geeks</h1></center> <center> <div class = "GeeksForGeeks"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </center> </body> </html> |
Output:
The output of two different screen width is shown which proves that it fits with content inside it.
Screen 1:

Screen 2:
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" dir = "ltr"> <head> <meta charset = "utf-8"> <title>Geeks for Geeks Example</title> <!--CSS Code--> <style media = "screen"> body { background: violet; overflow: auto; color: white; } .GeeksForGeeks { background: dodgerblue; position: absolute; display: inline-block; left: 1%; right: 1%; top: 50%; } </style> </head> <body> <!-- HTML Code --> <center><h1 style="color: forestgreen; top: 35%; left: 35%; position: absolute;">Geeks For Geeks</h1></center> <center> <div class="GeeksForGeeks"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </center> </body> </html> |
Output:
The output of two different screen width is shown which proves that it fits with content inside it.
Screen 1:

Screen 2:
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 --> <center><h1 style = "color: lime; top: 35%; left: 35%; position: absolute;">Geeks For Geeks</h1></center> <center> <div class = "GeeksForGeeks"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> </center> </body> </html> |
Output:
The output of two different screen width is shown which proves that it fits with content inside it.
Screen 1:

Screen 2:
Recommended Posts:
- How to overlay one div over another div using CSS
- How to make div height expand with its content using CSS ?
- How to make div width expand with its content using CSS ?
- How to copy the content of a div into another div using jQuery ?
- How to position a div at the bottom of its container using CSS?
- How to center a div within another div?
- How to clear all div's content inside a parent div ?
- How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS?
- How to make a div fill a remaining horizontal space using CSS?
- Make a div horizontally scrollable using CSS
- How to make a HTML div responsive using CSS ?
- How to make a div span two rows in a grid using CSS ?
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- How to show contents of selected row of a table in Bootstrap model using jQuery ?
- Making a div vertically scrollable using CSS
- How to align content of a div to the bottom using CSS ?
- How to horizontally center a div using CSS?
- How to auto-resize an image to fit a div container using CSS?
- How to give a div tag 100% height of the browser window using CSS
- How to place two div side-by-side of the same height using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : aditya_taparia

