How to Set This Div to Be The Minimum Possible Width to Display Its Floating Contents

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

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;
}

How to make a div with no content have a width?

a div usually needs at least a non-breaking space ( ) in order to have a width.

Expand a div to fill the remaining width

The solution to this is actually very easy, but not at all obvious. You have to trigger something called a "block formatting context" (BFC), which interacts with floats in a specific way.

Just take that second div, remove the float, and give it overflow:hidden instead. Any overflow value other than visible makes the block it's set on become a BFC. BFCs don't allow descendant floats to escape them, nor do they allow sibling/ancestor floats to intrude into them. The net effect here is that the floated div will do its thing, then the second div will be an ordinary block, taking up all available width except that occupied by the float.

This should work across all current browsers, though you may have to trigger hasLayout in IE6 and 7. I can't recall.

Demos:

  • Fixed Left: http://jsfiddle.net/A8zLY/5/
  • Fixed Right: http://jsfiddle.net/A8zLY/2/

div {
float: left;
}

.second {
background: #ccc;
float: none;
overflow: hidden;
}
<div>Tree</div>
<div class="second">View</div>

make div's height expand with its content

You need to force a clear:both before the #main_content div is closed. I would probably move the <br class="clear" />; into the #main_content div and set the CSS to be:

.clear { clear: both; }

Update: This question still gets a fair amount of traffic, so I wanted to update the answer with a modern alternative using a new layout mode in CSS3 called Flexible boxes or Flexbox:

body {  margin: 0;}
.flex-container { display: flex; flex-direction: column; min-height: 100vh;}
header { background-color: #3F51B5; color: #fff;}
section.content { flex: 1;}
footer { background-color: #FFC107; color: #333;}
<div class="flex-container">  <header>    <h1>     Header       </h1>  </header>
<section class="content"> Content </section>
<footer> <h4> Footer </h4> </footer></div>

How to make a div fill a remaining horizontal space?

This seems to accomplish what you're going for.

#left {  float:left;  width:180px;  background-color:#ff0000;}#right {  width: 100%;  background-color:#00FF00;}
<div>  <div id="left">    left  </div>  <div id="right">    right  </div></div>

Set a floating div's width to take up remaining space

Remove float and clear from .display-field. Now the .display-field div starts from the left side of the browser so you need to add the desired colors to the divs to manipulate the output.

.display-label {
float:left;
clear:left;
min-width:160px;
background:white
}

.display-field {
background:red
}​

DEMO

Preventing div from breaking off and shooting down

Using CSS table layout. Wrap the two divs into a container.

JsFiddle Demo

.main {    display: inline-block;    border: 1px solid black;}.images {    display: table;    width: 100%;}.big, .small {    display: table-cell;    vertical-align: top;}.small {    text-align: right;}
<div class="wrapper">    <div class="main">        <p>Using display table so the box will wrap to this text</p>        <p>Ref. <a href="http://stackoverflow.com/questions/27190514/making-divs-inside-flex-boxes-shrink-wrap">StackOverflow #27190514</a></p>        <div class="images">            <div class="big">                <img src="http://placehold.it/200/200" />            </div>            <div class="small">                <img src="http://placehold.it/50/50" />            </div>        </div>    </div></div>

Two divs, one fixed width, the other, the rest

See: http://jsfiddle.net/SpSjL/ (adjust the browser's width)

HTML:

<div class="right"></div>
<div class="left"></div>

CSS:

.left {
overflow: hidden;
min-height: 50px;
border: 2px dashed #f0f;
}

.right {
float: right;
width: 250px;
min-height: 50px;
margin-left: 10px;
border: 2px dashed #00f;
}

You can also do it with display: table, which is usually a better approach: How can I put an input element on the same line as its label?

CSS when inline-block elements line-break, parent wrapper does not fit new width

You can't. By default, inline-block elements have a shrink-to-fit width:

The shrink-to-fit width is:

min(max(preferred minimum width, available width), preferred width).

Then,

  • When preferred minimum width <= preferred width <= available width, the width will be the preferred width, as you desire.
  • When available width <= preferred minimum width <= preferred width, the width will be the preferred minimum width, as you desire.
  • When preferred minimum width <= available width <= preferred width, the width will be the available width, even if you don't like it.

If you really don't want this, I guess you could add a resize event listener with JS, and set the desired width manually.



Related Topics



Leave a reply



Submit