I have HTML table with 5x5 cells. When hovering on any <td> I need to change the background of a 2x2 block of 4 cells. Here is the code to change the background in the current row.
td.1:hover, td.1:hover + td.1 {
background:#eee;
}
I don't know how to change the background in the row like this:
With CSS only, you could only affect the TD's children or next sibling. I.e., you could extend the background to the TD next to the one you hover but not on another row.
To do what you want to do, you would have to use JavaScript, as you would need to walk up and down the DOM, something CSS does not allow you to do.
To do this with jQuery, for example, try something like this:
$('td').hover(function () {
var t = $(this),
index = t.index(); // the position of the TD in the row, so you can find the one below it
t.addClass('hovered'); // apply hovered class to this TD...
t.next().addClass('hovered'); // ...and to the TD next to it...
t.closest('tr').next().find('td:eq(' + index + ')').addClass('hovered').next().addClass('hovered'); // ...and to the 2 below
}, function () {
// remove the hovered class when no longer hovering
$(this).closest('table').find('td').removeClass('hovered');
});
And in your CSS give whatever styling to the 'hovered' class you want.
DEMO 1
If, however, you want to change the background of the exact same 4 TDs every time you hover over any TD in the table, this can be done purely with CSS. Just give a class name to the 4 cells you want highlighted. Let's call this class 'block2x2' for example. Then your CSS is:
table:hover .block2x2 {
background: #eee; // whatever background style you want
}
DEMO 2
Related
How can I use the style input of p-checkbox to change the border and background color of a checkbox?
I already tried [style]="{'background': '#ff0000'}". But this only applies the style to the div which holds the actual checkbox. So its useless. Instead I need to change the border-color and background of the div which has the classes p-checkbox-box and p-highlight.
Note: I cant use CSS here because the colors are dynamic and dependant on the content.
You can use renderer2 to manipulate DOM elements and then add style:
Get all checkboxes using document.getElementsByClassName('p-checkbox-box')
Iterate over each element and add the style you want using renderer2.setStyle()
try this piece of code and add it in ngAfterViewInit():
let chkboxes = document.getElementsByClassName('p-checkbox-box')
for (let index = 0; index < chkboxes.length; index++) {
const element = chkboxes[index];
this._renderer2.setStyle(element,'background-color','#bf2222');
this._renderer2.setStyle(element,'border-color','#bf2222');
}
I wanted to have one element highlight either when it gets hovered, or some other element is also hovered. Yet the code i've written to achieve this seems to override the hover pseudo-class whenever it gets run. I can't seem to see why -- minimal example in this fiddle: https://jsfiddle.net/mLynfz3x/
As soon as the second element gets hovered, the hover pseudo class for the first one is removed, and I'm not sure why. Is it intended that the jQuery .css() function override pseudo-classes? Or is the issue something else that I've missed entirely
Thank you!
The set Color for the Element Testlink doesnt disable the hover-pseudo class, the fixed color for that element is just, lets say "higher priority". So all you gotta do to fix it is add:
#testLink:hover {
color: olive !important;
}
and it should work with your existing JQuery.
This is what I did
$("#aTestItem").hover(() => {
$("#testLink").css("color", "olive");
}, () => {
$("#testLink").css("color", "black");
});
$("#testLink").hover(() => {
$("#testLink").css("color", "olive");
}, () => {
$("#testLink").css("color", "black");
});
I have a table in html with two td content in it. it is a dynamic td. I want to implement the functionality like if in each of table row, each big td value text color displayed as green and less value text display as red;
Please find my source code:
forData+='<tr><td>'+forPlant+'</td><td>'+forAsking+'</td><td>'+forProduction+'</td></tr>';
$(".forTable").append(forData);
if(forAsking<forProduction){
console.info("Less value");
//-----here I want to display Whatever value is less it displayed as red and other is green
}else{
//---same manner followed in there as well
console.info("More Asking");
}
Create 2 classes for those colors
.green{
color:green;
}
.red{
color:red;
}
Then first compare the values then append the result.
forData+='<tr><td>'+forPlant+'</td>';
if(forAsking<forProduction){
forData+='<td class="green">'+forProduction+'</td><td class="red">'+forAsking+'</td>';
}else{
forData+='<td class="green">'+forAsking+'</td><td class="red">'+forProduction+'</td>';
}
forData+= '</tr>';
$(".forTable").append(forData);
You can see the demo here
I am using html2canvas.js 0.4.1 to convert a DOM to canvas for printing. When the text has a background color on a span, and the span wraps to the next line, then the canvas version covers the whole rectangle of the 2 wrapping lines. It doesn't just cover the text. Furthermore it hides the text in the prior span.
The following fiddle demonstrates it pretty well. The 'one one' span is completely covered up by the blue background on the 'two two' span.
https://jsfiddle.net/sddah9k2/1/
css:
#content {
width:200px;
}
.select {
background-color:#3efcdb
}
html:
<div id='content'>
<span >one one one one one one </span>
<span class="select" >two two two two two two </span>
<span >three three three three three </span>
</div>
<div id="img-out"></div>
code:
html2canvas($("#content"), {
onrendered: function(canvas) {
theCanvas = canvas;
document.body.appendChild(canvas);
// Convert and download as image
Canvas2Image.saveAsPNG(canvas);
$("#img-out").append(canvas);
// Clean up
//document.body.removeChild(canvas);
}
});
My theory is html2canvas can't tell what the bounding box of the spans are, or maybe it just doesn't support multiple rectangles as bounds.
Is there any type of CSS effect I can use on the HTML text that will render correctly in html2canvas? It doesn't have to be a background color, but I have to somehow indicate that the wrapping span is highlighted.
Canvas does not support multi line inline texts. So your libraries splits it up, but the wrapping .select box gets the dimensions like an inline-block element.
You need to split and wrap every highlighted word with a seperate span.
JS:
var $selected = $('#content.notupdated .select');
$selected.each( function() {
var $this = $(this);
var words = $this.text().split(" ");
$this.empty();
$.each(words, function(i, v) {
$this.append($("<span>").text(v + ' '));
});
});
$('#content.notupdated').removeClass('notupdated').addClass('updated');
I updated you fiddle for that: https://jsfiddle.net/sddah9k2/7/
I even updated #Kaiidos improved fiddle of yours: https://jsfiddle.net/sddah9k2/6/ (I worked primarily on that one)
I already accepted Seika85 answer since his was the first. Here is what I ended up with, retaining the original span and also to do this for every 'select' span:
link:
https://jsfiddle.net/sddah9k2/9/
js:
// Get all the select spans
$('span.select').each(function (ix, block) {
// split the text spans on word so it wraps in the same place
var txtar = $(block).text().split(' ');
// Remove the class on the outer span, but keep it intact.
$(block).removeClass('select');
$(block).html(''); // empty it
// Replace each word. Note the space before closing span!
$(txtar).each(function (ix, txt) {
$(block).append($('<span class="bg">'+txt+' </span>'));
});
});
css:
.select {
background-color:#3efcdb;
}
.bg {
background-color:#3efcdb;
}
I want to fix header and 3 left columns of my table.
But I found only one suitable solution. Here is link: http://hazaa.com.au/blog/how-to-create-an-html-table-with-frozen-headers-and-columns/
I need to divide my table to 4 independent tables and that's not good.
Also I found plugin http://www.fixedheadertable.com/ (as suggestion in another such question on SO) unfortunately it can freeze only one left column (I need 3 columns).
Are there any other simple ways to implement this feature?
the standard html table does not support frozen columns, even with css.
if you can use a jquery plugin, i suggest you to use http://www.datatables.net/ with the fixedcolumns plugin (just configure and it works as supposed :) )
if not, you will end up with u solution where you havo to split up the table in the one or the other way, and if you want to have scrollable tables you will have to use javascript since css and html do not support such features yet.
Thought I'd share how I've done this. This code is surely somewhat based off other code found on this site but have long since lost exactly where. Requires jQuery. Potentially doesn't support various odd things you might want to do to a table.
<div class='tableFreeze'>
<table>
<thead>
<tr><th>Headers</th><th>Go</th><th>Here</th></tr>
</thead>
<tbody>
<!--table body stuff goes here-->
</tbody>
</table>
</div>
<style>
.tableFreeze {
width: 800px;
max-height: 500px;
position: relative;
overflow: scroll;
}
</style>
<script>
$(document).ready(function(){
//Sets a table to have frozen headers, or both headers and first column
//A div around the table must have a class of 'tableFreeze'
//can also add class 'tableFreeze_headersOnly' to the div to specify only headers should be frozen
//Table inside of div must have one thead and one tbody
//can set the div to have custom CSS defining the width and max height
$('.tableFreeze').each(function(){
var div=$(this);
//get the table in the DIV
var table=div.children('table');
// save original width to table object
var table_original_width=table.width();
//do the same for each td or th in the header
table.find('thead tr td,thead tr th').each(function(){
$(this).attr("data-item-original-width",$(this).width());
});
//do the same for the heights of each first cell on the left
var table_original_height=table.height();
table.find('tr').each(function(){
$(this).find('td,th').eq(0).attr("data-item-original-height",$(this).find('td,th').eq(0).height());
});
//need a copy of the table to strip away and make it just the header
var table_header=table.clone();
//set the whole copy of this table to have the proper width
table_header.width(table_original_width);
//set width of all cells in the header
table_header.find('thead tr td,thead tr th').each(function(){
$(this).width($(this).attr("data-item-original-width"));
});
//remove the tbody
table_header.find('tbody').remove();
//set the width and height of this header bar. add a header class
table_header.css({'width':table_original_width,'height':table_header.find("td,th").first().attr('data-item-original-height'),"position":"absolute","left":0,"top":0}).addClass('tableFreeze_header');
//add to div
div.append(table_header);
//if we have this class, we don't need to do the rest
if ($(this).hasClass('tableFreeze_headersOnly')) return;
//another copy of the table for just the left bar
var table_leftcol=table.clone();
//go through each row (in both tbody and thead)
table_leftcol.find('tr').each(function(){
//remove all but the first cell
$(this).children('td,th').slice(1).remove();
//set the height of each cell to be the original height
$(this).children('td,th').height(($(this).children('td,th').attr("data-item-original-height")*1)+1);
});
//set: the height of the whole table based on the original height we got, the width based on the width set of the first cell, absolutely position it on the left and right, and an id for finding later
table_leftcol.css({'height':table_original_height,'width':table_leftcol.find("td,th").first().attr('data-item-original-width'),"position":"absolute","left":0,"top":0}).addClass('tableFreeze_leftcol');
//add to div
div.append(table_leftcol);
//and finally a similar thing for just the top left cell (a1). That will need to always be on the top left.
var table_a1=table.clone();
//set copy to be original table width
table_a1.width(table_original_width);
//get the width and height of the a1 cell
var table_a1_width=table_a1.find("thead tr td,thead tr th").eq(0).attr("data-item-original-width");
var table_a1_height=table_a1.find("thead tr td,thead tr th").eq(0).attr("data-item-original-height");
//remove all but the first cell in the header
table_a1.find("thead tr td,thead tr th").slice(1).remove();
//and remove the entire tbody
table_a1.find('tbody').remove();
//set the first (and should be only remaining) cell to have the proper width/height
table_a1.find("thead tr td,thead tr th").eq(0).width(table_a1_width).height(table_a1_height);
//table positionin' stuff
table_a1.css({'width':table_a1_width,'height':table_a1_height,"position":"absolute","left":0,"top":0}).addClass('tableFreeze_a1');
//add to div
div.append(table_a1);
});
});
</script>