Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I adjust DIV width to contents

Tags:

html

css

layout

I have a div element with style attached:

.mypost {     border: 1px solid Peru;     font-family: arial;     margin: auto;     min-width: 700px;     width: 700px; } 

I am diplaying WordPress post contents inside the DIV block but for simplicity let assume that there is only one <img> inside the DIV. I want my div to be minimum 700 px wide and adjust the width if image is wider than 700 px.

What are my options to achieve that? Please advice.

UPDATE

See my Fiddle http://jsfiddle.net/cpt_comic/4qjXv/

like image 604
Captain Comic Avatar asked Jun 02 '11 10:06

Captain Comic


People also ask

How do I make a div fill full width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do I get content width in CSS?

The width property in CSS specifies the width of the element's content area. This “content” area is the portion inside the padding, border, and margin of an element (the box model). In the example above, elements that have a class name of . wrap will be 80% as wide as their parent element.

Can I use max-width fit-content?

fit-content as min- or max-widthYou can also use fit-content as a min-width or max-width value; see the example above. The first means that the width of the box varies between min-content and auto, while the second means it varies between 0 and max-content.


1 Answers

One way you can achieve this is setting display: inline-block; on the div. It is by default a block element, which will always fill the width it can fill (unless specifying width of course).

inline-block's only downside is that IE only supports it correctly from version 8. IE 6-7 only allows setting it on naturally inline elements, but there are hacks to solve this problem.

There are other options you have, you can either float it, or set position: absolute on it, but these also have other effects on layout, you need to decide which one fits your situation better.

  • inline-block jsFiddle Demo
like image 53
kapa Avatar answered Sep 23 '22 12:09

kapa