I have an HTML table, with three columns.In the second column, I added a simple style as shown below.
table {
border: 1px solid black;
}
.requiredBar {
background-color: #c00;
}
.requiredCol {
width: 1px;
max-width: 1px;
}
<table>
<tr>
<td>Name</td>
<td class="requiredCol requiredBar"> </td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Password</td>
<td class="requiredCol requiredBar"> </td>
<td><input type="text" /></td>
</tr>
</table>
The output that I am getting is like this. The red bar shown is actually a column. I am trying to reduce its height without changing the height of other cells in the same row.
Is it possible to change the height of some cell (red bar in this case) in an html table of the same row?
You can add a div inside your requiredCol and set its height
table {
border: 1px solid black;
}
.requiredBar {
background-color: #c00;
height: 10px;
width: 2px;
display: block;
}
.requiredCol {
width: 2px;
}
<table>
<tr>
<td>Name</td>
<td class="requiredCol">
<div class="requiredBar"></div>
</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Password</td>
<td class="requiredCol">
<div class="requiredBar"></div>
</td>
<td><input type="text" /></td>
</tr>
</table>
You can't change one cell height without changing row height in a table(actually that's a standard) and that leads table to change some other cells in table.
If you want to ordering some tags (like input and label in this case) its better to use CSS Grid Layout instead of table.
If you insist on using table,you can set the position of item ,which stay in cell with flexbox instead of changing exact cell.
The main idea behind the flex layout is to give the container the ability to alter its items' width/height (and order) to best fill the available space.
Related
I have
<table>
<tr>
<td style='text-align:center'>
CENTERED
</td>
</tr>
</table>
I would like to ADD a single character either on the left or on the right of CENTERED, without affecting the x-position of the CENTERED string.
To be clear: NOT on the left of the cell, but on the left of the CENTERED string. And the CENTERED string can be anything, so I do not want to calculate something pixel-perfect and move it by some constant pixel value.
I tried fiddling with position:absolute/relative and padding-left/left but I couldn't find a working solution.
Ideally if it could be build with classes:
<table>
<tr>
<td style='text-align:center'>
<span class='centeredRegardless'>CENTERED</span>
<span class='onTheLeftWithoutAffectingThePreviousOne'>*</span>
</td>
</tr>
</table>
Then I will need those classes defined :)
Thank you
If you want some cells to have the extra character and some not you could put the extra character as content in a pseudo element positioned absolutely so it doesn't change the positioning of the actual text at all.
For a left character position is right: 100% and for a character on the right the positioning is left: 100%.
td {
position: relative;
}
td.extraChar::before {
content: '*';
position: absolute;
right: 100%;
}
<table>
<tr>
<td style='text-align:center' class="extraChar">
<span class='centeredRegardless'>CENTERED</span>
</td>
</tr>
<tr>
<td style='text-align:center'>
<span class='centeredRegardless'>CENTERED</span>
</td>
</tr>
</table>
Something like this? (remove and collapse the border when happy)
td { border: 1px solid black; min-width:20px; }
.left { text-align:left;}
.right { text-align:right;}
.center { text-align:center;}
<table>
<tr>
<td class="right">></td>
<td class="center">
CENTERED
</td>
<td class="left"><</td>
</tr>
</table>
I have a table and what I want is to not make the space between the content as big. This is what the page looks like right now:
So this is some of the code for the table (I'm not posting it all because it's repetitive, all the tags are closed at the end too).
I have tried changing the padding and margin and it doesn't work. This is the CSS:
/*styling the table for the add ons*/
table,
tr,
td,
input {
margin: 30px;
padding: 10px;
margin-right: 20px;
margin-left: 45px;
line-height: 1.4em;
font-size: 17px;
}
.pushRight {
position: relative;
left: 10px;
bottom: 75px;
}
<table style="width:100%">
<tr>
<td><label><input type="checkbox" name="example" value="Value"><span><b>Voice over artist</b> €475</span>
<table>
<tr>
<td class="pushRight">If you require a voice artist for your video project<br>please select this option.</td>
</tr>
</table>
</td>
<td><label><input type="checkbox" name="example" value="Text"><span><b>Creative Concept</b> €1200</span>
<table>
<tr>
<td class="pushRight">We are bursting with ideas, so if you don't have one of<br>your own please select this option.</td>
</tr>
</table>
</td>
</div></tr>
<tr><div class="secondRow">
<td><label><input type="checkbox" name="example" value="Value"><span><b>Script</b> €850</span></label>
<table>
<tr>
<td class="pushRight">If you would like us to allocate our scriptwriter to your project<br>please select this option.</td>
</tr>
</table>
</td>
<td><label><input type="checkbox" name="example" value="Text"><span><b>Storyboarding</b> €875</span>
<table>
<tr>
<td class="pushRight">It is not essential to storyboard every video project, <br>however if you would like to include this process,<br>please select this option.</td>
</tr>
</table>
</td>
</div></tr>
First of all, you have some divs that don't do much in your html(div with class second row), when you use tables, the tr tags define the rows automatically, so you don't have to put a div to say that's my first row etc...
Using a lot of margin, and padding everywhere will make your table look ugly depending on which browser you're using, i tried to reorganize your code and just added this in your css :
td{
padding : 20px
}
table{
margin: 0 auto;
}
All you have to do now is to play with the padding to change the space as you want. you can also change the width of the table if you want a bigger table.
Here's a jsfiddle :
https://jsfiddle.net/rfroq680/2/
I had this recently, you need to use border-spacing in css.
Try this:
table{
border-collapse:separate;
border-spacing:0 6px;
}
You'll get 6px between rows with the above code.
This should help too: https://www.w3schools.com/cssref/pr_border-spacing.asp
I can’t figure this one out for the life of me. Take a table that has 2 rows, each with 2 cells. The top-right and bottom-left cells are set to colspan="2" (so the table really has 3 columns). If the non-colspan cells have a 100×100 image inside, and the colspan cells have a 200×100 image, you’d expect the table width to be something like 300, right? Nope nope nope! It’s more like 400.
See this pen: http://codepen.io/sandwich/pen/apYPdL
It seems like the colspan’d cells are greedy for width, which goes contrary to typical table sizing of shrinking the cells to fit the content.
Can anyone shine a light on this behavior? In the pen, the desired result is for the first two rows to size themselves like they do when the third row is added, but without the third row having to be added.
If the non-colspan cells have a 100×100 image inside, and the colspan cells have a 200×100 image, you’d expect the table width to be something like 300, right?
Well probably if the <table> had border-collapse:collapse but as it is separate and 20px left and right borders , that's 80px extra for 2 columns. that's 380px at minimum.
Try table-layout:fixed the default is auto. Using fixed will allow you more control of a table's behavior.
CODEPEN
SNIPPET
// Toggle visibility of 3rd row, which has no spanned cells.
$(".toggle_3rd_row").click(function() {
$(".row_3").fadeToggle();
});
// Toggle applying CSS widths to <td>s
$(".toggle_widths").click(function() {
$("table").toggleClass("defined_sizes");
})
body {
padding: 20px;
}
img {
display: block;
}
table {
width: 400px;
margin: 20px 0;
table-layout: fixed;
}
table.defined_sizes .cell_1-1 {
width: 100px;
}
table.defined_sizes .cell_1-2 {
width: 220px;
}
table.defined_sizes .cell_2-1 {
width: 220px;
}
table.defined_sizes .cell_2-2 {
width: 100px;
}
table .row_3 {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="toggle_3rd_row">Toggle third row</button>
<button class="toggle_widths">Toggle cell widths in CSS</button>
<table border="1" cellpadding="0" cellspacing="20">
<caption>Why do the cells only size properly with the colspan-less third row present??</caption>
<tr>
<td class="cell_1-1">
<img src="http://placehold.it/100x100?text=100w">
</td>
<td colspan="2" class="cell_1-2">
<img src="http://placehold.it/222x100?text=222w">
</td>
</tr>
<tr>
<td colspan="2" class="cell_2-1">
<img src="http://placehold.it/222x100?text=222w">
</td>
<td class="cell_2-2">
<img src="http://placehold.it/100x100?text=100w">
</td>
</tr>
<tr class="row_3">
<td>
<img src="http://placehold.it/100x100?text=100w">
</td>
<td>
<img src="http://placehold.it/100x100?text=100w">
</td>
<td>
<img src="http://placehold.it/100x100?text=100w">
</td>
</tr>
</table>
I need to create an HTML table with the following layout:
[Name] [Message] Date]
Where the width of [Name] should be the width of the longest name (Up to a max), [Date]should be a fixed width of 95px (And floating to the right), while [Message] should take the remaining width.
I've tried using multiple div's, but I can't get the result I need, and a table seems much simpler.
So far, the following isn't working:
<table style="width: 100%">
<tr>
<td style="width: 100%; max-width: 100px">NAME</td>
<td style="width: 100%">message</td>
<td style="width: 95px">TIME</td>
</tr>
<tr>
<td style="width: 100%; max-width: 100px">NAME OTHER</td>
<td style="width: 100%">message</td>
<td style="width: 95px">TIME</td>
</tr>
</table>
Edit 1 Seems as though this example has exactly what I need. Although I still think a table would be neater.
Edit 2 The [Message] needs to allow for multiline...
Edit 3 Here is a working sample of what I need (Exactly) based on the link in Edit 1
This cannot be done in CSS alone, due to the requirements. The first column should be flexible, which is easy (just prevent line breaks and let the column take its natural width), and setting the last column width is trivial, but telling the browser to use all the rest in the mid column (instead of expanding the first column too) cannot be done in CSS. If you set its width to 100%, things work the desired way in some browsers, but other browsers (like IE) treat it differently. You would require a width of something plus 100% plus 95px to equal 100%, which is of course impossible, and browsers handle this in different ways.
However, with a little bit of JavaScript the medicine goes down: do as outlined above, with 100%, then postprocess the table by setting the first column to a specific width in pixels (using the value allocated by the browser), remove the width: 100% setting, and set table layout to fixed—which means that the browser now has two columns width fixed width, total width set to 100%, and one column with no width set, so it is easy to it to allocate the remaining width to the mid column.
<style>
td:first-child { white-space: nowrap }
td:nth-child(2) { width: 100% }
td:nth-child(3) { width: 95px }
</style>
<table border cellspacing=0 style="width: 100%">
<tr>
<td style="">NAME</td>
<td style="">message</td>
<td style="width:95px">TIME</td>
</tr>
<tr>
<td>NAME OTHER</td>
<td>message</td>
<td>TIME</td>
</tr>
</table>
<script>
(function () {
var row = document.getElementsByTagName('tr')[0];
var cell1 = row.children[0];
cell1.style.width = cell1.clientWidth + 'px';
row.children[1].style.width = 'auto';
document.getElementsByTagName('table')[0].style.tableLayout = 'fixed';
})();
</script>
For simplicity, this code is based on the assumption that there are no other tables on the page. Modify as needed. The attributes border cellspacing=0 are there just make the cell widths more visible.
Update: This does not address the issue of setting a maximum width on the first column. That requirement is underdefined unless you specify what should happen if the width is exceeded (truncation, word wrap, wrap anywhere, wrap with hyphenation?).
try this code .
.test
{
max-width:100px;
}
<table style="text-align: center;">
<tr>
<th>NAME</th>
<th>message</th>
<th style="width: 95px">TIME</th>
</tr>
<tr>
<td class="test">NAME OTHER</td>
<td>message</td>
<td style="width: 95px">TIME</td>
</tr>
</table>
The following .css code provides the template for the attached picture:
table {
display: table;
width: 100%;
table-layout: fixed;
position: absolute;
top: 10px;
empty-cells: hide;
}
td.small:first-Child {
vertical-align: top;
width: 100px;
overflow: hidden;
text-overflow: ellipsis;
}
td.small:last-Child {
vertical-align: top;
width: 95px;
}
td.extend {
vertical-align: top;
word-wrap: break-word;
}
.userName a {
color: #9DC8FC;
}
<tr>
<td class="small userName">
<a title="Administrator" href="#">Administrator</a>
</td>
<td class="extend">
is it me you're looking for?
</td>
<td class="small">
10:14:01 AM
</td>
</tr>
I have an div container where an image is displayed, above that image I would like to show an table. So, my problem is that the table isn't displayed over the image. Instead it is shown after the image? Can somebody tell me how I can align the table above the image?
<div id="foto2">
<img src="foto2.jpg" class="bg"> <span class="oben">
<table>
<tr>
<td onclick="playAudio('test.mp3')">Hamdullah</td>
<td onclick="playAudio('test2.mp3')">Was?</td>
</tr>
<tr>
<td onclick="playAudio('test.mp3')">Penner</td>
<td onclick="playAudio('test2.mp3')">Heidefick</td>
</tr>
</table>
</span>
</div>
And my css:
html, body {
margin: 0;
width: 100%;
height: 100%;
background-color:#00061c;
}
img.bg {
width: 100%;
}
.oben {
left: 0;
width: 100%;
}
#font-face {
font-family: "Hallo";
src: url(hallo.ttf);
}
table {
width:100%;
color:#FFF;
}
tr {
text-align:center;
}
td {
font-family: Hallo;
font-size: xx-large;
}
In order to see your table "above" ( I suppose you mean in the z axis.) so the image is displayed as a background of the table you can use 2 alternatives solutions
1) a background for your foto2 div.
2) an absolute positioned table
Alternative 1 : Background for your foto2 div.
I dont think you need that oben rule with a left property for a not positioned element (which actually wont work either). The span tag as well I will keep it out of the game unless you have a very particular reason for it...mmmm
This is the fiddle
This is the css for foto2 div. You may adjust the repeat and other properties at your will
#foto2 {
background:url( your image url here );
}
this is the html
<div id="foto2">
<table>
<tr>
<td onclick="playAudio('test.mp3')">Hamdullah</td>
<td onclick="playAudio('test2.mp3')">Was?</td>
</tr>
<tr>
<td onclick="playAudio('test.mp3')">Penner</td>
<td onclick="playAudio('test2.mp3')">Heidefick</td>
</tr>
</table>
</div>
Alternative 2: Absolute positioned table
With this alternative you may use a full width image and not to be constrained by the table size. its your call which alternative to use
Your foto2 div should be relative positioned.Then....
table {
position:absolute;
top:0px;
width:100%;
color:#FFF;
}
And here the fiddle for the absolute table
Have you tried putting the image after the table, like?
</table>
<img src="foto2.jpg" class="bg" />
Or put it in the lowest row of the table like:
<tr>
<td onclick="playAudio('test.mp3')">Penner</td>
<td onclick="playAudio('test2.mp3')">Heidefick</td>
</tr>
<tr>
<td><img src="foto2.jpg" class="bg"></td>
</tr>
</table>
By the way why are you using <span> and put the <table> inside it?
Try inserting it after the closing span tag
<div id="foto2">
<span class="oben">
<table>
<tr>
<td onclick="playAudio('test.mp3')">Hamdullah</td>
<td onclick="playAudio('test2.mp3')">Was?</td>
</tr>
<tr>
<td onclick="playAudio('test.mp3')">Penner</td>
<td onclick="playAudio('test2.mp3')">Heidefick</td>
</tr>
</table>
</span>
<img src="foto2.jpg" class="bg" />
</div>