How could I force span to be bottom of table td?
I have this code:
<td>
<span class='cat-otsikko'>Product title</span>
<span class='cat-hinta'>Product price</span>
</td>
And I would like to force cat-hinta to be always bottom of the td. How could I get this done?
I need it because I want product price to always be bottom of the table, so it does not depend of product title (it may be 1-3 lines long).
EDIT:
Fancy´s code did not work. Here is the whole code I have: (tuotteet is class of the table)
.tuotteet tr{
border:1px dashed gray;
}
.tuotteet td {
width:30%;
border-right:dashed 1px gray;
border-bottom:dashed 1px gray;
padding:5px 2px 5px 2px;
border-top:dashed 1px gray;
text-align:center;
position: absolute;
}
.cat-otsikko {
font-weight:bold;
font-size:101%;
}
.cat-hinta {
font-size:108%;
color:#ED1C24;
font-weight:bold;
display:block;
bottom:0px;
position: absolute;
bottom: 0;
}
Edit 2
I tried Jeaffrey´s code but it seemed too complex for me.
So, how I could embed the mu is too short user code in this script:
<table border="0" class="tuotteet">
<?php
$productsPerRow = 3;
for($i=0;$i<$count;$i++){
if($i % $productsPerRow == 0) {
print"<tr>";
}
$tuote = $prods[$i];
print"
<td>
<span class='cat-otsikko'>".buildLink('prod', $tuote['id'])."</span>
<div class='product-alaosa'>
<span class='cat-hinta'>".price($tuote['hinta'])."</span>
<span class='cat-kori'><a class='button green medium' href='".buildUrl("addtocart", $id)."'>Lisää ostoskoriin</a></span>
</div>
</td>
\n";
if($i % $productsPerRow == $productsPerRow - 1) {
print"</tr>";
}
}
?>
The CSS vertical positioning model is, um, rather limited so you have to kludge around it.
You can't use absolute positioning because that removes elements from the normal document flow and you'll end up with overlapping elements unless you can specify how tall everything is.
One thing you can do is to split your single cell into two cells and then add rowspan="2" to the rest of the cells in that row:
<tr>
<td class="top">
<span class='cat-otsikko'>Product title</span>
</td>
<td rowspan="2">
Short
</td>
</tr>
<tr>
<td class="bottom">
<span class='cat-hinta'>Product price</span>
</td>
</tr>
And then use vertical alignment on the two table cells:
td.top {
vertical-align: top;
border: 1px solid red;
}
td.bottom {
vertical-align: bottom;
border: 1px solid green;
}
And a demo: http://jsfiddle.net/ambiguous/Es6Tn/
This is ugly but it will work and non-trivial vertical alignment in CSS is pretty much always ugly unless you want to pixel-position everything.
Are you trying to bottom align, for example, the buttons of you products? You must have fixed width and height of table cell to achieve this.
http://jsfiddle.net/JeaffreyGilbert/EW6Ax/
Related
My problem is that I can't find a way to fix the table row height,
if the username exceeded it overlaps to the other column.
check the last two row
and it's also scrollable at side and the username is still in their position.
code for single row:
<tr>
<td class="headcol">
<div class="innerHead">
<div class="user-id" style="display:none;">18993</div>
<input type="checkbox" class="checkboxes" name="user_select[]">
TestingalksdjaskldjsalkdjalskdjaksduqwoieuoqweuowqeiTesting#gmail.com </div>
</td>
<td class="forcedWidthUserCode">Tested091237871</td>
<td class="textAlignCenter">Field staff</td>
<td class="forcedWidth">Testing</td>
<td class="forcedWidth">Tested</td>
<td> N/A </td>
<td class="textAlignCenter">Active</td>
<td> N/A </td>
<td class="forcedWidth"> N/A </td>
<td> N/A </td>
<!--<td>N/A</td>-->
</tr>
CSS:
.headcol {
position: absolute;
width: 18em;
border-right: 2px solid #fff;
background-color: #fff;
z-index: 0;
}
table tr td {
/* background: #fff; */
padding: 6px;
text-align: left;
vertical-align: middle;
border-bottom: 1px solid #ddd;
}
how can I align and wrap the text base on the width of the username column?
It happens because there is a white space between those nodes (the checkbox and the text node). The line breaks at white space.
There are two way to handle this.
As mentioned by #Supraja Ganji: Use word-break.
table tr td {
word-break: break-all;
}
or prevent the whole line from breaking, and hide anything that overflows:
table tr td {
white-space: nowrap;
overflow: hidden;
}
Your username is too long and doesnot contain any space, so it is not wrapping.
for td give word-break: break-all
table tr td {
/* background: #fff; */
padding: 6px;
text-align: left;
vertical-align: middle;
border-bottom: 1px solid #ddd;
word-break: break-all;
}
Using a div inside td is a very bad idea.
Using a div instide a td is not worse than any other way of using tables for layout. (Some people never use tables for layout though, and I happen to be one of them.)
If you use a div in a td you will however get in a situation where it might be hard to predict how the elements will be sized. The default for a div is to determine its width from its parent, and the default for a table cell is to determine its size depending on the size of its content.
The rules for how a div should be sized is well defined in the standards, but the rules for how a td should be sized is not as well defined, so different browsers use slightly different algorithms.
Let me know if you require any further help
.headcol {
position: relative;
width: 18em;
border-right: 2px solid #fff;
background-color: #fff;
z-index: 0;
}
.innerHead {
word-break: break-all;
overflow: hidden;
}
<table>
<tbody><tr>
<td class="headcol">
<div class="innerHead">
<div class="user-id" style="display:none;">18993</div>
<input type="checkbox" class="checkboxes" name="user_select[]">
TestingalksdjaskldjsalkdjalskdjaksduqwoieuoqweuowqeiTesting#gmail.com </div>
</td>
<td class="forcedWidthUserCode">Tested091237871</td>
<td class="textAlignCenter">Field staff</td>
<td class="forcedWidth">Testing</td>
<td class="forcedWidth">Tested</td>
<td> N/A </td>
<td class="textAlignCenter">Active</td>
<td> N/A </td>
<td class="forcedWidth"> N/A </td>
<td> N/A </td>
<!--<td>N/A</td>-->
</tr>
</tbody></table>
If we want to achieve the following, i.e. style the border in such a way that the border will have the thick right bottom corner and upon hover show a plus sign.
What I have done: I have done selection of cells in tables. Normal borders. Even tried border styling, but most of which is rounded or cut-off borders, which is not of use here.
What I am trying to do: I am trying to mimic series fill functionality of excel in html table. Functionality is not a problem. I am just trying to get the styling of these borders right so that it is easier to comprehend.
So, How can we customize the border to get the desired effect only using css and html?
select.js
$(function () {
$('td').click(function () {
$(this).toggleClass('highlight');
});
});
table.html
<table border="1" cellpadding="15">
<tbody>
<tr>
<td>23</td>
<td>57</td>
<td>62</td>
</tr>
</tbody>
</table>
table.css
.highlight {
border-radius: 0px 0px 5px 5px;
border: 2px solid black;
}
Edit
I went back and added a different behavior to each cell after looking at ther image I believe I understand now See updated Snippet.
It has been brought to my attention that you probably don't want every cell that way (I tend to go into automatic coding, like a robot 🤖.) I modified it so that the last cell will have the div.pad.
Did you mean a square on every cell or just the table? If the latter, let me know, it's an easy adjustment. I used
position: relative and absolute
z-index
border-collapse: separate
border-spacing, border, and outline
Pseudo-elements ::before and ::after
2 unicode entities \1f4c4 📄 and \2795 ➕
and the pseudo-class :hover of course.
Added tr:last-of-type td:last-of-type to single out the last cell.
It looks bulky because there's no dimensions mentioned, but that's fine since the bulkiness highlights the styles and how they interact. Each square is a div that is a child of a cell (<td>). Making the cells relative whilst the divs are absolute allows us to give it coordinates in relation to the <td> edges. Once positioned in the bottom right corner, we give the div some dynamic content that appears on :hover. When :hovered upon, the pseudo-elements ::before and ::after appear. They to are positioned, but with a relative value because we want to move the fonts of ::before and ::after relative to their original position, rather than to another positioned element (like we did previously with each<td> and div.
Special note on the div.pad's styling.
position:absolute allowed us to easily pick the bottom right corner. If bottom:0 and right:0 puts .pad snugly into the corner, then we can continue going forward with negative length values in order for .pad to sit halfway in and halfway out of cell/table borders.
Added outline:2px solid white instead of border because unlike border, outline width doesn't displace other elements in the layout. The white color is to blend into the background giving the appearance of .pad being more of a separate yet related component of the table.
z-index:1 was also given to .pad so that the white outline is clearly defined from the table borders.
The other main points are:
The borders were made so that they were defined as separate properties but appeared as one border (like border-collapse: collapse;) To avoid that disconnected look the border-collapse:separate gives, we use outline to fill in that border-spacing of 1px; if we were to use only borders, the table as a whole would increase noticeably in size. The border and outline styles are inset and the last cell has an over sized outline style outset designating it as highlighted.
SNIPPET
table {
border-collapse: separate;
border-spacing: 1px;
border: 1px inset black;
outline: 1px inset black;
table-layout: fixed;
width: 80vw;
min-height: 150px;
}
td {
border:1px solid black;
outline: 1px inset black;
}
td:hover {
border: -3px inset black;
outline: 6px outset black;
position: relative;
z-index:1;
padding:1px;
}
.pad {
background: black;
cursor: pointer;
position: absolute;
height: 1px;
width: 1px;
z-index: 1;
bottom: -1px;
right: -1px;
}
td:hover .pad,
.pad:hover {
border: 4px solid black;
bottom: -9px;
right: -9px;
outline: 2px solid white;
z-index:2;
padding:2px;
}
.pad:hover::before {
content: '\1f4c4';
position: relative;
top: 20px;
left: 10px;
font-size: 1.2rem;
z-index:2;
}
.pad:hover::after {
content: '\2795';
position: relative;
top: 16px;
left: 24px;
font-size: .7rem;
z-index:2;
}
<table>
<tbody>
<tr>
<td>
<div class='pad'></div>
</td>
<td>
<div class='pad'></div>
</td>
<td>
<div class='pad'></div>
</td>
</tr>
<tr>
<td>
<div class='pad'></div>
</td>
<td>
<div class='pad'></div>
</td>
<td>
<div class='pad'></div>
</td>
</tr>
<tr>
<td>
<div class='pad'></div>
</td>
<td>
<div class='pad'></div>
</td>
<td>
<div class='pad'></div>
</td>
</tr>
</tbody>
</table>
I need help with some CSS styling.
I have made a table with 2 rows, and 2 columns, but the first column in the first row has a rowspan of 2. That creates a table like this: http://i.imgur.com/UjdSwu5.png, which is fine.
My problem is that when I try to apply padding to the 'name' and 'id' cells (but not the image cell), only the name cell gets padded. Here is a screenshot of no padding: http://i.imgur.com/0CGVhDL.png, and here is a screenshot of when I try to pad both cells: http://i.imgur.com/ipvHv2M.png
HTML:
<div id="body">
<div>
<div>
<div>
<div id="content">
<div id="items">
<ul class="list">
<li>
<table border="0">
<tr>
<td rowspan="2"><a href="index.html"><img
src="images/Stone.png" alt="Image" height="50" width="50"></a>
</td>
<td>
<h3 class="name">Stone</h3>
</td>
</tr>
<tr>
<td>
<p class="id">1</p>
</td>
</tr>
</table>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
CSS:
#content td h3 {
padding:5px 1px 5px 30px;
}
#content td p {
padding:5px 1px 5px 30px;
}
If I do the following then the id cell gets padded how i want it to, but it also pads the img cell.
#content td {
padding:5px 1px 5px 30px;
}
What am i doing wrong?
you are simply applying padding to the wrong element.
you are applying a padding to the h3 and p elements inside #content td but what you really want to do is apply the padding to the cell which is td.
In order to that properly, you need to identify your cells, like this:
<td class="name">
<h3>Stone</h3>
</td>
and
<td class="id">
<p>1</p>
</td>
and the CSS should be something like this
#content td.name {
padding:5px 1px 5px 30px;
}
#content td.id {
padding:5px 1px 5px 30px;
}
Also, a good practice would be not to name a class as id that could be very confusing afterwards.
I would advise calling it item-id instead, for example.
<td class="item-id">
<p>1</p>
</td>
#content td.item-id {
padding:5px 1px 5px 30px;
}
First of all, I suggest using the class-name of the elements, to style them.
Here's the css which should do what you want:
.name, .id{
padding: 80px;
}
In this fiddle, you can see a working solution: http://jsfiddle.net/63eUh/
As Kevin Smouts already said, you applied the padding to the wrong Element - which can easly happen, when you are adressing Elements in this way - it's difficult to read.
Whenever you change your HTML-structure, you have to care about css and update it as well. So I really don't recommend putting all your html-tree inside css to reach the correct elements.
I have a table of data and each cell is a link. I want to allow the user to click anywhere in the table cell and have them follow the link. Sometimes the table cells are more than one line but not always. I use td a {display: block} to get the link to cover most of the cell. When there is one cell in a row that is two lines and the others are only one line the one liners don't fill the entire vertical space of the table row. Here is the sample HTML and you can see it in action here http://www.jsfiddle.net/RXHuE/:
<head>
<style type="text/css">
td {width: 200px}
td a {display: block; height:100%; width:100%;}
td a:hover {background-color: yellow;}
</style>
<title></title>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com/">Cell 1<br>
second line</a>
</td>
<td>
Cell 2
</td>
<td>
Cell 3
</td>
<td>
Cell 4
</td>
</tr>
</tbody>
</table>
</body>
Set an arbitrarily large negative margin and equal padding on the block element and overflow hidden on the parent.
td {
overflow: hidden;
}
td a {
display: block;
margin: -10em;
padding: 10em;
}
http://jsfiddle.net/RXHuE/213/
You need a small change in your CSS. Making td height:100%; works for IE 8 and FF 3.6, but it doesn't work for Chrome.
td {
width: 200px;
border: solid 1px green;
height: 100%
}
td a {
display: block;
height:100%;
width:100%;
}
But making height to 50px works for Chrome in addition to IE and FF
td {
width: 200px;
border: solid 1px green;
height: 50px
}
td a {
display: block;
height:100%;
width:100%;
}
Edit:
You have given the solution yourself in another post here; which is to use display: inline-block;.
This works when combined with my solution for Chrome, FF3.6, IE8
td {
width: 200px;
border: solid 1px green;
height: 100%}
td a {
display: inline-block;
height:100%;
width:100%;
}
Update
The following code is working for me in IE8, FF3.6 and chrome.
CSS
td {
width: 200px;
border: solid 1px green;
height: 100%;
}
td a {
display: inline-block;
height:100%;
width:100%;
}
td a:hover {
background-color: yellow;
}
HTML
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com/">Cell 1<br>
second line</a>
</td>
<td>
Cell 2
</td>
<td>
Cell 3
</td>
<td>
Cell 4
</td>
</tr>
</tbody>
</table>
The example lays here
Little late to the party, but there's a nice solution I just discovered.
You can use a combination of relative and absolute positioned elements, along with a pseudo element to get the effect you're looking for. No extra markup needed!
Change the table cell (<td>), to be position: relative;, and create a ::before or ::after pseudo element on the <a> tag, and set it to position: absolute;, and also use top: 0; left: 0; right: 0; bottom: 0;.
Because the pseudo element is attached to the anchor tag, and you're telling it to take up the entire table cell, it will force the anchor tag to be at least that size, whilst not affecting the actual content of the anchor tag itself (thereby retaining its vertically centered alignment).
For example
table {
border-collapse: collapse;
table-layout: fixed;
}
td {
position: relative;
width: 200px;
padding: 0.5em 1em;
border: 2px solid red;
background-color: lime;
}
td a {
/* FONT STYLES HERE */
text-decoration: none;
}
td a::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 0;
}
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com/">Cell 1<br>
second line</a>
</td>
<td>
Cell 2
</td>
<td>
Cell 3
</td>
<td>
Cell 4
</td>
</tr>
<tr>
<td>
Cell 5
</td>
<td>
<a href="http://www.google.com/">Cell 6<br>
second line</a>
</td>
</tr>
</tbody>
</table>
Hope this helps!
Following hack works [Tested on Chrome / Firefox / Safari]
Have the same padding for td and anchor elements. And for anchor also have margin which is equal to -ve of padding value.
HTML
<table>
<tr>
<td><a>Hello</a></td>
</tr>
</table>
CSS:
td {
background-color: yellow;
padding: 10px;
}
a {
cursor:pointer;
display:block;
padding: 10px;
margin: -10px;
}
Working Fiddle :http://jsfiddle.net/JasYz/
Try display: block:
td a {display: block; height:100%;}
[EDIT] WTF ... I can confirm this doesn't work in FF 4 and Chrome. This works:
td a {display: block; height: 2.5em; border: 1px solid red;}
That suggests that height:100%; isn't defined in a table cell. Maybe this is because the cell gets its size from the content (so the content can't say "tell me your size" because that would lead to a loop). It doesn't even work if you set a height for the cells like so:
td {width: 200px; height: 3em; padding: 0px}
Again the code above will fail. So my suggestion is to use a defined height for the links (you can omit the width; that is 100% by default for block elements).
[EDIT2] I've clicked through a hundred examples at http://www.cssplay.co.uk/menus/ but none of them mix single line and multi-line cells. Seems like you hit a blind spot.
I will post the same answer here, as I did on my own question.
Inspired by Jannis M's answer, I did the following:
$(document).ready(function(){
$('table tr').each(function(){
var $row = $(this);
var height = $row.height();
$row.find('a').css('height', height).append(' ');
});
});
I added a since empty links (not containing text nodes) can not be styled(?).
See my updated fiddle.
Only problem here is that using display: block forces the browser to ignore the vertical align: center...
oops.
I jury rigged it to look right for one cell with height:60 and a font that occupied 20 pixels by adding a br... Then I realized that I had some items with 2-line text. Dang.
I ended up using the javascript. The javascript doesn't give the nice mousey pointy clicker thing, but the line of text does, so it will actually trigger a visual response, just not where I want it to... Then the Javascript will catch all the clicks that 'miss' the actual href.
Maybe not the most elegant solution, but it works well enough for now.
Now if I could only figure out how to do this the right way....
Any ideas on how to add the mouse icon change to a hand for the area covered by the onclick? Right now, the click to page works, but the icon only changes when it hits the href which only affects the text.
Why don't you just get rid of the <a> altogheter and add an onClick to the <td> directly?
<head>
<style type="text/css">
td {
text-align:center;
}
td:hover {
cursor:pointer;
color:#F00;
}
</style>
<title></title>
</head>
<body>
<table>
<tbody>
<tr>
<td onclick="location.href='http://www.google.com/';">Cell 1<br />second line</td>
<td onclick="location.href='http://www.google.com/';">Cell 2</a></td>
<td onclick="location.href='http://www.google.com/';">Cell 3</td>
<td onclick="location.href='www.google.com';">Cell 4</td>
</tr>
</tbody>
</table>
This way you cut out the middle man.
PS: i know this was asked and answered many years ago, but none of the answers above solved the problem in my case. Hope this helps someone.
For me the only solution is to replace <table> <tr> with <div>s and style them using display:table and display:table-row accordingly.
Then you can replace <td> with just <a> and style it with display:table-cell.
Work perfectly even on varying heights of <td> contents.
so original html without anchors:
<table>
<tr>
<td>content1<br>another_line</td>
<td>content2</td>
</tr>
</table>
now becomes:
a:hover
{
background-color:#ccc;
}
<div style="display:table; width:100%">
<div style="display:table-row">
content1<br>another_line
content2
</div>
</div>
I have used this solution: works better then the rest in my case.
CSS:
.blocktd {width: 100%; height: 100%; padding: 0px; overflow: hidden}
a.blocktd {margin: 0em; padding: 50px 20px 50px 20px; display: block;}
a.blocktd:hover {border: 4px solid #70AEE8; border-radius: 10px; padding: 46px 16px 46px 16px; transition: 0.2s;}
And in HTML: ...
I'm trying to achieve table similar to this using css/html only. Is it possible ?
So the white area is the places table. This is the HTML for the table :
<table class="places">
<tr>
<td class="solid">K</td>
<td> </td>
<td> </td>
<td class="solid">P</td>
<td> </td>
</tr>
<tr>
<td class="solid">25</td>
<td class="solid">26</td>
<td> </td>
<td class="solid">47</td>
<td class="solid">48</td>
</tr>
(...)
</table>
And my css :
.places{
position:relative;
background:white;
width:160px;
margin:0 auto;
text-align:left;
padding:5px;
border-collapse: collapse;
}
.places tr {
}
.places td {
width:22px;
height:22px;
text-align:center;
}
.solid {
border: 1px solid #d2cdd1;
border-top:none;
background-color:#e7e7e7;
text-align:center;
cursor:pointer;
}
I was pretty sure, that although tables are a bit different than other html objects, padding should work here. But it looks that I was wrong. Cellspacing/cellpading have no effect. Currently I was able to get something looking like this :
You need the border-spacing property.
Table cells are not like other elements, because while div and p gets are block level elements, and span and input are inline, table cells and rows get their own table-cell and table-row display values.
Using border-spacing with border-collapse: separate will give you what you'd need. Have a look: http://jsfiddle.net/kjag3/1/
PS. I've also taken the liberty of cleaning up the HTML by separating them into two tables, so you won't need the fillers for the empty cells.
The reason you can't set any spacing between the cells is that you have border-collapse set to collapse in the styles for your table. If you use border-collapse:separate instead, you should be able to add margins to your table cells and put spacing between them.
Using border-collapse:collapse makes it so that adjacent table cells use the same border; naturally, you wouldn't be able to put space between two elements when they're attached to each other.
I wonder whether a table structure is appropriate for what you're trying to achieve?
To me, it looks like the 'K' and 'P' are headings, and the gap between the 'K' and 'P' numbers suggests that 'K' and 'P' are separate and shouldn't be part of the same table. So I suggest getting rid of the table and restructuring your HTML to use simple headings and div tags like this:
HTML:
<div class="places">
<h2>K</h2>
<div>25</div>
<div>26</div>
<div>23</div>
<div>24</div>
<div>21</div>
<div>22</div>
</div>
<div class="places">
<h2>P</h2>
<div>47</div>
<div>48</div>
<div>45</div>
<div>46</div>
<div>43</div>
<div>44</div>
</div>
CSS:
.places {
width: 55px;
float: left;
margin: 0 25px 0 0;
}
.places h2, .places div {
width: 22px;
height: 22px;
margin: 0 3px 3px 0;
border: 1px solid #d2cdd1;
border-top:none;
background-color:#e7e7e7;
text-align:center;
cursor:pointer;
font-weight: normal;
font-size: 12pt;
}
.places div {
float: left;
}