I’m creating a terminal-like app with JS. I want to be able to change the color of individual characters, so I decided to use a lot of spans to display them. Each span contains only one character. This is what I ended up with. It works fine, but I want the "span screen" (green outline) to fit the screen as much as it can. It should scale but keep ascpect ratio. I posted the code on CodePen.
I tried changing the size of the div that contains spans, but it didn’t help. Then I tried changing the font size of spans, but it didn’t help either. Maybe there’s a better way to accomplish it all.
UPDATE: This is how it is now, and this is how it should be.
Here’s the code:
document.addEventListener("DOMContentLoaded", draw);
function draw() {
game = document.getElementById("game");
for (y = 0; y <= 25; y++) {
for (x = 0; x <= 100; x++) {
cell = document.createElement("span");
cell.innerText = "1";
game.appendChild(cell);
}
game.appendChild(document.createElement("br"));
}
}
* {
margin: 0;
padding: 0;
border: 0;
}
html {
min-height: 100vh;
}
body {
min-height: 100vh;
box-shadow: 0px 0px 0px 10px red inset;
display: flex;
justify-content: center;
align-items: center;
}
#game {
box-shadow: 0px 0px 0px 10px green inset;
}
span {
font-family: Cascadia Code;
}
<div id='game'></div>
3
Answers
I am struggling to understand your issue, but I think you are trying to lay out the numbers in a grid of 101 by 25 inside a container of unknown width. If you set the width of each span to
calc(100%/101)
, you should be able to accomplish it.Could you use a grid?
If it’s a game the good thing is after you can address each cell by row and col.
Getting HTML elements to scale exactly can be tricky.
But SVG’s are designed to be scaled, so if yo build your terminal inside an SVG things become much easier.
Below is a working snippet, I’ve adjusted the Row & Cols to just fit nicer inside a SO Snippet. You can also make the SVG scale X & Y with the
preserveAspectRatio
attribute, click the SVG to see the difference.