I have a div that contains a fixed width content (image inside another div) and a variable width content (text that changes dynamically). I need this outer div to:
Because it should be the only element on the line, I can not use display:inline
or display:inline-block
. On the other hand, display:block
does not auto-shrink. Here is my code:
http://jsfiddle.net/D9QV9/
My experiments with overflow
, float
and clear
yielded no result. Any help, advice, link etc is very welcome. Thank you.
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).
Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.
The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width). The problem with the <div> above occurs when the browser window is smaller than the width of the element (500px).
Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.
This is how I did: I gave #outer
the CSS-style display:inline-block
and then simply put that in another div with the css display:block
to make sure that #outer
stayed on a separate line:
CSS
#outer {
text-align: center;
border-style: solid;
border-width: 1px;
border-color: #000000;
height: 50px;
width:auto;
display:inline-block;
}
#image {
float: right;
width: 50px;
}
#element_block {
display:block;
}
HTML
Some content above div
<div id="element_block">
<div id="outer">
Text inside div<div id="image">IMG</div>
</div>
</div>
Some content below div
DEMO
Use flex box:
.holder{
display:flex;
flex-direction:column;
align-items:center;
}
.item{
border:1px solid red;
margin:10px;
padding:10px;
}
<div class="holder">
<div class="item">
Item has a lot of text
</div>
<div class="item">
Small
</div>
<div class="item">
Very big and wide with lots of content
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With