I want to do the following with div construction:
<table border="1">
<tr>
<td>Field 1</td><td><input type="text"></td>
</tr>
<tr>
<td>Field 2 longest</td><td><input type="text"></td>
</tr>
<tr>
<td>Field 3 long</td><td><input type="text"></td>
</tr>
</table>
http://jsfiddle.net/6AvMm/
the main problem is, how to do the first column as width as the longest (field 2) ? You know, tables are only for tabulary datas - and this case is clearly a layout.
using display:table display:table-row; AND display:table-cell;
Updated fiddle
HTML:
<div class="holder">
<div class="row">
<div class="cell">Field 1</div>
<div class="cell"><input type="text" /></div>
</div>
<div class="row">
<div class="cell">Field 2</div>
<div class="cell"><input type="text" /></div>
</div><div class="row">
<div class="cell">Field 3</div>
<div class="cell"><input type="text" /></div>
</div>
</div>
CSS:
.holder{
display:table;
border:1px solid #000;
border-width:1px 1px 0 0;
}
.row{display:table-row;}
.cell{
display:table-cell;
border:1px solid #000;
border-width:0 0 1px 1px;
}
DEMO
This would typically be done with floats. Using display: table is usually still not advised for layouts.
<div class="column">
<div class="row">
<p>Your content</p>
</div>
<div class="row">
<p>Your content (longest field)</p>
</div>
</div>
<div class="column">
<div class="row">
<p>Your content</p>
</div>
<div class="row">
<p>Your content</p>
</div>
</div>
CSS:
.column{
float: left;
}
Demo Fiddle
This provides a lot of flexibility as you can easily adjust the amount of rows separately in each column, or simply skip the whole "row" thought and just write your content with headings in the column divs. Example
Using this method, you will have a lot more control over margins and positioning (needed for layouts), compared to the table method.
It seems like you're trying to move away from tables because of the semantic reason that tables are not suitable for layout. Therefore, I think you will have problems with your layout in the future if you just use display: table-cell and the way that property functions is changed. I would recommend using something like the following CSS to abandon tables completely:
div.tr {
display:block;
border: 1px solid;
position: relative;
padding: 3px;
}
div.tr div.td {
display: inline-block;
border: 1px solid;
padding: 3px;
}
div.tr div.td:first-child {
min-width: 35%;
}
.table {
width: 40%;
padding: 3px;
border: 1px solid;
}
http://jsfiddle.net/6AvMm/6/
Related
I want to create something like this using divs and it also should be without using display:table css rule etc. How do I create table header this??
May be try this as a starting point? You might need to tweak it a lot.
* {box-sizing: border-box;}
.row {overflow: hidden; clear: both;}
.cell {border: 1px solid #999; padding: 0px; float: left;}
.cell.full {float: none;}
.col-1 {width: 20%;}
.col-2 {width: 40%;}
.col-3 {width: 60%;}
.col-33 {width: 33.3%;}
.row-2 {height: 3em;}
<div class="row">
<div class="cell col-1 row-2">Subject</div>
<div class="cell col-3 row-2">
<div class="row">
<div class="cell full">First Term</div>
</div>
<div class="row">
<div class="cell col-33">October Test</div>
<div class="cell col-33">December Exam</div>
<div class="cell col-33">Term Average</div>
</div>
</div>
<div class="col-1 row-2 cell">Teacher's Evaluation</div>
</div>
Preview:
If you can use flexbox can i use flexbox
The benefit of using flexbox is that you can get the the same result as you using the table tag including vertical-align:middle.
In the snippet, keep attention to the text-alignment in the cells.
You can use flexboxgrid like this:
[class*="col"] {
text-align:center;
border:1px solid;
display: flex;
justify-content: center;
flex-direction: column;
}
<link href="http://cdn.jsdelivr.net/flexboxgrid/6.3.0/flexboxgrid.min.css" rel="stylesheet" />
<div class="row ">
<div class="col-xs-3">Subject</div>
<div class="col-xs-6">
<div class="row">
<div class="col-xs-12">First Term</div>
</div>
<div class="row">
<div class="col-xs-4">October Test</div>
<div class="col-xs-4">December Exam</div>
<div class="col-xs-4">Term Average</div>
</div>
</div>
<div class="col-xs-3">Teacher's Evaluation</div>
</div>
The result:
http://jsbin.com/zetaro/edit?html,css,output
Just use colspan and rowspan =)
tutorial how to do it
I made it:
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
<table>
<tr>
<th rowspan="2"><br>Subject<br></th>
<th colspan="3">First term</th>
<th rowspan="2">Teacher's Evaluation</th>
</tr>
<tr>
<td>October test</td>
<td>December exam</td>
<td>Term Average</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
https://jsfiddle.net/b3aqLny1/
Try this
.table {
width: 100%;
table-layout: fixed;
display: table;
text-align: center;
}
.table .row{
display: table-row;
}
.table .row .col {
display: table-cell;
vertical-align: middle;
height: 100px;
border: 2px solid #333;
width: 33%;
}
.table .col.col-big {
width: 50%;
}
span {
display: inline-block;
padding: 10px;
}
.inner-table.table .row .col {border-width: 1px;}
<div class="table">
<div class="row">
<div class="col">
<span>Subject</span>
</div>
<div class="col col-big">
<div class="table inner-table">
<div class="row">
<div class="col one-col" style="width: 100%">
<span>First Term</span>
</div>
</div>
<div class="row">
<div class="table inner-table">
<div class="row">
<div class="col"><span>Test 1</span></div>
<div class="col"><span>Test 2</span></div>
<div class="col"><span>Test 3</span></div>
</div>
</div>
</div>
</div>
</div>
<div class="col">
<span>Teacher's Evaluation</span>
</div>
</div>
</div>
The Dompdf has a bug that it does not base on the div css to scale size which the div has over the page size. I have tried the table which the css not work for it. How can i limit the size?
Here is the code:
HTML
<style>
.table {
display: table;
width:100px;
padding:5px;
}
.row {
display: table-row;
border-bottom: 1px solid black;
width:100px;
}
.cell {
display: table-cell;
width:100px;
border: 1px solid #000000;
text-align:left;
padding:3px;
}
.cell_DB{
width:100px;
}
</style>
<div class="table cell_DB" border="0" cellpadding="3">
<div class="row cell_DB" >
<div class="cell cell_DB" >
Title
</div>
</div>
<div class="row cell_DB">
<div class="cell cell_DB">
<p class="cell_DB">
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<br/>
<br/>
</p>
</div>
</div>
<div class="row cell_DB">
<div class="cell cell_DB">
<p class="cell_DB" style="color: blue">
</p>
</div>
</div>
</div>
This is currently solution which make the display to be block.
.cell_DB{
display:block;
}
<p style="word-wrap:break-word;width:98%;">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
Try adding <div style="width:80%;"> ....CODES... </div> after </style> tag
It's been a while I try to keep away from using tables for laying out elements, as I realized that they were not meant for that and that normal container elements like div,p along with CSS are enough to achieve any layout one can need. I've been successful so far but there's one situation that I don't seem to overcome on my own. What I basically need is something that a table with the following markup would do:
<table>
<tr>
<td>Michael</td>
<td rowspan="4"><img src="Photo.png"/></td>
<td>Svenson</td>
</tr>
<tr>
<td>Steve</td>
<td>Manson</td>
</tr>
<tr>
<td>Bob</td>
<td>Sandgal</td>
</tr>
<tr>
<td>Mirko</td>
<td>Lahovic</td>
</tr>
</table>
But I don't want to use table. This image will give you better idea of what I need:
I've tried using float left and giving the middle div the total height of the four adjacent divs, but this time the second line of divs begin from the bottom line of the middle div.
To demonstrate the changes , i have used border-color on the div, the code is pretty simple and clear.
In the example below the height has been fixed to 400px
<html>
<head>
<style type="text/css">
*, *:before, *:after {
box-sizing: border-box;
}
.row{
width : 100%;
border : 1px solid #ff0000;
padding: 5px;
float:left;
}
.cont{
height :400px;
border : 1px solid #00ff00;
width:33%;
padding:10px;
float:left;
}
.small-row{
height:25%;
border: 1px solid #0000ff;
width:100%;
padding:2px;
float:left;
}
</style>
</head>
<body>
<div class="row">
<div class="cont">
<div class="small-row"></div>
<div class="small-row"></div>
<div class="small-row"></div>
<div class="small-row"></div>
</div>
<div class="cont">
<div class="large-row"></div>
</div>
<div class="cont">
<div class="small-row"></div>
<div class="small-row"></div>
<div class="small-row"></div>
<div class="small-row"></div>
</div>
</div>
</body>
</html>
I'd t ry using display:inline-block on the outer divs like so:
<div style='display:inline-block;height:100px;'>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
</div>
<div style='display:inline-block;height:100px;'>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;height:100px;"></div>
</div>
<div style='display:inline-block;height:100px;'>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
<div style="width:200px;background-color:blue;height:20px;margin: 5px 0;"></div>
</div>
No floats required :)
I know there are lots of ways to center content with an unknown width on a fluid width page in HTML/CSS but I can't get them to work in this case for some reason and need help.
Firstly, let me state that I need a solution that works in common browsers and in IE6 (don't ask why).
Here's an example of markup and the problem. In this example I want the yellow boxes centered inside the blue box.
example on jsfiddle.net
<div style="background:blue;margin:0 auto;width:100%;">
<table style="margin:0 auto;">
<tr>
<td>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<td>
</tr>
</table>
</div>
I tried this method using a table but I also tried the -50% +50% method. I am happy to use any method that works on all common browsers and IE6.
Can someone help me fix it.
Please do not lecture me on IE6 or incorrect use of the TABLE tag.
Try this,
<tr>
<td>
<div style="width: 379px;">
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
<div style="background:yellow;float:left;padding:50px;">Test</div>
</div>
</td>
</tr>
what I understood from your requirement that you want to make your div to center ? then please have a look on the below code
<style type="text/css">
.yourclass
{
background:yellow;
float:left;
padding:50px;
}
.blueback
{
background:blue;
}
.mytable
{
width: auto;
margin-left: auto;
margin-right: auto;
}
div.clear
{
clear:both;
}
</style>
<div class="blueback">
<table class="mytable">
<tr>
<td>
<div class="yourclass">Test</div>
<div class="yourclass">Test</div>
<div class="yourclass">Test</div>
<div class="clear"></div>
<div class="yourclass">Test</div>
<div class="yourclass">Test</div>
<div class="yourclass">Test</div>
</td>
</tr>
</table>
Hope it helps...
After lots of research I can find no solution to this that works in all browsers and doesn't require IE6 hacks.
The best solution is display:inline-block and IE6/7 and various other hacks (eg. FF2).
The final solution taken from here is as follows:
<style>
li {
width: 200px;
min-height: 250px;
border: 1px solid #000;
display: -moz-inline-stack;
display: inline-block;
vertical-align: top;
margin: 5px;
zoom: 1;
*display: inline;
_height: 250px;
}
</style>
<li>
<div>
<h4>This is awesome</h4>
<img src="http://farm4.static.flickr.com/3623/3279671785_d1f2e665b6_s.jpg"
alt="lobster" width="75" height="75"/>
</div>
</li>
I want to use div elements to create a table-like layout.
What I want
.tableStyle td {
background: red;
color: #fff
}
.contentBox {
min-height: 200px;
}
<table class="tableStyle" width="100%" cellspacing="10">
<tr>
<td width="25%">
<div class="contentBox">One</div>
</td>
<td width="25%">Two</td>
<td width="50%" colspan="2">Three</td>
</tr>
<tr>
<td colspan="2">
<div class="contentBox">Four</div>
</td>
<td colspan="2">Five</td>
</tr>
</table>
What I have
.table {
display: table;
width: 100%;
border-collapse: separate;
border-spacing: 10px;
color: #fff
}
.table .row {
display: table-row;
}
.table .table-cell {
display: table-cell;
background: red;
width: 50%;
}
.contentBox {
min-height: 200px;
}
.table .smlCell {
width: 25%;
}
.table .table {
border-spacing: 0;
}
<div class="table">
<div class="row">
<div class="table-cell">
<div class="table">
<div class="row">
<div class="table-cell smlCell">
<div class="contentBox">One</div>
</div>
<div class="table-cell smlCell">
Two
</div>
</div>
</div>
</div>
<div class="table-cell">Three</div>
</div>
<div class="row">
<div class="table-cell">
<div class="contentBox">Four</div>
</div>
<div class="table-cell">Five</div>
</div>
</div>
I want to have equal spacing between the cells marked "One" and "Two".
I also want all cells in a row to be of same height.
After searching on net, I know that there are some limitations or issues
for display: table such as a lack of colspan/rowspan equivalents which may help what I'm trying to accomplish.
Is there anyway (apart form <table>) to create this?
Sure there is!
Demo Fiddle
HTML
<div class='table'>
<div class='row'>
<div class='table'>
<div class='cell'>One</div>
<div class='cell'>Two</div>
</div>
<div class='cell'>Three</div>
</div>
<div class='row'>
<div class='cell'>Four</div>
<div class='cell'>Five</div>
</div>
</div>
CSS
html, body{
width:100%;
height:100%;
margin:0;
padding:0;
}
.table {
display:table;
width:100%;
height:100%;
}
.cell {
display:table-cell;
width:50%;
}
.row {
display:table-row;
}
.cell {
background:red;
color:white;
border:5px solid white;
}
Try this. I have used inline instead of CSS class. You had placed one div in wrong location.
<div class="table">
<div class="row">
<div class="table-cell">
<div class="table">
<div class="row">
<div class="table-cell smlCell">
<div style="width:50% float: left" class="contentBox">One
</div>
<div style="width:50% float: right" class="contentBox">
Two
</div>
</div>
</div>
</div>
</div>
<div class="table-cell">Three</div>
</div>
<div class="row">
<div class="table-cell">
<div class="contentBox">Four</div>
</div>
<div class="table-cell">Five</div>
</div>
</div>
with support for older (ms-explorer?). But if your content is table by it's nature, use table and not those tricks :-)
<!DOCTYPE HTML>
<html>
<head>
<title>Bla!</title>
<style type='text/css'>
/* reset sizing model ... */
* { box-sizing: border-box; -webkit-box-sizing: border-box;-moz-box-sizing: border-box; }
/* basic div definitions */
div { display:inline-block; min-height:50px; background-color:red; border:solid 3px white; }
/* 1/2 line div */
div.half { width:50%; }
/* 1/4 line div */
div.quater { width:25%; }
</style>
</head>
<body class='body' id='body' >
<div class='quater'> One </div><div class='quater'> Two </div><div class='half'> Three </div><div class='half'> Four </div><div class='half'> Five </div>
</body>
</html>