CSS Formula for Precisely Offsetting Text

38 Views Asked by At

On my website, I am aligning a body of text precisely as exemplified in the following snippet.

html, body {
  width: 100%;
  height: 100%;
  font-family: arial;
  font-size: 48px;
  text-transform: uppercase;
}

body {
  margin: 0;
  background: skyblue;
}

div {
  width: 200px;
  height: 100vh;
  padding-top: calc(100vh - (1.5rem * 1.35));
  box-sizing: border-box;
  border: solid red 1px;
}

span {
  line-height: 1.35;
  display: inline-block;
  border: solid black 1px;
}
<div><span>This</span>&nbsp;<span>is</span>&nbsp;<span>a</span><span>body</span><span>of</span>&nbsp;<span>text.</span></div>

However, I was hoping to take it one step further. For instance, I want to be able to place the top of the text as close as possible to, say, 60vh from the top of the page while still displaying half of the last line. Below is an example of what I mean in JS.

Note: Just noticed the second snippet does not display properly unless you open it to edit it. If you transfer it to codepen, it should work properly.

const
  div = document.querySelector('div'),
  span = document.querySelector('span'),
  lineHeight = parseFloat(getComputedStyle(span).lineHeight),
  target = innerHeight * 0.6,
  remainder = (innerHeight - target) / lineHeight % 1 * lineHeight

div.style.paddingTop = target + remainder - lineHeight / 2 + 'px'
html, body {
  width: 100%;
  height: 100%;
  font-family: arial;
  font-size: 48px;
  text-transform: uppercase;
}

body {
  margin: 0;
  background: skyblue;
}

div {
  width: 200px;
  height: 100vh;
  position: absolute;
  box-sizing: border-box;
  border: solid red 1px;
}

span {
  line-height: 1.5;
  display: inline-block;
  border: solid black 1px;
}
<div><span>This</span>&nbsp;<span>is</span>&nbsp;<span>a</span>&nbsp;<span>body</span>&nbsp;<span>of</span>&nbsp;<span>text.</span></div>

Notably, I know you can obviously find the "remainder" using calc, viewport units, and rem, but the rest is what is confusing because I am not great at math and also lacking sleep.

Hence I was hoping that somebody out there, who is better at math than me, would be able to tell me whether or not a pure CSS solution without preprocessors is possible (i.e. using only calc, viewport units, rem units, etc) before I waste any more time thinking about this. I know there are some nifty CSS formulas for fluid typography, but is something like this possible?

[ edit ] I thought about this some more while laying in bed. I do not believe it is possible without being able to calculate the "remainder." And there does not seem to be any way to calculate the "remainder" with only addition, subtraction, multiplication, and division. Please correct me if I am wrong.

1

There are 1 best solutions below

1
Sivak On

The goal is to have the DIV tag be 100% of the document's height and then the text is offset a little bit within the DIV?

I think just adding another tag within the DIV to offset all the text can work. You said you want 60vh. Line Height should also be used when dealing with text height.

html, body {
  width: 100%;
  height: 100%;
  font-family: arial;
  font-size: 48px;
  text-transform: uppercase;
}

body {
  margin: 0;
  background: skyblue;
}

div {
  width: 200px;
  height: 100vh;
  box-sizing: border-box;
  border: solid red 1px;

}

p
{   margin: 0;
    padding-top: 60vh;
  margin-top: -0.8em;
  line-height: 1.6em;
}

span {
  line-height: 1.35;
  display: inline-block;
  border: solid black 1px;
}
<html>
<body>
<div><p><span>This</span>&nbsp;<span>is</span>&nbsp;<span>a</span><span>body</span><span>of</span>&nbsp;<span>text.</span></p></div>
</body>
</html>

Or is this not quite it?