How to make div not larger than its contents using CSS?
Given a div and the task is to set div not larger then its content. If the size of content change then the size of div also change.
Method 1: Use display: inline-block property to set a div size according to its content.
Example:
<!DOCTYPE html> <html> <head> <style> .geeks { background-color: green; color:white; display: inline-block; } </style> </head> <body> <div class = "geeks"> Welcome to GeeksforGeeks! </div> </body> </html> |
chevron_right
filter_none
Output:

Method 2: This method set the width and height property to fit-content value.
<!DOCTYPE html> <html> <head> <style> .geeks { background-color: green; color:white; width:fit-content; height:fit-content; } </style> </head> <body> <div class = "geeks"> Welcome to GeeksforGeeks! </div> </body> </html> |
chevron_right
filter_none
Output:

Recommended Posts:
- Java program to merge contents of all the files in a directory
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- Copy the entire contents of a directory to another directory in PHP
- How to make a redirect in PHP?
- Do you want to make your own games?
- Which language should you learn first if you want to make an app?
- How to make a placeholder for a 'select' box?
- How to make dynamic chart in PHP using canvasJS?
- How to make text input non-editable using CSS?
- How to make a vertical line using HTML ?
- How to make elements float to center?
- C/C++ program to make a simple calculator
- How to make div height expand with its content using CSS ?
- Best way to make a d3.js visualization layout responsive
- How to make sure that Apache service keeps running in Ubuntu
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.



