I'm trying to make the div have same height so i used display table , problem that i having is the width will grow.
As an example try adding content in the tableright , when it is exceeded the width , it will not break to the next line but it will expand horizontally to the tableleft.
Please Advice.
Below is the sample code
<div id="maintable">
<div id="table-row">
<div id="tableleft></div>
<div id="tableright></div>
</div>
</div>
<style>
#maintable
{display:table;
width:100%;
}
#table-row
{
display:table-row
width:100%;
}
#tableleft
{
width:60%;
display:table-cell;
}
#tableright
{
width:40%;
display:table-cell;
}
</style>
You made few syntax error there.
Further to make sure if one big un spaced word is placed in table-cell i have used "table-layout: fixed;"
check this fiddle: http://jsfiddle.net/5q9K8/17/
The HTML:
<div id="maintable">
<div id="table-row">
<div id="tableleft">lorem</div>
<div id="tableright">ipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsumipsum</div>
</div>
</div>
The Css:
#maintable
{
display:table;
width:100%;
table-layout: fixed;
word-wrap:break-word;
}
#table-row
{
display:table-row;
width:100%;
}
#tableleft
{
width:60%;
display:table-cell;
background: #eef;
}
#tableright
{
width:40%;
display:table-cell;
background: #efe;
}
Add width as accordingly here just an example to show div height equal to table height.
css
.maintable {
width:100%;
display: table;
border: 1px solid blue;
}
.maintable .table-row {
display: table-row;
border: 2px solid black;
}
.maintable .table-row .tableleft, .maintable .table-row .tableright {
display: table-cell;
border: 4px solid red;
}
.tableright {
width: 60%;
}
.tableleft {
width: 40%;
}
DEMO
Final DEMO
Related
This is blowing my mind. I have a wrapper div and 2 divs inside it, one of the divs its height is 100% but does not stretch to fit the wrapper.
Here is a JSFIDDLE
The HTML:
<div class="wrapper">
<div class="inner_left">
Just<br />Some<br />Words
</div>
<div class="inner_right">
Anything
</div>
</div>
The CSS:
.wrapper {
width:auto !important;
height:auto !important;
margin:auto;
float:left;
padding:0;
background-color:#ccc;
}
.inner_left {
background-color:#f0f0f0;
width:270px;
height:auto !important;
border:1px solid #666;
float:left;
margin:auto;
padding:10px;
text-align:center;
}
.inner_right {
background-color:#f0f0f0;
width:200px;
height:100%;
border:1px solid #666;
float:right;
margin:auto;
padding:10px;
text-align:center;
}
I need the div (inner_right) to auto fit the height of the wrapper. So whenever the wrapper's height shrinks or stretches, this div stretches to the maximum height of the wrapper.
Anyone knows why my code isn't working? Appreciate any help.
Here is a solution using display:table and display:table-cell
.wrapper {
display: table;
width: 100%; /* whatever you want */
padding: 0;
background-color: #ccc;
}
.wrapper > div {
display: table-cell;
border: 1px solid #666;
background-color: #f0f0f0;
padding: 10px;
text-align: center;
}
.inner_left {
width: 270px;
}
.inner_right {
width: 200px;
}
<div class="wrapper">
<div class="inner_left">Just
<br />Some
<br />Words</div>
<div class="inner_right">Anything</div>
</div>
#showdev is right, the parent element needs to have its height explicitly set in order for the height of the child element to work the way you want it to.
Try to set 100% height to whole document:
html,body {
height: 100%;
}
and for wrapper class:
.wrapper {
width:auto !important;
height: 100%;
margin:auto;
float:left;
padding:0;
background-color:#ccc;
}
fiddle
I'm trying to set up automatically resizable divs based on the content that will go into them. The code is kind of hackey but I was wondering if anyone could provide me some suggestions on getting it to work properly.
CSS:
.container { min-width:748px; margin-left:180px; margin-right:3px; border: 1px solid #210B61; border-top:none; }
.first_div { width:233px; background:#A9BCF5; border: 1px solid #210B61; float:left; overflow:auto; }
.second_div { background:#E0ECF8; overflow:hidden; }
.label_for_first_div { width:100px; text-align:left margin-left:5px; }
HTML:
<div class="container">
<div class="first_div">
<div class="label_for_first_div">
</div>
</div>
<div class="second_div">
</div>
</div>
The trouble with this is the first_div will not display inside the container without the use of the label_for_first_div. Getting the first div to fit the height of that container has been troubling too. Would using display:table; be my best option?
you can use calc() to accomplish this
.container {
min-width:748px;
margin-right:3px;
border: 1px solid #210B61;
border-top:none;
overflow: hidden;
}
.first_div {
width:233px;
background:#A9BCF5;
float:left;
}
.second_div {
width: calc(100% - 233px);
background:red;
float: left;
}
EXAMPLE 1
OR
You can do this by setting the divs to display: table-cell and the parent to display: table
.container {
display: table;
min-width:748px;
margin-right:3px;
border: 1px solid #210B61;
border-top:none;
}
.first_div {
display: table-cell;
width:233px;
background:#A9BCF5;
}
.second_div {
display: table-cell;
background:red;
}
EXAMPLE 2
I'm looking for a CSS layout solution (IE9+) that will mimic "auto adjustable table cells" like so:
The only thing I change using tables, is the bottom cells height, the upper cell automatically response to the change.
Here is the table code and a JSFiddle demo that randomize the bottom cell height.
HTML:
<div>
<table>
<tr>
<td>Auto Adjust</td>
</tr>
<tr>
<td id="adjust">Fixed Height</td>
</tr>
</table>
</div>
CSS:
td {
border:solid 1px red
}
table {
width:100%;
height:100%;
}
div {
height:300px;
border:solid 1px blue;
}
#adjust{
height:50px;
}
Q: How can I achieve the same goal using modern layout techniques?
How about this?
http://jsfiddle.net/NicoO/HfpL6/4/
.wrap
{
height:300px;
border:solid 1px blue;
}
.table {
width:100%;
height:100%;
display: table;
border-spacing: 2px;
}
.row
{
display: table-row;
}
.cell
{
display: table-cell;
border:solid 1px red;
vertical-align: middle;
}
#adjust{
height:50px;
}
This html:
<div class="wrap">
<div class="table">
<div class="row">
<div class="cell">
Auto Adjust
</div>
</div>
<div class="row">
<div id="adjust" class="cell">
Fixed Height
</div>
</div>
</div>
</div>
Update
The only other possibility i see is to use calc you sure need some more JS then:
http://jsfiddle.net/NicoO/HfpL6/7/
You need to get all children with JS and apply a new height to all of them.
html, body
{
height: 100%;
margin:0;
padding:0;
}
*
{
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.wrap
{
height:300px;
border:solid 1px blue;
}
.table
{
width:100%;
height:100%;
}
.row
{
height: 50%;
position: relative;
}
.cell
{
border:solid 1px red;
vertical-align: middle;
position: absolute;
top:2px;
right:2px;
bottom:2px;
left:2px;
}
#adjust:not([style])
{
background-color: green;
}
You could try Flexbox. It is meant to handle layout with both flexible and fixed widths.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes
I am having some trouble with div positioning. I'm working on a comment system in wich comments can get upvotes and downvotes. For every comment the up/down vote-buttons needs to be left of my comment text, and vertically aligned in the middle of my comment-container div. (regardless of how big the comment is)
At the moment it wont work properly, because the buttons wont get to the middle of the div. (see: http://jsfiddle.net/mcSfe/1838/)
In the testcase i want the leftside to be stretched all the way down, and the red box vertically centered in the middle of the leftside. vertical-align, and display:table-cell, did not brought the right result..
Here is my test html code:
<div class="commentContainer">
<div class="leftside">
<div class="innerleft">
test
</div>
</div>
<div class ="CommentBox">
<p>hello</p>
<p>this is my comment</p>
<p>another line of comment</p>
</div>
and here is my test css code:
div.commentContainer{
float:left;
border:1px solid blue;
}
div.leftside {
float:left;
width: 50px;
background: gray;
text-align: center;
}
div.innerleft {
float:left;
width: 25px;
height: 25px;
margin-left:13px;
background: red;
}
div.CommentBox {
float:right;
width:200px;
background-color:green;
}
Remove float from .commentbox and .leftside and add display:table-cell with vertical-align:middle
div.commentContainer{
float:left;
border:1px solid blue;
}
div.leftside {
width: 50px;
background: gray;
text-align: center;
display: table-cell;
vertical-align: middle
}
div.innerleft {
float:left;
width: 25px;
height: 25px;
margin-left:13px;
background: red;
}
div.CommentBox {
width:200px;
background-color:green;
display: table-cell
}
DEMO
LIke this
demo
css
div.commentContainer{
float:left;
border:1px solid blue;
display:table;
}
div.leftside {
display:table-cell;
width: 50px;
background: gray;
text-align: center;
}
div.innerleft {
width: 25px;
height: 25px;
margin-left:13px;
background: red;
vertical-align:middle;
}
div.CommentBox {
display:table-cell;
width:200px;
background-color:green;
}
Inside of using floats, use inline-block.
JSFiddle
CSS
div.commentContainer{
float:left;
border:1px solid blue;
}
div.leftside {
display:inline-block;
vertical-align:middle;
width: 50px;
background: gray;
text-align: center;
}
div.innerleft {
float:left;
width: 25px;
height: 25px;
margin-left:13px;
background: red;
}
div.CommentBox {
display:inline-block;
vertical-align:middle;
width:200px;
background-color:green;
}
Issues regarding inline-block whitespace can be addressed separately.
I am unable to set my text as vertical-center. My text is placed in position:absolute div.
<div class="mydiv">Frameworks and Extensions</div>
and the CSS:
.mydiv{
width:100px;
height:70px;
border:1px solid red;
position:absolute;
text-align:center;
background-color:#ccc;
}
See fiddle: http://jsfiddle.net/3Zv5s
Sometimes Text will come two or one line. The text should be in vertical center like in table td.
Thanks for your valuable time and suggestion.
Try with this CSS:
.mydiv{
width:100px;
height:70px;
border:1px solid red;
display: table-cell;
vertical-align: middle;
text-align:center;
background-color:#ccc;
}
Try this
UPDATED
You could set container to position: absolute; and make .mydiv to display:table-cell and vertical-align:middle.
HTML -
<div id="container"><div class="mydiv">Frameworks and Extensions</div></div>
CSS -
#container{
position: absolute;
}
.mydiv{
width:100px;
height:70px;
border:1px solid red;
display: table-cell;
vertical-align: middle;
text-align:center;
background-color:#ccc;
}
Try for this
You could put your text into a table and use vertical-align
http://jsfiddle.net/3Zv5s/6/
Try this:
HTML:
<div class="mydiv">
<span class="span">Frameworks and Extensions</span>
</div>
CSS:
.mydiv{display:table-row;}
.span{display:table-cell;vertical-align:middle;height:inherit;}
Fiddle here.
You could use a combination of display: table and display: table-cell.
Change slightly your markup:
<div class="mydiv">
<div>Frameworks and Extensions</div>
</div>
And your CSS:
.mydiv{
width:100px;
height:70px;
border:1px solid red;
display: table;
background-color:#ccc;
overflow: hidden;
}
.mydiv div {
vertical-align: middle;
display: table-cell;
}
http://jsfiddle.net/3Zv5s/2/
Option #1 : table-cell
Simplest solution, but only for IE7+
<div class="mydiv">
<span>Frameworks and Extensions</span>
</div>
.mydiv {
width:100px;
height:70px;
border:1px solid red;
position:absolute;
text-align:center;
background-color:#ccc;
display: table;
}
.mydiv>span {
display: table-cell;
vertical-align: middle;
}
Fiddle
Option #2 : double span
A bit more tricky, but works under IE7
<div class="mydiv">
<span><span>Frameworks and Extensions</span></span>
</div>
.mydiv {
width:100px;
height:70px;
border:1px solid red;
position:absolute;
text-align:center;
background-color:#ccc;
display: block;
line-height: 65px; /* 70px applied on 1st span */
}
.mydiv>span {
display: inline-block;
vertical-align: middle; /* 2nd span centered */
line-height: 0;
}
.mydiv>span>span {
line-height: 20px; /* here's the "true" line-height */
}
Fiddle