Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
Building Tooltip using CSS
Next article icon

How to make div not larger than its contents using CSS?

Last Updated : 16 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
There are three ways to achieve this problem:
  1. By default case
  2. Using inline-block property
  3. 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:

Next Article
Building Tooltip using CSS

S

SandySharma
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 Select
    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. [GFGTABS] HTML <html> <head> <style> .parent * { color: blue; font-weigh
    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
  • What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
    The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line. Difference between the "word-break: break-all;" and "word-wrap: break-word;" word-break:
    2 min read
  • How to make div height expand with its content using CSS ?
    When designing a webpage, ensuring that a div adjusts its height according to its content is important for creating a flexible and responsive layout. By using CSS, you can easily make a div adapt to varying content sizes, which is particularly useful when dealing with dynamic or unpredictable conten
    3 min read
  • How to float three div side by side using CSS?
    Floating three div elements side by side using CSS means aligning them horizontally in a row. This is done by applying float: left; to each div, which moves them to the left of the parent container, allowing them to sit next to each other. These are the following ways to solve this problem: Table of
    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
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences