Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to make a div not larger than its contents?

I have a layout similar to:

<div>
<table>
</table>
</div>

I would like for the div to only expand to as wide as my table becomes.
by

2 Answers

Bharatgxwzm
display: inline-block joins an additional edge to your element.

I recommend this:
#element {
display: table; / IE8+ and all other modern browsers /
}


You can additionally now quickly focus that fancy new #element just by adding margin: 0 auto.
MounikaDasa
We can make it using fit-content property in width and height:
In this following method we set the width and height property to fit-content value.

<!DOCTYPE html>
<html lang = "en" dir = "ltr">
<head>
<meta charset = "utf-8">
<title>Studytonight Example</title>
<!--CSS Code-->
<style media = "screen">
Studytonight{

width:fit-content;
height:fit-content;
left: 0;
top: 50%;
}
</style>
</head>

<body>
<!-- HTML Code -->
<center><h1>Studytonight</h1></center>
<center>
<div class = "Studytoniht">
css language
</div>
</center>
</body>
</html>

Login / Signup to Answer the Question.