简体   繁体   English

在整个页面上均匀且动态地拆分文本

[英]Split text evenly and dynamically across page

I'm wondering if it's possible to have something like this 我想知道是否可能有这样的事情

 .full-width { font-size: 10vh; letter-spacing: 15vw; } 
 <div class="full-width"> testing </div> 

and let's say that works, font is a nice size and is spread from one end to the other evenly. 可以说,效果很好,字体大小合适,并且从一端均匀地分布到另一端。 I made that css just by trying to see which size worked. 我通过尝试查看哪种大小来制作了CSS。

But if I change the text from 'testing' to something 'home', it wouldn't stretch across no more. 但是,如果我将文本从“测试”更改为“家庭”,它就不会再延伸了。

for example, 例如,

 .full-width { font-size: 10vh; letter-spacing: 15vw; } 
 <div class="full-width"> home </div> 

Notice how home does not reach from end to end anymore, until I change the css (which again I discover from trail and error). 请注意,直到我更改css(再次从跟踪和错误中发现)之后,home才不再从头到尾到达。

 .full-width { font-size: 10vh; letter-spacing: 30vw; } 
 <div class="full-width"> home </div> 

I'm not sure how I would dynamically change the text via js to predict the full needed width. 我不确定如何通过js动态更改文本以预测所需的完整宽度。

In regards to Yosef's answer; 关于约瑟夫的回答; this is for a react component which I'm trying to pass new text towards in. So when I get into that situation innerText return's null so it causes me an error. 这是针对我正在尝试向其中传递新文本的react组件。因此,当我遇到这种情况时, innerText返回的null为null,因此会导致我出错。

I tried approaching it like this 我试图这样接近

var footerContents = this.props.page();
 var contents = [];
 footerContents.split('').map(i => contents.push(<span> {i} </span>)).join('');

and then just display the contents 然后只显示内容

<div class="full-width">
  {contents}
</div>

it can be done with javascript: 可以用javascript完成:

  var el = document.getElementById('test'); var html = el.innerText.split('').map(i => '<span>'+i+'</span>').join(''); el.innerHTML = html; 
  .full-width { font-size: 10vh; display: flex; justify-content: space-between; } 
 <div id="test" class="full-width"> home </div> 

for react case: 对于反应情况:

var footerContents = this.props.page() || '';
var contents = footerContents.split('').map(i => <span> {i} </span>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM