I have code similar to this situation:
table {
table-layout: auto;
background-color: tomato;
border: 1 solid black;
}
.table-wrapper {
display: inline-block;
}
.tabs {
background-color: #00bcd4;
}
<div>
<div class="tabs">
smth
</div>
<div class="table-wrapper">
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Sample</th>
<th>Sample</th>
<th>Sample</th>
</tr>
<tr>
<td>Jill31222222</td>
<td>Smith12333333333333</td>
<td>5031231231231232</td>
<td>Jill31231231231</td>
<td>Smith312312312312</td>
<td>50312312312312312</td>
</tr>
<tr>
<td>Eve12312312312</td>
<td>Jackson1233123123123123312</td>
<td>94312312312312</td>
<td>Jill312312312312</td>
<td>Smith312312312</td>
<td>5031233123123123</td>
</tr>
</table>
</div>
</div>
I have a table with table-layot: auto style, which cells display very large text. Above table are displayed tabs. After horizontal scrolling tabs div is cut.
Is there a possibility to stretch tabs div, depending on table's width?
Relevant image:
A div (or any element with display:block) is only as wide as its container.
So one solution is to put it in a container that is as wide as the table. For instance, an inline-block around the table, which will stretch itself to the correct width.
.div-and-table-wrapper {
display:inline-block;
min-width:100%;
}
table {
table-layout: auto;
background-color: tomato;
border: 1 solid black;
}
.table-wrapper {
display: inline-block;
}
.tabs {
background-color: #00bcd4;
}
<div class="div-and-table-wrapper">
<div class="tabs">
smth
</div>
<div class="table-wrapper">
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Sample</th>
<th>Sample</th>
<th>Sample</th>
</tr>
<tr>
<td>Jill31222222</td>
<td>Smith12333333333333</td>
<td>5031231231231232</td>
<td>Jill31231231231</td>
<td>Smith312312312312</td>
<td>50312312312312312</td>
</tr>
<tr>
<td>Eve12312312312</td>
<td>Jackson1233123123123123312</td>
<td>94312312312312</td>
<td>Jill312312312312</td>
<td>Smith312312312</td>
<td>5031233123123123</td>
</tr>
</table>
</div>
</div>
(Note: the min-width is in there to ensure that the tabs div will be at least as wide as the window in case the table is narrower.
If you don't want that, i.e. if you want the tabs div to always be the same width as the table, perhaps you're better off turning it into a caption.)
You need to set the width of the parent div to fit-content:
table {
table-layout: auto;
background-color: tomato;
border: 1 solid black;
}
.table-wrapper {
display: inline-block;
}
.tabs {
background-color: #00bcd4;
}
.tabs-wrapper {
width: fit-content;
}
<div class="tabs-wrapper">
<div class="tabs">
smth
</div>
<div class="table-wrapper">
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Sample</th>
<th>Sample</th>
<th>Sample</th>
</tr>
<tr>
<td>Jill31222222</td>
<td>Smith12333333333333</td>
<td>5031231231231232</td>
<td>Jill31231231231</td>
<td>Smith312312312312</td>
<td>50312312312312312</td>
</tr>
<tr>
<td>Eve12312312312</td>
<td>Jackson1233123123123123312</td>
<td>94312312312312</td>
<td>Jill312312312312</td>
<td>Smith312312312</td>
<td>5031233123123123</td>
</tr>
</table>
</div>
</div>
An alternative would be, as Mr Lister already suggested, using inline-block instead of fit-content.
Related
What I Want
With pure HTML and CSS, to have a table where each cell either:
has a border
contains an element that is the same size as the full cell
such that there are no visible gaps between cells.
Visually, I have the left but need the right
What I've Tried
I've tried playing around with setting various combinations of box-sizing and padding to no avail.
You can see one of my attempts at this JSFiddle.
You have added padding:0 to just the coloured cells. As the first cell still have padding, it is increasing all the row height.
If you add:
.rows-index {
border: 1px solid black;
padding: 0;
box-sizing: border-box;
}
You will notice there's still a white 1px line which is not white, It is the space the border of the first cell is taking (as before, increasing the row hight). It does not matter if use border-box in this table. All cells will have same height but the coloured divs won't fill that gap.
I would suggest to use outline insteed of border as a solution:
table {
border-collapse: collapse;
border-spacing: 0;
}
.cols-index, .rows-index {
outline: 1px solid black;
padding: 0;
box-sizing: border-box;
}
.table-cell {
padding: 0;
box-sizing: border-box;
}
.table-cell div {
width: 100%;
height: 100%;
display: inline-block;
box-sizing: inherit;
}
.purple { background-color: purple; }
.red { background-color: red; }
.lightblue { background-color: lightblue; }
<table xmlns="http://www.w3.org/1999/html">
<thead>
<tr>
<th class="top-left-cell"></th>
<th class="cols-index">Bish</th>
<th class="cols-index">Bash</th>
<th class="cols-index">Bosh</th>
</tr>
</thead>
<tbody>
<tr>
<td class="rows-index">First</td>
<td class="table-cell"><div class="purple">A</div></td>
<td class="table-cell"><div class="purple">B</div></td>
<td class="table-cell"><div class="purple">C</div></td>
<tr>
<tr>
<td class="rows-index">Second</td>
<td class="table-cell"><div class="red">D</div></td>
<td class="table-cell"><div class="red">E</div></td>
<td class="table-cell"><div class="red">F</div></td>
<tr>
<tr>
<td class="rows-index">Third</td>
<td class="table-cell"><div class="lightblue">G</div></td>
<td class="table-cell"><div class="lightblue">H</div></td>
<td class="table-cell"><div class="lightblue">I</div></td>
<tr>
</tbody>
</table>
Edited: your height:100%; in the coloured divs is not doing anything as the parent does not have a fixed height. If you don't want to use outline you could set the height of this divs to 19px which is the height of the first cell... the cell that set the height of your rows. However this "solution" is ugly as hell and won't work if anytime any cell has 2 lines of text.
Edited... again: or third option, using border would be to set a line-height to your coloured divs. This is much cleaner than the previous edited paragraph and it will work as a kind of padding: https://jsfiddle.net/654gu2sf/
You can 'extend' the cells' coloring a little with padding and border to fill up the blank spaces (which can be seen to correspond to the borders in the first column).
An adjusment has to be made in the first row in the table body which is not given a top 'extension'.
table * {
padding: 0;
margin: 0;
box-sizing: border-box;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
.cols-index,
.rows-index {
border: 1px solid black;
}
.table-cell {
padding: 0;
box-sizing: border-box;
}
.table-cell div {
width: 100%;
height: 100%;
display: inline-block;
box-sizing: inherit;
}
.table-cell div {
padding: 1px;
background-color: var(--col);
border-top: var(--col) 1px solid;
border-bottom: var(--col) 1px solid;
}
.purple {
--col: purple;
}
.red {
--col: red
}
.lightblue {
--col: lightblue;
}
tbody tr:first-child td>div {
border-top-width: 0px;
}
td {
text-align: center;
}
.rows-index {
text-align: right;
}
<table xmlns="http://www.w3.org/1999/html">
<thead>
<tr>
<th class="top-left-cell"></th>
<th class="cols-index">Bish</th>
<th class="cols-index">Bash</th>
<th class="cols-index">Bosh</th>
</tr>
</thead>
<tbody>
<tr>
<td class="rows-index">First</td>
<td class="table-cell">
<div class="purple">A</div>
</td>
<td class="table-cell">
<div class="purple">B</div>
</td>
<td class="table-cell">
<div class="purple">C</div>
</td>
</tr>
<tr>
<td class="rows-index">Second</td>
<td class="table-cell">
<div class="red">D</div>
</td>
<td class="table-cell">
<div class="red">E</div>
</td>
<td class="table-cell">
<div class="red">F</div>
</td>
</tr>
<tr>
<td class="rows-index">Third</td>
<td class="table-cell">
<div class="lightblue">G</div>
</td>
<td class="table-cell">
<div class="lightblue">H</div>
</td>
<td class="table-cell">
<div class="lightblue">I</div>
</td>
</tr>
</tbody>
</table>
You can add the cellspacing attribute to to table tag to adjust spacing between cells.
To remove the spacing, set cellspacing=0.
td, th {
border: 1px solid black;
}
<table cellspacing=0>
<tr>
<th></th>
<th>bish</th>
<th>bash</th>
<th>bosh</th>
</tr>
<tr>
<td>first</td>
<td>A</td>
<td>A</td>
<td>A</td>
</tr>
<tr>
<td>second</td>
<td>A</td>
<td>A</td>
<td>A</td>
</tr>
<tr>
<td>third</td>
<td>A</td>
<td>A</td>
<td>A</td>
</tr>
</table>
This question already has answers here:
HTML -- two tables side by side [duplicate]
(4 answers)
Closed 6 years ago.
This is how the display currently looks:
This is how I want it to look:
.d1{
background:#F0F0F0;
border: 1px solid #A4A4A4;
}
#designs input, #itemz input{
height:19px;
font-size: 15px;
}
#designs #fds_image {
background-size: 190px 221px;
height: 221px;
width: 190px;
overflow:hidden;
}
#designs #fds_image img{
width: 190px;
}
<table id="designs" width="auto" align="center" border="0" bgcolor="#EBEBEB" cellspacing="5">
<tbody>
<tr>
<td class="d1" name="item">
<div id="fds_image">
<button class="preview_switch">M</button>
</div>
<hr>
<div class="bottom_bar">
<button name="preview" data-original="m">Preview</button>
<br>
<tbody>
<td class="d1" name="item">
<div id="fds_image">
<button class="preview_switch">M</button>
</div>
</div>
<hr>
<div class="bottom_bar">
<button name="preview" data-original="m">Preview</button>
I tried to do many things and researched online, but for some reason, it is not working. How can I make the tables display side-by-side, like I've shown?
You can override the default display: table to inline-table.
table {
display: inline-table;
}
Example of horizontally align multiple <table> elements.
body {
text-align: center;
}
table {
width: 100px;
height: 200px;
border-collapse: collapse;
display: inline-table;
}
td {
border: 1px solid grey;
}
tr:first-child {
height: 100%;
}
<table>
<tbody>
<tr>
<td>Content</td>
</tr>
<tr>
<td><button>Button</button></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Content</td>
</tr>
<tr>
<td><button>Button</button></td>
</tr>
</tbody>
</table>
You could try floating the tables.
table {
float: left;
}
You can make a bigger table that holds both of your tables currently side by side.
<table style="margin: auto;">
<tbody>
<tr>
<td><!-- Your first table --></td>
<td><!-- Your second table --></td>
</tr>
</tbody>
</table>
I am trying to have a triangle/arrow at the right of arrow-td. Initial plot with the code below works but the triangle/arrow doesn't scroll with its container arrow-td.
How could I keep the triangle positioned relative to arrow-td even when the user scrolls through main-div?
NOTE: The arrow should stay outside (just right) of main-div. Adding position: relative to arrow-td won't work as that would force arrow to be inside of main-div since overflow-y: auto is activated on main-div.
Check out the plunker
HTML
<div class="main-div">
<table>
<tbody>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td class="arrow-td">
<div class="left-of-arrow">With arrow</div>
<div class="arrow"></div>
</td>
</tr>
.........
</tbody>
</table>
</div>
CSS
.main-div{
height: 200px;
width: 200px;
overflow-y: auto;
}
table{
width: 100%;
max-width: 100%;
}
td{
width: 100%;
background-color: #eee;
display: flex;
}
td>div{
display: flex;
}
.arrow{
right: 300px;
position: absolute;
width: 0;
height: 0;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
border-right: 12px solid red;
}
Isn't possible to your arrow track your td without adding a position relative and changing you main-div width to 100% and setting a size to your table, like I did on that plunker
See my Plunker
To do what you wan't you'll need to add a JavaScript function to track the offset of that td every time you scroll the page, and setting the top property of your arrow.
EDIT:
I wasn't in home yesterday, so, I couldn't write a code to solve your issue. I saw your code and there is some issues on your approach. I can't comment there because I don't have karma. But I did some comments on that fork, explaining why those approaches aren't that good.
Updated Plunker
You have your .arrow set with position absolute, meaning it will not be relative to any scrolling you do unless you explicitly specify it to be.
So, by adding position: relative to your .main-div, you will achieve the arrow moving relative to the .arrow-td.
Where your design fails in look and feel is on the .main-div itself. It has a fixed width of 200px. This may be intentional but for your purposes of having the arrow sit outside the table, it will just not be possible using the current HTML structure you have.
One thing you can do is play with how overflow works for your type of problem.
I decided to take a swing at implementing a solution which is what you see below... however the final implementation is truly up to you! I also put in comments a second version of how this could look, in the CSS named Example #2 give it a try :-)
.wrapper {
overflow-y: auto;
width: 335px; /* Example #2: change this to: 100% for a different visual effect */
}
.main-div {
height: 200px;
width: 100%; /* Example #2: change this to 200px for a different visual effect */
}
table {
width: 100%;
max-width: 100%;
}
td {
width: 100%;
background-color: #eee;
display: flex;
}
td > div {
display: flex;
}
.arrow {
left: 300px;
position: absolute;
width: 0;
height: 0;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
border-right: 12px solid red;
}
.arrow-td {
position: relative;
}
<div class="wrapper">
<div class="main-div">
<table>
<tbody>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td class="arrow-td">
<div class="left-of-arrow">With arrow</div>
<div class="arrow"></div>
</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
</tr>
</tbody>
</table>
</div>
</div>
This is how I solved the problem through javascript:
I simply track arrow-td's top position, if it is changed .arrow div's top position should also be modified accordingly.
Check out the PLUNKER
Code:
$(document).ready(function(){
setInterval(function(){
var heightLimit = $(".main-div").height();
var parentTop = $(".arrow").parent().position().top;
var arrowTop = $(".arrow").position().top;
//Change arrow top position only if parent's position has been changed
if(arrowTop != parentTop){
if(parentTop < 0 || parentTop > heightLimit -25){
$(".arrow").css("visibility","hidden");
}
else{
$(".arrow").css("visibility","visible");
$(".arrow").css("top",parentTop);
}
}
},500);
});
I want to have a fixed width for my editable table, but I also wanting to set different width for each TD.
In my attempt I am able to get the table set at a fixed width, but this causes the width of the TDs appear to be 50% instead of the 80% - 20% I had before setting the fixed width
CSS
table {
margin: 15px 0;
border: 1px solid black;
table-layout: fixed;
width: 100%;
}
.fixed td:nth-of-type(1) {width:20%;}
.fixed td:nth-of-type(2) {width:80%; text-align: left;}
.fixed {
margin:0px;padding:0px;
width:100%;
border:1px solid #000; }
.fixed td {
margin:0px;padding:0px;
width:100%;
border:1px solid #000; }
HTML
<div class="fixed" contenteditable="true">
<table>
<tbody>
<tr>
<td colspan="2">Header:</td>
</tr>
<tr>
<td>Name:</td>
<td><br/></td>
</tr>
<tr>
<td>DOB::</td>
<td><br/></td>
</tr>
<tr>
<td>Comments:</td>
<td><br/></td>
</tr>
</table>
What am I missing? Check this Fiddle if it will help. Try it out by typing enough to see it automatically goes to the next line after a certain point.
The problem with your code is that your first <tr> is having colspan="2". So when you give a width:100% to all the TDs of the table, the css won't get applied to the underlying TDs as you want.
Your solution is to separate the Header td: <td colspan="2">Header:</td> into a separate table (Refer HTML-1 below)
or
put the underlying TDs in the same TR as that of the header (Refer HTML-2 below).
Also change the CSS and simplify it like I did below. you have written a lot of unnecessary CSS.
Working Fiddle Here
Here's what I tried. try this:
HTML-1:
<table class="fixed" >
<tbody>
<tr>
<td colspan="2">Header:</td>
</tr>
</tbody>
</table>
<table class="fixed" >
<tbody>
<tr>
<td>Name:</td>
<td>test</td>
</tr>
<tr>
<td>DOB::</td>
<td>tes</td>
</tr>
<tr>
<td>Comments:</td>
<td>test</td>
</tr>
</table>
HTML-2:
<table class="fixed" >
<tbody>
<tr>
<td colspan="2">Header:</td>
<tr>
<td>Name:</td>
<td>test</td>
</tr>
<tr>
<td>DOB::</td>
<td>tes</td>
</tr>
<tr>
<td>Comments:</td>
<td>test</td>
</tr>
</tr>
</tbody>
</table>
Simplified CSS:
table {
margin: 0 0;
width: 100%;
table-layout: fixed;
}
.fixed td:nth-of-type(1) {width:80%;}
.fixed td:nth-of-type(2) {width:20%; text-align: left;}
.fixed td {
margin:0px;padding:0px;
border:1px solid #000; }
You have Errors in your html syntax although that is nothing to do with the problem.
See if you need something like this fiddle.
table {
margin: 15px 0;
border: 1px solid black;
width: 100%;
}
.fixed td:nth-of-type(1) {width:20%;}
.fixed td:nth-of-type(2) {width:80%; text-align: left;}
.fixed {
margin:0px;padding:0px;
width:100%;
border:1px solid #000; }
.fixed td {
margin:0px;padding:0px;
width:100%;
border:1px solid #000; }
<div class="fixed" contenteditable="true">
<table>
<tbody>
<tr>
<td colspan="2">Header:</td>
</tr>
<tr>
<td>Name:</td>
<td><br/></td>
</tr>
<tr>
<td>DOB::</td>
<td><br/></td>
</tr>
<tr>
<td>Comments:</td>
<td><br/></td>
</tr>
</tbody>
</table></div>
otherwise you wont be able to achieve variable td width as all the td will have same width in a column.
you can use colspan attribute for a workaround.
Firstly, this is a duplicate question: Equal-height scaling cells in an html table
The user that asked the question above didn't get a solution for the same issue I'm having.
Within the JSFIDDLE you will notice a cell with a red background. This cell is the highest and I need all other cells to pick up the highest cell height and span to the corresponding cell height.
The solution cannot contain fixed heights as this must be dynamic
Here is a mock up of what I'm trying to achieve: http://jsfiddle.net/pAz6G/
Here is HTML:
<table class="left" cellspacing="0" borderspacing="0" >
<tr>
<td>
<h2>Very Servere</h2>
<p>50m from high water on East Coast, 100m from high water on West Coast. Characterised by:</p>
<ul>
<li>Heavy salt deposits</li>
<li>The almost constant smell of saly spray in the air.</li>
<li>Close to breaking stuff (typically starts 50-100 metres) such as is found on exposed coasts.</li>
</ul>
<p>This environment may be extended inland by revailing winds and local coniditions</p>
</td>
</tr>
</table>
<table cellspacing="0" borderspacing="0" class="right">
<tr>
<td class="section">
<span class="section-heading">Applications</span>
<table class="inner">
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
</table>
</td>
<td class="section">
<span class="section-heading">Applications</span>
<table class="inner">
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
</table>
</td>
<td class="section">
<span class="section-heading">Applications</span>
<table class="inner">
<tr>
<td>Roofing</td>
</tr>
<tr>
<td class="red">Rain washing plus manual washing every 3 months</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
</table>
</td>
</tr>
</table>
Here is CSS:
/* Column Styling */
.left {
float: left;
width: 350px;
}
.left td {
padding: 10px;
}
.right {
float: left;
width: 400px;
}
/*********************************************/
/* General Styling */
.no-padding {
padding: 0;
}
td {
background: grey;
color: white;
vertical-align: top;
}
p {
margin: 0;
}
.red {
background: red;
}
/*********************************************/
/* Section Styling */
.section {
border-left: 1px solid #fff;
}
.section-heading {
display: block;
padding: 10px;
}
/*********************************************/
/* Nested Tables */
.inner {
width: 100%;
}
.inner td {
border-top: 1px solid #fff;
padding: 10px;
}
/*********************************************/
Instead of using multiple tables, try using one table.
Keeping it simple :)