[html] How can I make a div not larger than its contents?

If you have containers breaking lines, after hours looking for a good CSS solution and finding none, I now use jQuery instead:

_x000D_
_x000D_
$('button').click(function(){_x000D_
_x000D_
  $('nav ul').each(function(){_x000D_
    _x000D_
    $parent = $(this).parent();_x000D_
    _x000D_
    $parent.width( $(this).width() );_x000D_
    _x000D_
  });_x000D_
});
_x000D_
nav {_x000D_
  display: inline-block;_x000D_
  text-align: left; /* doesn't do anything, unlike some might guess */_x000D_
}_x000D_
ul {_x000D_
  display: inline;_x000D_
}_x000D_
_x000D_
/* needed style */_x000D_
ul {_x000D_
  padding: 0;_x000D_
}_x000D_
body {_x000D_
  width: 420px;_x000D_
}_x000D_
_x000D_
/* just style */_x000D_
body {_x000D_
  background: #ddd;_x000D_
  margin: 1em auto;_x000D_
}_x000D_
button {_x000D_
  display: block;_x000D_
}_x000D_
nav {_x000D_
  background: #bbb;_x000D_
  margin: 1rem auto;_x000D_
  padding: 0.5rem;_x000D_
}_x000D_
li {_x000D_
  display: inline-block;_x000D_
  width: 40px;_x000D_
  height: 20px;_x000D_
  border: solid thin #777;_x000D_
  margin: 4px;_x000D_
  background: #999;_x000D_
  text-align: center;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
_x000D_
<button>fix</button>_x000D_
_x000D_
<nav>_x000D_
  <ul>_x000D_
    <li>3</li>_x000D_
    <li>.</li>_x000D_
    <li>1</li>_x000D_
    <li>4</li>_x000D_
  </ul>_x000D_
</nav>_x000D_
_x000D_
<nav>_x000D_
  <ul>_x000D_
    <li>3</li>_x000D_
    <li>.</li>_x000D_
    <li>1</li>_x000D_
    <li>4</li>_x000D_
    <li>1</li>_x000D_
    <li>5</li>_x000D_
    <li>9</li>_x000D_
    <li>2</li>_x000D_
    <li>6</li>_x000D_
    <li>5</li>_x000D_
    <li>3</li>_x000D_
    <li>5</li>_x000D_
  </ul>_x000D_
</nav>
_x000D_
_x000D_
_x000D_

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to width

How to Determine the Screen Height and Width in Flutter Change New Google Recaptcha (v2) Width How to get the <td> in HTML tables to fit content, and let a specific <td> fill in the rest Full width image with fixed height Width equal to content CSS to make table 100% of max-width CSS Input with width: 100% goes outside parent's bound HTML email in outlook table width issue - content is wider than the specified table width How to set up fixed width for <td>? How to make div's percentage width relative to parent div and not viewport