Fluent table width - html

This behaviour is a little weird, I think.
I have such a structure
<div class="canvas">
<div class="table">
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
</div>
</div>
Where .table, .row, .cell are display:table, table-row and table-cell.
Say a cell has a fixed width of 100px and the canvas has a width of 250px and overflow:hidden, the last cell in the row will break down. The table seems to inherit the width of the canvas. Never defined it anywhere.
Why is table not just simply growing with it cells? I mean it's a table, I don't want rows to break. If I wanted that, I would use divs.
How can I get the table to behave like I expected?
Thanks.

Consider these 3 samples, the first 2 have a surrounding div where the last of those 2 is using "min-width" and the last (3:rd) without any div.
All 3 "groups" have a div with display: table and a normal table element.
As far as I can see, they all behave as they supposed to, so to answer your question(s):
It's correct that there isn't a defined width on the table, but there is one on its parent, which then sets a boundary to which the table try to adapt. There is also a width set to a cell, which it tries to adapt to. This should make (and obviously does) the table's cells to break line.
If one would remove all the width/min-width: *px then it will start to grow, as I guess you expected, but only until it hits the next boundary, which in this case is the body, and when there, it will start break line again.
How do you expect them to behave?
.canvas {
width: 250px;
overflow: hidden;
}
.canvas.min-width {
width: auto;
min-width: 250px;
}
.table {
display: table;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
width: 100px;
vertical-align: top;
}
<div class="canvas">
<div class="table">
<div class="row">
<div class="cell"> Heyyyy </div>
<div class="cell"> Heyyyyy 2 </div>
<div class="cell"> Heyyyyyy 33333333 44444444444 5555555555555555</div>
</div>
</div>
<table>
<tr>
<td class="cell"> Heyyyy </td>
<td class="cell"> Heyyyyy 2 </td>
<td class="cell"> Heyyyyyy 33333333 44444444444 5555555555555555</td>
</tr>
</table>
</div>
<hr />
<div class="canvas min-width">
<div class="table">
<div class="row">
<div class="cell"> Heyyyy </div>
<div class="cell"> Heyyyyy 2 </div>
<div class="cell"> Heyyyyyy 33333333 44444444444 5555555555555555</div>
</div>
</div>
<table>
<tr>
<td class="cell"> Heyyyy </td>
<td class="cell"> Heyyyyy 2 </td>
<td class="cell"> Heyyyyyy 33333333 44444444444 5555555555555555</td>
</tr>
</table>
</div>
<hr />
<div class="table">
<div class="row">
<div class="cell"> Heyyyy </div>
<div class="cell"> Heyyyyy 2 </div>
<div class="cell"> Heyyyyyy 33333333 44444444444 5555555555555555</div>
</div>
</div>
<table>
<tr>
<td class="cell"> Heyyyy </td>
<td class="cell"> Heyyyyy 2 </td>
<td class="cell"> Heyyyyyy 33333333 44444444444 5555555555555555</td>
</tr>
</table>

Related

Flexbox to change position of elements

Is it possible to use flexbox to do something like in this example? Basically Id like to flip elements which are in a column to be in a row when viewed in mobile. How would you do this?
<p> Desktop:</p>
<table style="height: 52px;" width="331">
<tbody>
<tr>
<td style="width: 103.533px;">image-icon1</td>
<td style="width: 103.533px;">image-icon2</td>
<td style="width: 103.533px;">image-icon3</td>
</tr>
<tr>
<td style="width: 103.533px;">Text1</td>
<td style="width: 103.533px;">Text2</td>
<td style="width: 103.533px;">Text3</td>
</tr>
</tbody>
</table>
<p> Mobile:</p>
<table style="height: 71px;" width="189">
<tbody>
<tr>
<td style="width: 87.1px;">image-icon1</td>
<td style="width: 87.1px;">Text1</td>
</tr>
<tr>
<td style="width: 87.1px;">image-icon2</td>
<td style="width: 87.1px;">Text2</td>
</tr>
<tr>
<td style="width: 87.1px;">image-icon3</td>
<td style="width: 87.1px;">Text3</td>
</tr>
</tbody>
</table>
Yes, its possible
Check below example.
I am using CSS media query #media(max-width:768px) to check whether the width of screen is less than 768px(can be any value less than this).
Depending on that, I am setting the direction of flex children to column, so that the children can stack on top of each other.
Along with that, on mobile screen, I am also setting, the display of inner containers(.sec class) to flex,
<div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div>
So that, its children will align itself in row.
.container {
display: flex;
flex-direction: row;/* default value*/
}
.text,.img{
padding:10px;
}
.sec {
flex: 1;
background-color: wheat;
padding: 5px;
border: 1px solid black;
text-align: center;
}
#media(max-width:768px) { /* can be anything less than this*/
.container {
flex-direction: column;
}
.sec {
display: flex;
flex-direction: row;/* default value*/
}
}
<div class="container">
<div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div>
<div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div> <div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div> <div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div> <div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div> <div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div> <div class="sec">
<div class="img">Image</div>
<div class="text">
Text
</div>
</div>
</div>

Bootstrap 4 spacing in tables

There is a gap between elements of an input group, if put into a table.
<link href="https://rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css" rel="stylesheet"/>
<p>This is fine</p>
<div class="row">
<div class="col-xs-4">
<div class="input-group">
<div class="input-group-addon">a</div>
<div class="input-group-addon">b</div>
</div>
</div>
</div>
<table>
<tr>
<td>There is a gap between spans in tables</td>
</tr>
<tr>
<td>
<div class="input-group">
<div class="input-group-addon">a</div>
<div class="input-group-addon">b</div>
</div>
</td>
</tr>
</table>
How can I get rid of this gap?
You could try removing the border-spacing added by the table. Alternatively, you can set the border-collapse to collapse if you prefer the way that looks.
border-spacing is inherited by default in CSS by child elements. input-group is set to display: table which means it inherits the borders-spacing: 2px from the parent table. This means it will be applied to input-group-addons since they are being displayed as table cells.
table .input-group {
border-spacing: 0;
}
<link href="https://rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css" rel="stylesheet"/>
<p>This is fine</p>
<div class="row">
<div class="col-xs-4">
<div class="input-group">
<div class="input-group-addon">a</div>
<div class="input-group-addon">b</div>
</div>
</div>
</div>
<table>
<tr>
<td>Previously a gap between elements</td>
</tr>
<tr>
<td>
<div class="input-group">
<div class="input-group-addon">a</div>
<div class="input-group-addon">b</div>
</div>
</td>
</tr>
</table>

Using div with display table-cell inside real tables td

I have a complicated layout created with real tables and it works fine but now I created a new layout using DIVS and it worked fine until I tested height on cellphones and it looks pretty bad, it just don't want to keep my height 100% tested also set footer to margin bottom 0 and nothing so I will test the follow:
<table style="width:100%;height:100%;border:0;border-spacing:0px;border-collapse:collapse;">
<tr>
<td style="height:21px;">
<DIV style="display: table-cell;"> divvvvv </div></td>
</tr>
<tr>
<td style="height:79px;"">
</td>
</tr>
<tr>
<td style="height:100%;"> </td>
</tr>
<tr>
<td style="height:21px;"> </td>
</tr>
</table>
The question is, can I add those divs inside the table TD element without adding div-display-table and div-display-row before? It seems like the best way to go for me is to mix tables and divs. What would then be the correct way of mixing them? Because TDs in Tables will not respect the height and width neither so I must use both tables and divs seems like ...
Like this:
<div class="container">
<div class="row">
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
</div>
<div class="row">
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
</div>
</div>
Mixing in table elements with divs is eventually going to give you a headache, especially when it comes to responsive design. For this same issue, I use Bootstrap CSS. They have a grid system that is extremely effective in replacing table-style layouts and adapting to mobile devices. Your target HTML is actually really close to the markup that Bootstrap uses, so your head is obviously int he right place!
After downloading the Bootstrap js and css, I would do something like this:
<!-- the container-fluid class creates a full-width container -->
<div class="container-fluid">
<!-- the row class creates a row broken into 12 columns. -->
<div class="row">
<!-- specify how many columns an element should take up out of 12 for each given device. below is the markup for 3 evenly-spaced columns for a medium (desktop) device -->
<div class="col-md-4">Column 1</div>
<div class="col-md-4">Column 2</div>
<div class="col-md-4">Column 3</div>
</div>
<div class="row">
<div class="col-md-4">Column 1</div>
<div class="col-md-4">Column 2</div>
<div class="col-md-4">Column 3</div>
</div>
</div>
I tried to understand what you are wanting but it wasn't very clear...so this is what I assumed you meant.
DEMO
CSS:
.container {
display:table;
height:200px;
border:1px solid red;
}
.column {
display:inline-block;
}
table {
border: 1px solid black;
}
td {
width:150px;
border: 2px solid blue;
}
HTML:
<table style="width:100%;height:100%;border:0;border-spacing:0px;border-collapse:collapse;">
<tr>
<td style="height:21px;">
<div class="container">
<div class="row">
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
</div>
<div class="row">
<div class="column">Column 1</div>
<div class="column">Column 2</div>
<div class="column">Column 3</div>
</div>
</div>
</td>
</tr>
<tr>
<td style="height:79px;"">
</td>
</tr>
<tr>
<td style="height:100%;"> </td>
</tr>
<tr>
<td style="height:21px;"> </td>
</tr>
</table>

What is the analog of colspan when use display: table; in CSS?

The task is pretty strange. I have to create html table BUT I'm not allowed to use traditional <table> tag. My table should look like this:
It would be easy to do it like below:
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="5"></td>
</tr>
...
but, as I said, I'm not allowed to use traditional table tags (table, tr, td, th). Here is JSFIddle of what I have at the moment. How can I get the same result as with <td colspan="5"></td> but using only divs and CSS.
EDITS:
* Table cell's width in one row must not be fixed, it should be dynamic and it should be possible to make them (cells) different width (in one row).
* Table cell's width in different rows of the same column must be equal. Like in traditional table. Only "colspanned" cell's width must be different.
As stated in CSS 2.1 specification in part "17.5 Visual layout of table contents"
Cells may span several rows or columns. (Although CSS 2.1 does not define how the number of spanned rows or columns is determined ...
So the answer is easy! Don't think of CSS tables exactly the same as HTML tables. As there are some differences like what mentioned in "17.2.1 Anonymous table objects":
... the "missing" elements must be assumed in order for the table model to work. Any table element will automatically generate necessary anonymous table objects around itself, consisting of at least three nested objects corresponding to a 'table'/'inline-table' element, a 'table-row' element, and a 'table-cell' element. ...
So you can do it this way (each row as a table and dropped table-row for avoiding unnecessary div block) until they specify a way for defining number of spanned rows or columns:
CSS
.row {
display: table;
width: 100%;
}
.cell {
display: table-cell;
width: 16.67%;
}
.wideCell {
display: table-cell;
}
HTML
<div class="row">
<div class="cell">One</div>
<div class="cell">Two</div>
<div class="cell">Three</div>
<div class="cell">Four</div>
<div class="cell">Five</div>
<div class="wideCell">Six</div>
</div>
<div>One Two Three Four Five Six</div>
<div class="row">
<div class="cell">One</div>
<div class="cell">Two</div>
<div class="cell">Three</div>
<div class="cell">Four</div>
<div class="cell">Five</div>
<div class="wideCell">Six</div>
</div>
You need to use CSS float and width to get the table-like effect you're looking for. What I'm basically doing is I'm placing 5 divs all with a fixed width and class name, and floating them to the left. The wideCell has the same width the .wrapper which just holds them all together in a nice block.
HTML
<div class="wrapper">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="wideCell"></div>
</div>
CSS
.wrapper {
width:510px;
}
.cell {
width:100px;
height:50px;
background-color:#ff0000;
float:left;
border:1px #000 solid;
}
.wideCell {
width:508px;
height:50px;
background-color:#ff0000;
float:left;
border:1px #000 solid;
}
DEMO
EDIT
CSS
.table{
width: 100%;
}
.table .row{
width: 100%;
height: 25px;
margin: 0px;
padding: 0px;
}
.table .row .cell{
width: 150px;
float: left;
border: solid 1px #CCC;
height: 25px;
}
.table .clear_float{
clear: both;
}
.table .row .cell.rowspan{
width: 759px;
border: solid 1px #CCC;
}
html
<div class="table">
<div class="row">
<div class="cell">One</div>
<div class="cell">Two</div>
<div class="cell">Three</div>
<div class="cell">Four</div>
<div class="cell">Five</div>
</div>
<div class="clear_float" />
<div class="row">
<div class="cell rowspan">
One Two Three Four Five
</div>
</div>
<div class="clear_float" />
<div class="row">
<div class="cell">One</div>
<div class="cell">Two</div>
<div class="cell">Three</div>
<div class="cell">Four</div>
<div class="cell">Five</div>
</div>
<div class="clear_float" />
<div class="row">
<div class="cell rowspan">
One Two Three Four Five
</div>
</div>
<div class="clear_float" />
<div class="row">
<div class="cell">One</div>
<div class="cell">Two</div>
<div class="cell">Three</div>
<div class="cell">Four</div>
<div class="cell">Five</div>
</div>
<div class="clear_float" />
<div class="row">
<div class="cell rowspan">
One Two Three Four Five
</div>
</div>
</div>
I have searched a lot there is nothing like colspan in display:table CSS.
You can try this: CSS display:table
<div style="display:table;">
<div style="display:table-row;">
<span style="display:table-cell;">Name</span>
<span style="display:table-cell;"><input type="text"/></span>
</div>
<div style="display:table-row;">
<span style="display:table-cell;">E-Mail</span>
<span style="display:table-cell;"><input type="text"/></span>
</div>
<div style="display:table-row;">
<span style="display:table-cell;">Password</span>
<span style="display:table-cell;"><input type="text"/></span>
</div>
</div>
<div style="display:table;">
<div style="display:table-row; " >
<span style="display:table-cell;">
<button>Send Message</button>
</span>
</div>
</div>
You can do this ( where data-x has the appropriate display:xxxx set ):
<!-- TH -->
<div data-tr>
<div data-th style="width:25%">TH</div>
<div data-th style="width:50%">
<div data-table style="width:100%">
<div data-tr>
<div data-th style="width:25%">TH</div>
<div data-th style="width:25%">TH</div>
<div data-th style="width:25%">TH</div>
<div data-th style="width:25%">TH</div>
</div>
</div>
</div>
<div data-th style="width:25%">TH</div>
</div>
<!-- TD -->
<div data-tr>
<div data-td style="width:25%">TD</div>
<div data-th style="width:50%">
<div data-table style="width:100%">
<div data-tr>
<div data-td style="width:25%">TD</div>
<div data-td style="width:25%">TD</div>
<div data-td style="width:25%">TD</div>
<div data-td style="width:25%">TD</div>
</div>
<div data-tr>
...
</div>
</div>
</div>
<div data-td style="width:25%">TD</div>
</div>
Check this fiddle out i hope that would suffice what you need
html tables
CSS
div.table {border: 1px solid black; display: table; }
div.tr {border: 1px solid black; display: table-row; }
div.td {border: 1px solid black; display: table-cell; }
Html
<div class="table">
<div class="tr">
<div class="td">Row 1, Cell 1</div>
<div class="td">Row 1, Cell 2</div>
<div class="td">Row 1, Cell 3</div>
</div>
<div class="tr">
<div class="td">Row 2, Cell 1</div>
<div class="td">Row 2, Cell 2</div>
<div class="td">Row 2, Cell 3</div>
</div>

Create table layout using <div/> with width & height as percentages

How to create table layout in HTML only by using by passing both width & height parameters as percentages, not pixels so that it works as the same in all the browsers ?Also pls suggest some good material or link where I can find the format for required attributes & their values used to accomplish this task.Early replies are appreciated.
Check this http://jsfiddle.net/nalaka526/hUFh4/6/
CSS
.divTable
{
width: 50%;
height: 50%;
display: table;
}
.divTableRow
{
width: 100%;
height: 100%;
display: table-row;
}
.divTableCell
{
width: 25%;
height: 100%;
display: table-cell;
}
HTML
<div class="divTable">
<div class="divTableRow">
<div class="divTableCell">
H1
</div>
<div class="divTableCell ">
H2
</div>
<div class="divTableCell ">
H3
</div>
<div class="divTableCell ">
H4
</div>
</div>
<div class="divTableRow">
<div class="divTableCell">
a
</div>
<div class="divTableCell ">
b
</div>
<div class="divTableCell ">
c
</div>
<div class="divTableCell ">
d
</div>
</div>
</div>
I'm not entirely sure what you're asking. Do you just want a fluid <table> layout? Below is a very basic example of a fluid layout. Keep in mind, the table will only be as wide as the container it's in.
<table width="100%" height="100%">
<tr width="25%" height="100%">
<td></td>
</tr>
<tr width="75%" height="100%">
<td></td>
</tr>
</table>