Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS div as wide as its contents

I'm still very much a CSS novice. For many years I've managed my layout using nested tables, which I know all CSS people would tell me is evil. I've piddled around with CSS for many years, but ever since the beginning I've been unable to make it control my layout the way I want it to.

In short, what I want is to have a <div> that will only be as wide as its contents. I do not want to set its size. I want it to be only as wide as its contents, which is created dynamically.

A table does this perfectly. With a <div> it always seems to do something other than what I want. No "overflow" option does what I want.

The classic example is when I want to create a full-page columnar layout with the left column containing navigation links and the right side containing the content. I want the left column to be precisely as wide as the navigation links. No more, no less. (The navigation links are not static; they're created dynamically and could change in size/length at any time). And the right ("content") column should be whatever's left over. Most importantly, when I make the browser screen wider or narrower, I want left column to STILL be exactly as wide as the nav-links: no more, no less. I want the contents to automatically wrap as needed, if the browser's width gets too narrow. Under no circumstances do I want the text to be obscured or to go outside of the defined column.

In short, I want a <div> that works as well as a table without having to use a table. Surely, CSS experts. this can be done....right? I've asked this question in other forums, and have never yet received an acceptable answer.

Or take a somewhat-related simple example like this (perhaps this one is easy):

<ul style="background: orange;">
   <li>one</li>
   <li>two</li>
   <li>three</li>
</ul>

The entire thing is as wide as the browser screen. But I want it to only be as wide as the longest element. How do I do this with CSS? I can put the entire thing inside a table like this

<table border=0 cellpadding=0 cellspacing=0><tr><td>
<ul style="background: orange;">
   <li>one</li>
   <li>two</li>
   <li>three</li>
</ul>
</td></tr></table>

...and it does what I want. How do I do this with CSS instead of using those "evil" tables?


I tried miku's example.
It doesn't work (for what I want it to do).

If I made the extra long navigation line a long string like

 an&nbps;extra&nbps;long&nbps;navigation&nbps;link&nbps;goes&nbps;here&nbps;-&nbps;this&nbps;could&nbps;even&nbps;be&nbps;an&nbps;image!

I want the left navigation side to expand as needed. I don't want a maximum width. Using the example above, the extra long line will be obscured by the "main" div.

Also, the "main" div should be the rest of the browser's width, and in this case it's only as wide as the text inside.

like image 535
Mike Avatar asked Aug 10 '11 17:08

Mike


People also ask

How do you make a div wider as its contents?

You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).

How do I set the content width in CSS?

The width CSS property sets an element's width. By default, it sets the width of the content area, but if box-sizing is set to border-box , it sets the width of the border area.

Is a div 100% width by default?

So, by nature, it doesn't matter how much content the element contains because its width is always 100%, that is, until we alter it. Think of elements like <p> , <article> , <main> , <div> , and so many more.

Does width 100% include padding?

What is meant by width 100%? if you specify width:100%, the element's total width will be 100% of its containing block plus any horizontal margin, padding and border.


1 Answers

Not exactly sure about your question. I understand, that you want to have a dynamic left sidebar with a maximum width. If some text in the sidebar gets too long, it should be wrapped. Below is the code and a screenshot of how I imagine a solution:

  • Sidebar will be just as wide as the longest link (or something) in there (in the example below, there is a list in there)
  • If some content in the sidebar exceeds the maximum specified by the max-width property (here: 200px), it gets wrapped.
  • Main content takes up the remaining width.

Example A


Resizing the #main div (by resizing the browser window) works via overflow:hidden.

Example B


<html><head></head><body>

<div id="sidebar-left" 
    style="background:#EFEFEF; max-width:200px; float:left">
    <ul>
        <li>Navigation 1</li>
        <li>Navigation 2</li>
        <li>Extra Long Navigation item, 
            that exceeds our limit of 200px - and should be wrapped</li>
    </ul>
</div>

<div id="main" style="background:gray; overflow:hidden;">
    <p>Hello there - main content goes here</p>
</div>

</body></html>

If you don't want a max-sized sidebar, just drop the max-width attribute.

like image 136
miku Avatar answered Oct 06 '22 17:10

miku