I want a two-column div layout, where each one can have variable width e.g.
div {_x000D_
float: left;_x000D_
}_x000D_
_x000D_
.second {_x000D_
background: #ccc;_x000D_
}
_x000D_
<div>Tree</div>_x000D_
<div class="second">View</div>
_x000D_
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:
This question is related to
html
css
multiple-columns
You can try CSS Grid Layout.
dl {_x000D_
display: grid;_x000D_
grid-template-columns: max-content auto;_x000D_
}_x000D_
_x000D_
dt {_x000D_
grid-column: 1;_x000D_
}_x000D_
_x000D_
dd {_x000D_
grid-column: 2;_x000D_
margin: 0;_x000D_
background-color: #ccc;_x000D_
}
_x000D_
<dl>_x000D_
<dt>lorem ipsum</dt>_x000D_
<dd>dolor sit amet</dd>_x000D_
<dt>carpe</dt>_x000D_
<dd>diem</dd>_x000D_
</dl>
_x000D_
Check this solution out
.container {_x000D_
width: 100%;_x000D_
height: 200px;_x000D_
background-color: green;_x000D_
}_x000D_
.sidebar {_x000D_
float: left;_x000D_
width: 200px;_x000D_
height: 200px;_x000D_
background-color: yellow;_x000D_
}_x000D_
.content {_x000D_
background-color: red;_x000D_
height: 200px;_x000D_
width: auto;_x000D_
margin-left: 200px;_x000D_
}_x000D_
.item {_x000D_
width: 25%;_x000D_
background-color: blue;_x000D_
float: left;_x000D_
color: white;_x000D_
}_x000D_
.clearfix {_x000D_
clear: both;_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="clearfix"></div>_x000D_
<div class="sidebar">width: 200px</div>_x000D_
_x000D_
<div class="content">_x000D_
<div class="item">25%</div>_x000D_
<div class="item">25%</div>_x000D_
<div class="item">25%</div>_x000D_
<div class="item">25%</div>_x000D_
</div>_x000D_
</div>
_x000D_
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.
Here, this might help...
<html>_x000D_
_x000D_
<head>_x000D_
<style type="text/css">_x000D_
div.box {_x000D_
background: #EEE;_x000D_
height: 100px;_x000D_
width: 500px;_x000D_
}_x000D_
div.left {_x000D_
background: #999;_x000D_
float: left;_x000D_
height: 100%;_x000D_
width: auto;_x000D_
}_x000D_
div.right {_x000D_
background: #666;_x000D_
height: 100%;_x000D_
}_x000D_
div.clear {_x000D_
clear: both;_x000D_
height: 1px;_x000D_
overflow: hidden;_x000D_
font-size: 0pt;_x000D_
margin-top: -1px;_x000D_
}_x000D_
</style>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
<div class="box">_x000D_
<div class="left">Tree</div>_x000D_
<div class="right">View</div>_x000D_
<div class="clear" />_x000D_
</div>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
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"));
}
});
});
}
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.
<table WIDTH=100% border=0 cellspacing=0 cellpadding=2>_x000D_
<tr>_x000D_
<td WIDTH="1" NOWRAP bgcolor="#E0E0E0">Tree</td>_x000D_
<td bgcolor="#F0F0F0">View</td>_x000D_
</tr>_x000D_
</table>
_x000D_
Much less risky in terms of cross-browser compatibility too.
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>
<html>_x000D_
_x000D_
<head>_x000D_
<style type="text/css">_x000D_
div.box {_x000D_
background: #EEE;_x000D_
height: 100px;_x000D_
width: 500px;_x000D_
}_x000D_
div.left {_x000D_
background: #999;_x000D_
float: left;_x000D_
height: 100%;_x000D_
width: auto;_x000D_
}_x000D_
div.right {_x000D_
background: #666;_x000D_
height: 100%;_x000D_
}_x000D_
div.clear {_x000D_
clear: both;_x000D_
height: 1px;_x000D_
overflow: hidden;_x000D_
font-size: 0pt;_x000D_
margin-top: -1px;_x000D_
}_x000D_
</style>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
<div class="box">_x000D_
<div class="left">Tree</div>_x000D_
<div class="right">View</div>_x000D_
<div class="right">View</div>_x000D_
<div style="width: <=100% getTreeWidth()100 %>">Tree</div>_x000D_
<div class="clear" />_x000D_
</div>_x000D_
<div class="ColumnWrapper">_x000D_
<div class="ColumnOneHalf">Tree</div>_x000D_
<div class="ColumnOneHalf">View</div>_x000D_
</div>_x000D_
_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
Pat - You are right. That's why this solution would satisfy both "dinosaurs" and contemporaries. :)
.btnCont {_x000D_
display: table-layout;_x000D_
width: 500px;_x000D_
}_x000D_
_x000D_
.txtCont {_x000D_
display: table-cell;_x000D_
width: 70%;_x000D_
max-width: 80%;_x000D_
min-width: 20%;_x000D_
}_x000D_
_x000D_
.subCont {_x000D_
display: table-cell;_x000D_
width: 30%;_x000D_
max-width: 80%;_x000D_
min-width: 20%;_x000D_
}
_x000D_
<div class="btnCont">_x000D_
<div class="txtCont">_x000D_
Long text that will auto adjust as it grows. The best part is that the width of the container would not go beyond 500px!_x000D_
</div>_x000D_
<div class="subCont">_x000D_
This column as well as the entire container works like a table. Isn't Amazing!!!_x000D_
</div>_x000D_
</div>
_x000D_
You can use W3's CSS library that contains a class called rest
that does just that:
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">_x000D_
_x000D_
<div class="w3-row">_x000D_
<div class="w3-col " style="width:150px">_x000D_
<p>150px</p>_x000D_
</div>_x000D_
<div class="w3-rest w3-green">_x000D_
<p>w3-rest</p>_x000D_
</div>_x000D_
</div>
_x000D_
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
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 {_x000D_
display: flex;_x000D_
}_x000D_
_x000D_
.child {_x000D_
flex-grow: 1; // It accepts a unitless value that serves as a proportion_x000D_
}_x000D_
_x000D_
.left {_x000D_
background: red;_x000D_
}_x000D_
_x000D_
.right {_x000D_
background: green;_x000D_
}
_x000D_
<div class="parent"> _x000D_
<div class="child left">_x000D_
Left 50%_x000D_
</div>_x000D_
<div class="child right">_x000D_
Right 50%_x000D_
</div>_x000D_
</div>
_x000D_
Use calc
:
.leftSide {_x000D_
float: left;_x000D_
width: 50px;_x000D_
background-color: green;_x000D_
}_x000D_
.rightSide {_x000D_
float: left;_x000D_
width: calc(100% - 50px);_x000D_
background-color: red;_x000D_
}
_x000D_
<div style="width:200px">_x000D_
<div class="leftSide">a</div>_x000D_
<div class="rightSide">b</div>_x000D_
</div>
_x000D_
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.
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/
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{_x000D_
height:180px;_x000D_
border:3px solid #00f;_x000D_
display:flex;_x000D_
align-items:stretch;_x000D_
}_x000D_
div {_x000D_
display:flex;_x000D_
border:3px solid #0f0;_x000D_
}_x000D_
.second {_x000D_
display:flex;_x000D_
flex-grow:1;_x000D_
border:3px solid #f00;_x000D_
}
_x000D_
<div class="container">_x000D_
<div>Tree</div>_x000D_
<div class="second">View</div>_x000D_
</div>
_x000D_
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.
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.
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% ?
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/
Use the CSS Flexbox flex-grow
property to achieve this.
html, body {
height: 100%;
}
body {
display: flex;
}
.second {
flex-grow: 1;
}
_x000D_
<div style="background: #bef;">Tree</div>
<div class="second" style="background: #ff9;">View</div>
_x000D_
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!
Source: Stackoverflow.com