HTML - Table -vertical position text within cell - html

I would like to have text within a table cell to be positioned a little bit higher, near the top. Please take a look at the provided screenshot. You can see in the TD below Column 1 how I want it to look like:
https://jsfiddle.net/38f1aru6/1/
HTML:
<table class="table_text">
<tbody>
<tr>
<td colspan="4">
<center>
<b>Title</b>
</center>
</td>
</tr>
<tr>
<td>
<center>
<b>Column1</b>
</center>
</td>
<td>
<center>
<b>Column2</b>
</center>
</td>
<td>
<center>
<b>Column3</b>
</center>
</td>
<td>
<center>
<b>Column4</b>
</center>
</td>
</tr>
<tr>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>lirum</td>
<td>larum</td>
</tr>
</tbody>
</table>
CSS:
.td_content
{
}
.yellow_marked
{
background-color: yellow;
}
table
{
border: 1px solid black;
}
td
{
border: 1px solid;
}
I'm new to HTML and CSS... and a little bit shocked how complicated some simple styling can be.
My first guess was text alignment within a cell... vertical-align? Didn't work.
Then I tried to use an element with absolute positioning within an element with relative positioning. It did work ... almost ... but the text did flow out of the cell.
I would be very grateful if you could show me the right direction to solve this (easy?) task.
Edit:
I want to keep the yellow bar in the same position, but I want to move the text within the bar upward.

It seems from comments that you want to keep the yellow bar in the same position, but you want to move the text in it upward. That is not hard, but it does involve adding extra markup; otherwise it's impossible to separate the text from the background in that way. So, here you go.
.yellow_marked {
background-color: yellow;
}
table {
border: 1px solid black;
}
td, th {
border: 1px solid;
}
/* added css */
caption {
font-weight: bold
}
.yellow_marked>span {
position: relative;
top: -2px;
}
<table class="table_text">
<caption>
Title
</caption>
<tbody>
<tr>
<th>
Column1
</th>
<th>
Column2
</th>
<th>
Column3
</th>
<th>
Column4
</th>
</tr>
<tr>
<td>
<span class="td_content yellow_marked"><span>Cell content here</span></span>
</td>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>lirum</td>
<td>larum</td>
</tr>
</tbody>
</table>
Also, I'm sorry for changing your HTML, but you were using non-semantic markup for the title and the column headers, not to mention obsolete and unnecessary elements; couldn't bear to leave those in.
If you want, you can use the new CSS with the old HTML though.

you can use vertical-align:super on the span inside ( on .td-content )
see snippet below or jsFiddle
let me know if this is what you want
.td_content
{
vertical-align:super;
}
.yellow_marked
{
background-color: yellow;
}
table
{
border: 1px solid black;
}
td
{
border: 1px solid;
}
<table class="table_text">
<tbody>
<tr>
<td colspan="4">
<center>
<b>Title</b>
</center>
</td>
</tr>
<tr>
<td>
<center>
<b>Column1</b>
</center>
</td>
<td>
<center>
<b>Column2</b>
</center>
</td>
<td>
<center>
<b>Column3</b>
</center>
</td>
<td>
<center>
<b>Column4</b>
</center>
</td>
</tr>
<tr>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>lirum</td>
<td>larum</td>
</tr>
</tbody>
</table>

vertical-align:top works.
.td_content
{
}
.yellow_marked
{
background-color: yellow;
}
table
{
border: 1px solid black;
}
td
{
border: 1px solid;
height: 50px;
vertical-align: top; /* To move the text to the top */
padding-top: 5px; /* To create a gap at the top */
}
<table class="table_text">
<tbody>
<tr>
<td colspan="4">
<center>
<b>Title</b>
</center>
</td>
</tr>
<tr>
<td>
<center>
<b>Column1</b>
</center>
</td>
<td>
<center>
<b>Column2</b>
</center>
</td>
<td>
<center>
<b>Column3</b>
</center>
</td>
<td>
<center>
<b>Column4</b>
</center>
</td>
</tr>
<tr>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>lirum</td>
<td>larum</td>
</tr>
</tbody>
</table>

You could add the yellow background to the td instead. And then use a negative margin on the span element.
fiddle
.td_content {}
.yellow_marked {
background-color: yellow;
}
.yellow_marked span {
display: block;
margin-top: -4px;
}
table {
border: 1px solid black;
}
td {
border: 1px solid;
}
<table class="table_text">
<tbody>
<tr>
<td colspan="4">
<center>
<b>Title</b>
</center>
</td>
</tr>
<tr>
<td>
<center>
<b>Column1</b>
</center>
</td>
<td>
<center>
<b>Column2</b>
</center>
</td>
<td>
<center>
<b>Column3</b>
</center>
</td>
<td>
<center>
<b>Column4</b>
</center>
</td>
</tr>
<tr>
<td class="yellow_marked">
<span class="td_content">Cell content here</span>
</td>
<td>
<span class="td_content yellow_marked">Cell content here</span>
</td>
<td>lirum</td>
<td>larum</td>
</tr>
</tbody>
</table>

Related

Is it possible to control which cell's border prevail?

Here are four 2x2 tables, each having a single cell with a dark border. I'd like the dark border to stand out but unfortunately the adjoining light borders cover parts of the dark one in somewhat random ways. Is there a way to force a given cell's border not to be covered by adjoining ones?
I thought of using z-index, but unfortunately it doesn't work.
table {
border-collapse: collapse;
margin:auto;
}
td {
padding: 10px;
}
td.dark {
border: 8px solid black;
}
td.light {
border: 8px solid lightgrey;
}
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="dark">b</td> </tr>
</table>
<br>
<table>
<tr> <td class="dark"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<br>
<table>
<tr> <td class="light"> a </td> <td class="dark">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<br>
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="dark"> a </td> <td class="light">b</td> </tr>
</table>
The border-collapse mechanism gets in the way with this. The easiest solution is use absolute postioned ::before or ::after with either a border or a box-shadow to create the border.
The snippet shows both solutions with ::after.
Beware:
Because of the border-collapse, class .dark will still need a border, but its color is irrelevant as it will be overlayed with our custom border
Either ::before or ::after will overlay the content of the table cell making it hard (but not impossible) for the user to select the content.
snippet
body { display: flex; flex-flow: row wrap; gap: 2rem; justify-content: center }
table { border-collapse: collapse; margin:auto }
td { padding: 10px }
/* still need .dark border to trigger border collapse */
td.dark { border: 8px solid black } /* color is irrelevant */
td.light { border: 8px solid lightgrey }
/* Solutions */
/* ::before/::after will overlay the table cell, this may be unwanted behavior */
td:is(.s1,.s2)::after { background-color: CornSilk } /* to show overlay of ::after */
/* Just disable this rule, remove it or use 'transparent' as color value */
:is(.s1,.s2).dark { position: relative } /* new stacking context */
:is(.s1,.s2).dark::after { position: absolute; content: '' } /* relative to .dark */
/* Solution 1, using 'border' */
td.dark.s1::after {
inset: -8px; /* shorthand for top: -8px; right: -8px; bottom: -8px; left: -8px; */
border: 8px solid black
}
/* Solution 2, using 'box-shadow' */
td.dark.s2::after {
inset: 0; /* shorthand for top: 0; right: 0; bottom: 0; left: 0; */
box-shadow: 0 0 0 8px black
}
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="dark">b</td> </tr>
</table>
<table>
<tr> <td class="dark"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<table>
<tr> <td class="light"> a </td> <td class="dark s1">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="dark s2"> a </td> <td class="light">b</td> </tr>
</table>
See if this helps:
According to CSS guidelines this is a defined behavior of border-collapse.
If collapsing borders have the same style and width, but differ in
color, then from most to least preferred: cell, row, row group,
column, column group, table.
If come from same type of element, such as two rows then color is
taken from borders that are the topmost and the leftmost.
table {
margin: auto;
}
td {
padding: 10px;
}
td.dark {
border: 8px solid black;
}
td.light {
border: 8px solid lightgrey;
}
<table>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
<tr>
<td class="light"> a </td>
<td class="dark">b</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="dark"> a </td>
<td class="light">b</td>
</tr>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="light"> a </td>
<td class="dark">b</td>
</tr>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
<tr>
<td class="dark"> a </td>
<td class="light">b</td>
</tr>
</table>
OR you can have your own <td> styling for each row <td> element.

Indenting cells in HTML-tables

What I'm trying to achieve is the following table (which I created using Word):
Each cell basically acts as if it has two columns within it, the text-characters on the left and the digits on the right. How can I do this in HTML? The closest I have gotten is a normal table with empty cells acting as the space between the text and the digits, but that's not what I want. What's a cleaner way to do this?
If I'm understanding your intent correctly, see example below using a default auto layout and sharing a width between the wide columns to allow the skinny ones to only consume space necessary (unless you want to give them a fixed width of sorts). Though in the future a reproducible example of your effort is appreciated.
table {
table-layout: auto;
border-collapse: collapse;
border-bottom: #000 1px solid;
width: 100%;
}
th { text-align: left }
th, td {
padding: 0 .5rem;
}
table th:nth-child(odd) {
width: 50%;
border: #0f0 1px dashed;
}
table th:nth-child(2), table td:nth-child(2) {
border-right: #000 1px solid;
}
table tr:first-child {
border-bottom: #000 1px solid;
}
table tr:last-child {
border-top: #000 1px solid;
}
<table>
<tr>
<th>
wide column
</th>
<th>
skinny
</th>
<th>
wide column
</th>
<th>
skinny
</th>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
<tr>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
<td>
Stuff
</td>
</tr>
</table>

HTML table only dash in the middle

I would like to make a dash in the middle of a table.
The first picture shows my current state and the second picture shows how I would like it.
How do I get the line?
<table>
<tbody>
<tr>
<td>
<img src="https://thunder.cdn.overdrive.com/logos/crushed/1211.png?1" alt="Logo" />
</td>
<td>
<strong><font color=darkgrey>Tel.:</font></strong> <font color=grey>+44(0) XXX</font>
<br/>
<strong><font color=darkgrey>E-Mail:</font></strong> <font color=grey>xxx#xxx.com</font>
<br/>
<strong><font color=darkgrey>Web:</font></strong> <font color=grey>www.xxx.com</font>
</td>
</tr>
</tbody>
</table>
You just need to use border-left property to create a dotted grey line:
border-left: 2px dotted grey;
You will notice that that goes right on the text, so the next step is to add padding-left to move the text away from the dotted line.
Working example:
#add {
border-left: 2px dotted grey;
padding-left: 14px;
}
<table>
<tbody>
<tr>
<td>
<img src="https://thunder.cdn.overdrive.com/logos/crushed/1211.png?1" alt="Logo" />
</td>
<td id="add">
<strong><font color=darkgrey>Tel.:</font></strong> <font color=grey>+44(0) XXX</font>
<br/>
<strong><font color=darkgrey>E-Mail:</font></strong> <font color=grey>xxx#xxx.com</font>
<br/>
<strong><font color=darkgrey>Web:</font></strong> <font color=grey>www.xxx.com</font>
</td>
</tr>
</tbody>
</table>
You can do this inline as well:
<td style="border-left: 2px dotted grey; padding-left: 14px;">
It is also important to create an id as well and not just use td element or you will get this result:
td {
border-left: 2px dotted grey;
padding-left: 14px;
}
<table>
<tbody>
<tr>
<td>
<img src="https://thunder.cdn.overdrive.com/logos/crushed/1211.png?1" alt="Logo" />
</td>
<td>
<strong><font color=darkgrey>Tel.:</font></strong> <font color=grey>+44(0) XXX</font>
<br/>
<strong><font color=darkgrey>E-Mail:</font></strong> <font color=grey>xxx#xxx.com</font>
<br/>
<strong><font color=darkgrey>Web:</font></strong> <font color=grey>www.xxx.com</font>
</td>
</tr>
</tbody>
</table>
(two dotted lines will appear)

How to make a tag with dynamic content full height inside of td tag

I have a table where I load dynamic content, and I want that the content inside the cells is the same height as the cells! Precisely I have "a" elements with some text, inside the td cells. I get a problem when the text in a cell is long and goes at newline, and like you can see in the image the other "a" elements in the table row don't fit the full height of the cell.
See the live Demo
Here is the code
HTML
<table class="table" cellspacing="0">
<thead>
<tr>
<th>Data</th>
<th>Titolo</th>
<th>Sede</th>
<th>Città</th>
</tr>
</thead>
<tbody>
<tr>
<td>12/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>asd
</td>
<td>ada
</td>
<td>asdas
</td>
</tr>
<tr>
<td>04/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>ada
</td>
<td>prova
</td>
<td>asda
</td>
</tr>
<tr>
<td>07/09/2017
</td>
<td><a href="eventi.php?id=12">Evento Lignano <br>
sabbiadoro</a>
</td>
<td>Palazzetto dello Sport
</td>
<td>Perugia
</td>
</tr>
<tr>
<td>09/09/2015
</td>
<td>aaa
</td>
<td>aaa
</td>
<td>aaa
</td>
</tr>
<tr>
<td>09/03/2015
</td>
<td>sfsd
</td>
<td>ada
</td>
<td>dadasd
</td>
</tr>
</tbody>
CSS
table{
width:400px;
}
td a{
background:#ff0;
display: block;
padding:20px;
box-sizing:border-box;
height: 100%;
width: 100%;
}
td{
height:100%;
position: relative;
}
table {
width: 400px;
}
td a {
background: #ff0;
display: block;
padding: 20px;
box-sizing: border-box;
height: 100%;
width: 100%;
}
td {
height: 100%;
position: relative;
}
<table class="table" cellspacing="0">
<thead>
<tr>
<th>Data</th>
<th>Titolo</th>
<th>Sede</th>
<th>Città</th>
</tr>
</thead>
<tbody>
<tr>
<td>12/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>asd
</td>
<td>ada
</td>
<td>asdas
</td>
</tr>
<tr>
<td>04/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>ada
</td>
<td>prova
</td>
<td>asda
</td>
</tr>
<tr>
<td>07/09/2017
</td>
<td><a href="eventi.php?id=12">Evento Lignano <br>
sabbiadoro</a>
</td>
<td>Palazzetto dello Sport
</td>
<td>Perugia
</td>
</tr>
<tr>
<td>09/09/2015
</td>
<td>aaa
</td>
<td>aaa
</td>
<td>aaa
</td>
</tr>
<tr>
<td>09/03/2015
</td>
<td>sfsd
</td>
<td>ada
</td>
<td>dadasd
</td>
</tr>
</tbody>
</table>
Here is a little hack I've used in the past.
You can essentially add a pseudo element to the a element and absolutely position it relative to the closest td element. Since the pseudo elements are essentially children elements of the a element, when you click the pseudo element, you are inadvertently clicking the a element.
Updated Example
td a:after, td a:before {
content: '';
position: absolute;
top: 0; right: 0;
bottom: 0; left: 0;
}
td a:after {
background: #ff0;
z-index: -1;
}
You must define an height of your TD, otherwise the browser doesn't know the 100% of what is to be considered:
td {
height:100px;
position: relative;
}
https://jsfiddle.net/wg6paumw/4/

How do I place a number of tables on one line?

I have a <div> with a number of <tables> in it, how do I get these <tables> on one line?
<html>
<head>
<title>no-line-break</title>
<style type="text/css">
#foo
{
width: 500px;
border: 1px solid #000;
overflow-x: auto;
}
.bar
{
float: left;
width: 150px;
margin: 5px;
}
.bar th
{
background-color: #c0c0c0;
}
</style>
</head>
<body>
<div id="foo">
<table class="bar">
<tr>
<th>
Foo #1
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
</table>
<table class="bar">
<tr>
<th>
Foo #2
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
<tr>
<td>
Bar #2
</td>
</tr>
<tr>
<td>
Bar #3
</td>
</tr>
</table>
<table class="bar">
<tr>
<th>
Foo #3
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
</table>
<table class="bar">
<tr>
<th>
Foo #4
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
</table>
<div style="clear: both;"> </div>
</div>
</body>
</html>
You will need to add another div inside #foo that is at least as wide as the sum of all tables with their margins and paddings so that they won´t wrap.
example:
<html>
<head>
<title>no-line-break</title>
<style type="text/css">
#foo
{
width: 500px;
border: 1px solid #000;
overflow-x: auto;
}
#foo_inner
{
width: 650px;
}
.bar
{
float: left;
width: 150px;
margin: 5px;
}
.bar th
{
background-color: #c0c0c0;
}
</style>
</head>
<body>
<div id="foo">
<div id="foo_inner">
<table class="bar">
<tr>
<th>
Foo #1
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
</table>
<table class="bar">
<tr>
<th>
Foo #2
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
<tr>
<td>
Bar #2
</td>
</tr>
<tr>
<td>
Bar #3
</td>
</tr>
</table>
<table class="bar">
<tr>
<th>
Foo #3
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
</table>
<table class="bar">
<tr>
<th>
Foo #4
</th>
</tr>
<tr>
<td>
Bar #1
</td>
</tr>
</table>
<div style="clear: both;"> </div>
</div>
</div>
</body>
</html>
Perhaps a traditional table layout may be what you're going for.
http://jsfiddle.net/T8pfZ/2
Make all of them display:inline. So, do this:
.bar
{
float: left;
width: 150px;
margin: 5px;
display: inline;
}
#foo
{
width: 500px;
border: 1px solid #000;
overflow-x: auto;
display: inline;
}
Try adding "display:inline;" to your Foo CSS block. You may have to adjust your width.
If I understand you, you cant all the tables in the same line, so you need to put the display:inline in the #foo style.
#foo {
display:inline
}