How to make div not larger than its contents using CSS? Last Updated : 16 Apr, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report There are three ways to achieve this problem: By default case Using inline-block property Using fit-content property in width and height Default Case: In HTML div is by default fit to content inside it. The example goes like this: Example 1: html <!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: html <!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: html <!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: Comment More infoAdvertise with us S SandySharma Follow Improve Article Tags : CSS Technical Scripter 2018 Similar Reads Set the size of background image using CSS ? Setting the size of a background image using CSS allows you to control how the image fits within an element. By using the background-size property, you can specify the width and height, ensuring the image scales appropriately for responsive design. Syntax: background-size: width height;Note: If the 2 min read What is Greater-than Sign (>) Selector in CSS? In CSS, the greater than sign (>) is known as the child combinator selector. It is used to style elements that have a specific parent. Unlike other selectors, it only targets direct children of an element, meaning it only looks one level down in the HTML structure.How the Child Combinator Selecto 2 min read How to Select all Child Elements Recursively using CSS? Here are three methods to achieve to Select all Child Elements Recursively using CSS:1. Using the Universal Selector (*)The universal selector applies styles to all child elements within a parent recursively.HTML<html> <head> <style> .parent * { color: blue; font-weight: bold; } 3 min read How to style a checkbox using CSS ? Styling a checkbox using CSS involves customizing its appearance beyond the default browser styles. This can include changing the size, color, background, and border, and adding custom icons or animations. Techniques often involve hiding the default checkbox and styling a label or pseudo-elements fo 3 min read CSS word-break vs word-wrap (Overflow Wrap) : What's the Difference ? In CSS, controlling how long words break at line endings is crucial for clean layouts and good readability. Two properties often confused are word-break and overflow-wrap also known as word-wrap. This guide compares how they handle text overflow and when to use each.word-break Vs. word-wrapPropertyw 3 min read How to make div height expand with its content using CSS? When designing a webpage, it's important to make sure a div adjusts its height based on the content inside it. This is key to creating a flexible and responsive layout, especially when the content changes or is unpredictable. Using CSS, you can easily make a div adapt to different content sizes. One 3 min read How to float three div side by side using CSS? Aligning three <div> elements side by side is a fundamental layout technique in web design. Whether you're creating a profile card section, a features row, or a three-column layout for a homepage, placing elements in a horizontal line is a common requirement.While the traditional approach reli 3 min read How to change color of PNG image using CSS? Given an image and the task is to change the image color using CSS. Use filter function to change the png image color. Filter property is mainly used to set the visual effect to the image. There are many property value exist to the filter function. filter: none|blur()|brightness()|contrast()|drop-sh 1 min read What is the best way to include CSS file? Why use @import? CSS property can be include in the HTML page in a number of different ways. HTML documents are formatted according to the information in the style sheet which is to be included.There are many ways to include CSS file which are listed below: External style sheet (Using HTML <link> Tag): Externa 4 min read How to style icon color, size, and shadow by using CSS ? Styling an icon's color, size, and shadow in CSS involves setting the color property for the icon's color, adjusting its size with font-size or width/height, and applying box-shadow or text-shadow for visual depth effects.When using Font Awesome, adding icons is simple and requires no downloads or i 2 min read Like