Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set this div to be the minimum possible width to display its floating contents?

Tags:

html

css

Problem

I have "boxes" that float left so that I can display them in a line until they need to wrap. This works well, but my coloured background doesn't shrink to the minimum, it expands to the maximum.

JSFiddle

http://jsfiddle.net/RLRh6/

(Expand and shrink the Result section to see the effect)

HTML

<div class="container">
    <div class="boxes">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
</div>

CSS

.container {
    background: #fcc;
    margin: 0 auto;
    max-width: 300px;
}

.boxes {
    background: #cfc;
    overflow: hidden;
}

.box {
    border: 1px dashed blue;
    width: 70px;
    height: 50px;
    float: left;
    margin: 2px;
}

What I Get

Note the extra green colour, right of the boxes:

Example 1

Example 1

Example 2

Example 2

What I Want

Example 1

Example 1

Example 2

Example 2

Question

Is it possible to have the green-background div (".boxes") shrink to the minimum possible size to display the boxes without Javascript? You should be able to shrink and expand the div freely and not see any green to the right of the boxes.

like image 968
Rowan Freeman Avatar asked Jun 03 '13 06:06

Rowan Freeman


People also ask

How do I make a div less wide?

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

Can you set a minimum width CSS?

The min-width CSS property sets the minimum width of an element. It prevents the used value of the width property from becoming smaller than the value specified for min-width .

How do you make a div float in HTML?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.


2 Answers

Working DEMO http://jsfiddle.net/RLRh6/56/

If you want to achieve this only with html, css, should use media queries.

CSS

.container {
    margin: 0 auto;
    min-width: 76px;
    max-width: 228px;
}
@media only screen and (min-width: 76px) {
  .boxes {
      float:left;
      background: #cfc;
      width: 76px;
  }
}
@media only screen and (min-width: 152px) {
  .boxes {
      float:left;
      background: #cfc;
      width: 152px;
  }
}
@media only screen and (min-width: 228px) {
  .boxes {
      float:left;
      background: #cfc;
      width: 228px;
  }
}
.boxes {
    float:left;
    background: #cfc;
}

.box {
    border: 1px dashed blue;
    width: 70px;
    height: 50px;
    float: left;
    margin: 2px;
}
like image 166
yeyene Avatar answered Sep 20 '22 03:09

yeyene


Remove the min-width from the .container and add display:inline-block;

also if you want to center the .container then wrap the .container with a div and apply text-align:center to it.

.container {
    background: #fcc;
    margin: 0 auto;
   display:inline-block;
}

jsFiddle Link

like image 42
Roy Sonasish Avatar answered Sep 17 '22 03:09

Roy Sonasish