Truncate opposite end of string inside html table - html

Is there a css / html way of truncating the from the start of a string? Showing the end characters instead?
For example:
string = "A0000000982091011328885"
truncated (show start) = "A000000098..."
truncated (show end) = "...1011328885"
I've tried changing the text direction but apart from that I'm out of ideas. I am completely capable of doing this in Javascript however it'd be nice not to.
I'm also doing this within a table td, so if there is some weird table specific <element> that'd be satisfactory.

Here is a "reverse ellipsis" pen made by Roman Komarov which does exactly what you want using just pure CSS. It just requires a specific HTML markup in order to work.
<div class="box ellipsis reverse-ellipsis">
<div class="ellipsis__content">Here is some long content that doesn't fit.</div>
</div>
It also uses pseudo-elements as the ellipsis and positioned them at the start of the text.
.reverse-ellipsis::after {
content: "…";
float: left;
width: 1em;
padding: 0 1px 0 1em;
margin: -1.35em -1em;
background: #FFF;
}

var rows = document.getElementById('container').childNodes;
for (var i=0, row; row = rows[i]; i++) {
trimLeft(row);
}
function trimLeft(row){
var trimContents = function(row, node){
while (row.scrollWidth > row.offsetWidth) {
var childNode = node.firstChild;
if (!childNode)
return true;
if (childNode.nodeType == document.TEXT_NODE){
trimText(row, node, childNode);
}
else {
var empty = trimContents(row, childNode);
if (empty){
node.removeChild(childNode);
}
}
}
}
var trimText = function(row, node, textNode){
var value = '...' + textNode.nodeValue;
do {
value = '...' + value.substr(4);
textNode.nodeValue = value;
if (value == '...'){
node.removeChild(textNode);
return;
}
}
while (row.scrollWidth > row.offsetWidth);
}
trimContents(row, row);
}
#container {
width: 200px;
border: 1px solid blue;
}
#container div {
width: 100%;
overflow: hidden;
white-space: nowrap;
}
<div id="container" >
<div>A00000009sfsgsdfggdsf1011328885</div>
</div>

Related

How do I set properties according to custom HTML attributes in CSS?

I'm working on Fractions.
My checkpoint is :
frac {
display: inline-block;
position: relative;
vertical-align: middle;
letter-spacing: 0.001em;
text-align: center;
}
frac {
display: block;
padding: 0.01em;
}
frac {
border-top: thin solid black;
}
It is working but I want to two things :
I want to set default values, i.e. 0 to num and 1 to den.
I want to do use this type of code to make things simple : <frac num="0" den="1"> Note :It should not have a closing tag.The num and den attributes should have their default values if not mentioned.
Btw, I only know CSS.
There is no <frac> element in HTML.
To create your own <frac-el num="3" den="4"></frac-el>, you can create a small web component, which must adhere to custom element naming rules (name must contain a -). It also will need a closing tag.
That being said, here we go:
class FractionElement extends HTMLElement {
numerator = document.createElement('sup');
denominator = document.createElement('sub');
style = document.createElement('style');
constructor(n = 0, d = 1) {
super().attachShadow({mode:'open'});
this.num = n;
this.den = d;
this.style.textContent = `
:host { display: inline-flex; flex-direction: column; text-align: center; }
sup { border-bottom: 1px solid #666; }
`;
this.shadowRoot.append(this.numerator, this.denominator, this.style);
}
get num() { return this.numerator.textContent; }
set num(val) { this.numerator.textContent = val; }
get den() { return this.denominator.textContent; }
set den(val) { this.denominator.textContent = val; }
static get observedAttributes() {
return ['num', 'den'];
}
attributeChangedCallback(attr, oldVal, newVal) {
if (oldVal === newVal) return; // nothing to do, no change
switch (attr) {
case 'num': this.num = newVal ?? 0; break;
case 'den': this.den = newVal ?? 1; break;
}
}
}
customElements.define('frac-el', FractionElement);
<frac-el num="3" den="4"></frac-el>
<frac-el num="11" den="27691"></frac-el>
<frac-el></frac-el>
<frac-el num="3x + 7" den="y"></frac-el>

Auto Adjusted Row & Column Grid in HTML CSS

I have the below design to be created dynamically
I have this layout & wanted to convert this using HTML CSS. My requirement is that, when any column is deleted another column should increase the space in place of a deleted column.
Also, the same is needed when someone removes any row, the previous row should take place of that row.
To build the grid: You can archive this by using a loop inside a loop. For Styling i use flex box system.
const c = document.querySelector('div');
for (let i = 0; i < 4; i++) {
const row = document.createElement('div');
row.setAttribute('class','row')
for (let j = 0; j < 4; j++) {
const cell = document.createElement('div');
cell.setAttribute('class','cell')
cell.innerHTML = i + ' - ' +j
row.append(cell);
}
c.append(row)
}
.container {
background: yellow;
height:100vh;
}
.row {
display: flex;
}
.cell {
border: 1px solid black;
padding: 5px;
width:25%;
height: 25vh;
}
<div class="container"></div>

HTML dynamic table - stop expand height when insert text

I've dynamic table with fixed width and height. Using random function I get numbers of cells (3x3 - 7x7). Table is rendering well. On each cell I've click event which write one letter to pointed cell. There a problem, because row is expanding on height. How to stop it?
https://codepen.io/anon/pen/PdrNVM
let table = document.getElementById('table');
let tableSize = Math.floor((Math.random() * 5) + 3);
console.log(tableSize);
for (i = 0; i < tableSize; i++) {
let tr = document.createElement('tr');
for (j = 0; j < tableSize; j++) {
let td = document.createElement('td');
let content = document.createTextNode('');
td.appendChild(content);
tr.appendChild(td);
}
table.appendChild(tr);
}
let tds = document.querySelectorAll("td");
tds.forEach((td) => {
td.addEventListener("click", function() {
td.innerHTML = 'p';
});
});
table {
width: 200 px;
height: 200 px;
table-layout: fixed;
}
td {
background-color: green;
}
<h2>Click in cell</h2>
<table id='table'></table>
Just set a proper height and max-height like:
td {
background-color: green;
height: 50px;
max-height: 50px;
}
Set a fixed height for the tds. You don't need use the height for the table, let the child td make up the height.
table {
width: 200px;
table-layout: fixed;
}
td {
height: 60px;
background-color: green;
}

How to offset dataObject from referenceObject using popper.js?

How do I offset the boat from the anchor by a few pixels?
You can find a code pen where I have unsuccessfully tried to set an offset here
https://codepen.io/anon/pen/wXraLK?editors=1111
HTML
<script src="https://unpkg.com/popper.js/dist/umd/popper.min.js"></script>
<div class="anchor">Anchor</div>
<div class="boat">Boat</div>
CSS
.boat {
display: inline-block;
background-color: yellow;
}
.anchor {
display: inline-block;
background-color: gray;
}
JavaScript
var anchor = document.getElementsByClassName("anchor")[0];
var boat = document.getElementsByClassName("boat")[0];
var offsetTopModifier = function (data) {
data.offsets.popper.top += 50;
return data;
}
var popper = new Popper(
anchor,
boat,
{
placement: 'bottom-end',
modifiers: [offsetTopModifier]
}
);
This was the source that inspired my attempt:
https://github.com/FezVrasta/popper.js/issues/107
One work around was to set margins on the boat.

How can I slide out a line on the left and right side of text on mouse hover

Essentially, I'm looking to animate a line on the left and right side of text that will increase its width to the end of the display when I hover over the text.
Perhaps this will help...
without hovering:
SOME TEXT
on hover:
----------------------------SOME TEXT--------------------------------------------------------------------------------------
I'd like these lines to animate outward to the end on the parent. I've tried using the pseudo elements but had no luck. Some help would be greatly appreciated.
Here's how I'd do it. Feel free to play with animation duration and timing function:
.separator {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.separator:before, .separator:after {
content: '';
flex-grow:0;
height: 1px;
background-color: currentColor;
transition: flex-grow .6s cubic-bezier(.4,0,.2,1);
margin: 0 .5rem;
}
.separator:hover:before, .separator:hover:after {
flex-grow: 1;
}
<div class="separator">SOME TEXT</div>
<div style="width: 50%; margin-top: 60px;border: 1px solid red; color: blue; padding: 3rem 0;">
<div class="separator">TEST</div>
Here's a JavaScript solution. Adds - to either side until it reaches the end of the line, and it removes the dashes when the mouse moves away.
To prevent the overflow, you just have to track the clientHeight and stop adding dashes as soon as the height increases.
var div = document.getElementsByClassName('test')[0];
var origText = div.innerText;
var origHeight = div.clientHeight;
var tooLong = false;
var addTxtInt;
div.addEventListener('mouseover', function() {
addTxtInt = setInterval(function() {
if (tooLong)
return;
if (div.clientHeight > origHeight) {
div.innerText = div.innerText.substring(1, div.innerText.length - 1);
tooLong = true;
return;
}
div.innerText = "-" + div.innerText + "-";
if (div.clientHeight > origHeight) {
div.innerText = div.innerText.substring(1, div.innerText.length - 1);
tooLong = true;
return;
}
}, 80);
});
div.addEventListener('mouseleave', function() {
clearInterval(addTxtInt);
div.innerText = origText;
tooLong = false;
});
.test {
display: block;
overflow: hidden;
text-align: center;
}
<div class='test'>SOME TEXT</div>