David Silva
David Silva

Reputation: 2017

Outer div should adjust to image size

I have code like this:

<div class="container">
    <img src="img_url" alt="" />
</div>​

Container's width has set on 600px but if image is bigger, container's width should adjust to image size. How I should fix my css?

.container {
    margin: auto;
    margin-bottom: 30px;
    padding: 10px;
    width: 400px;
    background-color: #2a2929;
    border: 1px solid #212020;    
}

.container img {
    display: block;
    margin-left: auto;
    margin-right: auto;    
}​

Demo: http://jsfiddle.net/Gzjp7/3/

Upvotes: 0

Views: 1279

Answers (2)

bashleigh
bashleigh

Reputation: 9314

add to the container img

width:100%;
height:auto;

that should keep the image within the container

Upvotes: 1

Jan Hančič
Jan Hančič

Reputation: 53931

Sen min-width on the container, instead of width.

This will ensure that the div will have at least 400px width, and will expand if necessary.

.container {
    margin: auto;
    margin-bottom: 30px;
    padding: 10px;
    min-width: 400px;
    background-color: #2a2929;
    border: 1px solid #212020;    
}

If you don't want a minimal width for your container then just omit the width property, or set it to auto (which is the default).

Upvotes: 2

Related Questions