Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a block div that fits content width?

Tags:

html

css

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:

  • start from a new line
  • be exactly as wide as its content
  • preferably end in a new line

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.

like image 206
Eques Avatar asked Dec 19 '13 04:12

Eques


People also ask

How do you make a div fit the width of 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 make a div auto adjust width?

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.

Can we give width to div?

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

How do you make div width and height auto adjust to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.


2 Answers

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

like image 188
display-name-is-missing Avatar answered Oct 11 '22 14:10

display-name-is-missing


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>
like image 35
David Mold Avatar answered Oct 11 '22 13:10

David Mold