I want a two-column div layout, where each one can have variable width e.g.
div { float: left; } .second { background: #ccc; }
I want the 'view' div to expand to the whole width available after 'tree' div has filled needed space.
Currently, my 'view' div is resized to content it contains It will also be good if both divs take up the whole height.
Not duplicate disclaimer:
Expand div to max width when float:left is set because there the left one has a fixed width.
Help with div - make div fit the remaining width because I need two columns both aligned to left
overflow hidden
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; }
I just discovered the magic of flex boxes (display: flex). Try this:
<style>
#box {
display: flex;
}
#b {
flex-grow: 100;
border: 1px solid green;
}
</style>
<div id='box'>
<div id='a'>Tree</div>
<div id='b'>View</div>
</div>
Flex boxes give me the control I've wished css had for 15 years. Its finally here! More info: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
flex-grow: 100;
instead of flex-grow: 1;
?
flex-grow:10
and then I'd set #b to flex-grow: 90
so #a would be 10% of the line's width and #b would be 90% of the line's width. If no other elements have a flex width style, then it doesn't technically matter what you put.
Use the CSS Flexbox flex-grow
property to fill the remaining space.
html, body { height: 100%; } body { display: flex; } .second { flex-grow: 1; }
This would be a good example of something that's trivial to do with tables and hard (if not impossible, at least in a cross-browser sense) to do with CSS.
If both the columns were fixed width, this would be easy.
If one of the columns was fixed width, this would be slightly harder but entirely doable.
With both columns variable width, IMHO you need to just use a two-column table.
display:none;
Whilst making as much as possible 100% responsive on a sliding scale sometimes it's nicer to change your design completely for a mobile making use of the touch screen feel and button style.
Use calc
:
.leftSide { float: left; width: 50px; background-color: green; } .rightSide { float: left; width: calc(100% - 50px); background-color: red; }
The problem with this is that all widths must be explicitly defined, either as a value(px and em work fine), or as a percent of something explicitly defined itself.
Check this solution out
.container { width: 100%; height: 200px; background-color: green; } .sidebar { float: left; width: 200px; height: 200px; background-color: yellow; } .content { background-color: red; height: 200px; width: auto; margin-left: 200px; } .item { width: 25%; background-color: blue; float: left; color: white; } .clearfix { clear: both; }
Here, this might help...
<div class="clear" style="clear: both; height: 1px; overflow: hidden; font-size:0pt; margin-top: -1px;"></div>
If the width of the other column is fixed, how about using the calc
CSS function working for all common browsers:
width: calc(100% - 20px) /* 20px being the first column's width */
This way the width of the second row will be calculated (i.e. remaining width) and applied responsively.
css-grid
. Also works with position: fixed
which makes it all around perfect choice! Thank you!
I don't understand why people are willing to work so hard to find a pure-CSS solution for simple columnar layouts that are SO EASY using the old TABLE
tag.
All Browsers still have the table layout logic... Call me a dinosaur perhaps, but I say let it help you.
Tree | View |
Much less risky in terms of cross-browser compatibility too.
media-queries
to put both columns one above the other in small devices, it is impossible with the old table
tag. To get this working, you need to apply CSS
table styles. So, for nowadays it is better to solve this with CSS
if you want a responsive layout for any screen size.
You can try CSS Grid Layout.
dl { display: grid; grid-template-columns: max-content auto; } dt { grid-column: 1; } dd { grid-column: 2; margin: 0; background-color: #ccc; }
flex-grow - This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.
If all items have flex-grow set to 1, the remaining space in the container will be distributed equally to all children. If one of the children has a value of 2, the remaining space would take up twice as much space as the others (or it will try to, at least). See more here
.parent { display: flex; } .child { flex-grow: 1; // It accepts a unitless value that serves as a proportion } .left { background: red; } .right { background: green; }
Im not sure if this is the answer you are expecting but, why don't you set the width of Tree to 'auto' and width of 'View' to 100% ?
A slightly different implementation,
Two div panels(content+extra), side by side, content panel
expands if extra panel
is not present.
jsfiddle: http://jsfiddle.net/qLTMf/1722/
Pat - You are right. That's why this solution would satisfy both "dinosaurs" and contemporaries. :)
.btnCont { display: table-layout; width: 500px; } .txtCont { display: table-cell; width: 70%; max-width: 80%; min-width: 20%; } .subCont { display: table-cell; width: 30%; max-width: 80%; min-width: 20%; }
You can use W3's CSS library that contains a class called rest
that does just that:
150px
w3-rest
Don't forget to link the CSS library in the page's header
:
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
Here's the official demo: W3 School Tryit Editor
This is fairly easy using flexbox. See the snippet below. I've added a wrapper container to control flow and set a global height. Borders have been added as well to identify the elements. Notice that divs now expand to the full height as well, as required. Vendor prefixes should be used for flexbox in a real world scenario since is not yet fully supported.
I've developed a free tool to understand and design layouts using flexbox. Check it out here: http://algid.com/Flex-Designer
.container{ height:180px; border:3px solid #00f; display:flex; align-items:stretch; } div { display:flex; border:3px solid #0f0; } .second { display:flex; flex-grow:1; border:3px solid #f00; }
stretch
because it's the default value and no need to make the child element flex container so display:flex
is useless for the inner elements
Have a look at the available CSS layout frameworks. I would recommend Simpl or, the slightly more complex, Blueprint framework.
If you are using Simpl (which involves importing just one simpl.css file), you can do this:
<div class="ColumnOneHalf">Tree</div>
<div class="ColumnOneHalf">View</div>
, for a 50-50 layout, or :
<div class="ColumnOneQuarter">Tree</div>
<div class="ColumnThreeQuarters">View</div>
, for a 25-75 one.
It's that simple.
Thanks for the plug of Simpl.css!
remember to wrap all your columns in ColumnWrapper
like so.
<div class="ColumnWrapper">
<div class="ColumnOneHalf">Tree</div>
<div class="ColumnOneHalf">View</div>
</div>
I am about to release version 1.0 of Simpl.css so help spread the word!
I wrote a javascript function that I call from jQuery $(document).ready(). This will parse all children of the parent div and only update the right most child.
html
...
<div class="stretch">
<div style="padding-left: 5px; padding-right: 5px; display: inline-block;">Some text
</div>
<div class="underline" style="display: inline-block;">Some other text
</div>
</div>
....
javascript
$(document).ready(function(){
stretchDivs();
});
function stretchDivs() {
// loop thru each <div> that has class='stretch'
$("div.stretch").each(function(){
// get the inner width of this <div> that has class='stretch'
var totalW = parseInt($(this).css("width"));
// loop thru each child node
$(this).children().each(function(){
// subtract the margins, borders and padding
totalW -= (parseInt($(this).css("margin-left"))
+ parseInt($(this).css("border-left-width"))
+ parseInt($(this).css("padding-left"))
+ parseInt($(this).css("margin-right"))
+ parseInt($(this).css("border-right-width"))
+ parseInt($(this).css("padding-right")));
// if this is the last child, we can set its width
if ($(this).is(":last-child")) {
$(this).css("width","" + (totalW - 1 /* fudge factor */) + "px");
} else {
// this is not the last child, so subtract its width too
totalW -= parseInt($(this).css("width"));
}
});
});
}
If both of the widths are variable length why don't you calculate the width with some scripting or server side?
<div style="width: <=% getTreeWidth() %>">Tree</div>
<div style="width: <=% getViewWidth() %>">View</div>