Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a div dynamically change to content width and have it remain that width even as the browser window changes size?

Tags:

html

css

width

Take a look at this: (Scenario 1)

#container {
    height:150px;
    border:1px solid pink;
    width:1100px;
}
#widget {
    display:inline-block;
    width:100px;
    height:100px;
    background-color:red;
}​

http://jsfiddle.net/DQFja/1/

Very simple. A div with a bunch of other divs within it. Parent div has a fixed width of ~1000 pixels, children divs are set to display:inline-block; When you make the browser window smaller than the parent, a scroll bar appears and you can scroll to see the rest.

Now take a look at this: (Scenario 2)

#container {
    height:150px;
    border:1px solid pink;
    float:left;
}
#widget {
    display:inline-block;
    width:100px;
    height:100px;
    background-color:red;
}​

http://jsfiddle.net/zZRq7/

Same parent and children, but the width of the parent is dynamically determined by the number of div children you place in it. Which is dandy. However, when you make the browser window smaller, the parent div becomes as wide as the window and the children start wrapping around, making the parent taller (Even though we set a fixed height!)

How do I make the parent div BOTH dynamic in width as in scenario 2, but then also keep its shape/height/width when the browser window gets smaller so we can get a scrollbar?

like image 384
Daniel F. Dietzel Avatar asked Nov 14 '12 22:11

Daniel F. Dietzel


People also ask

How do I change the width of a div dynamically?

Answer: Use the JavaScript width() method You can set the width of a <div> box dynamically using the jQuery width() method.

How do you auto adjust the div width according to content in it?

Using inline-block property: Use display: inline-block property to set a div size according to its content.

How do I make a div stay the same size?

if you set width:500px; and height:300px; (or whatever size you want) the div should stay the same size. Also, you can set overflow:hidden; so that if whatever is in the div goes beyond those bounds, it is not shown, and does not resize the div. Save this answer.

How do I make a div fit it's content?

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).


2 Answers

Min-width is the way to go:

#container {
    height:150px;
    border:1px solid pink;
    min-width:1100px; // set the minimum width of the container to 1100px
    width: 100%; // if the window size is bigger than 1100px, then make the container span the entire window
}

See a live example here.

A hacky way to achieve is using the white-space property as follows:

#container {
    border:1px solid pink;
    white-space: nowrap;
}
#widget {
    display: inline-block;
    width:100px;
    height:100px;
    background-color:red;
}​

See a live example here

like image 83
jacktheripper Avatar answered Oct 20 '22 14:10

jacktheripper


Updating answer to include the white-space line:

#container {
    height:150px;
    border:1px solid pink;
    white-space:nowrap;
    overflow:auto;
}

http://jsfiddle.net/DQFja/3/

like image 43
MikeSmithDev Avatar answered Oct 20 '22 15:10

MikeSmithDev