简体   繁体   English

如何将两个字符串与字母间距对齐

[英]How to align two strings with letter-spacing

I want to make two string with different symbols count the same length.我想让两个具有不同符号的字符串长度相同。 The first letter of str 2 must be starting under first letter of str 1. The same for the last letters. str 2 的第一个字母必须从 str 1 的第一个字母开始。最后一个字母相同。

ie IE

<h1>123</h1> <!--one symbol more-->
<h1>12</h1> 

I want:我想:

<h1>123</h1> 
<h1>1 2</h1> <!--more letter-space-->

How can I get it dynamically with css for any string length?对于任何字符串长度,如何使用 css 动态获取它? Maybe no need to use letter-spacing for this!也许不需要为此使用字母间距!

If you want the output to be something like this:如果您希望 output 是这样的:

123
1 2

here is something that will work: HTML这是可行的方法:HTML

<h1>123</h1>
<h1 class="spacing">12</h1>

CSS: CSS:

.spacing{
  letter-spacing:14px;
}

H1 letters are about 14 pixels wide. H1 字母的宽度约为 14 像素。

If you can initially have space between numbers then you can easily do this by reducing the letter spacing and justifying the text:如果您最初可以在数字之间留出空格,那么您可以通过减小字母间距和对齐文本来轻松做到这一点:

 .box { display:inline-block; border:1px solid; }.box h1 { text-align-last:justify; letter-spacing:-0.1em; margin:5px 0; }
 <div class="box"> <h1>1 2 3</h1> <h1>1 2</h1> <h1>1 2 4 5</h1> </div> <h1>1245</h1>

you need to use javascript function and append the data to the element with reference id.您需要使用 javascript function 和 append 将数据传递给具有引用 id 的元素。

You can achieve this using JavaScript by splitting the inner text and adding spaces then joining again, but if you want to stick with html and css only i will suggest you to add spans in the h1 and give them margin.您可以使用 JavaScript 通过拆分内部文本并添加空格然后再次加入来实现此目的,但是如果您想坚持使用 html 和 css,我只会建议您在 h1 中添加跨度和边距。

You can use non-breaking space &nbsp in HTML您可以在 HTML 中使用不间断空格&nbsp

<h1>123</h1>
<h1>1&nbsp&nbsp3</h1>

Use twice for character space.字符空间使用两次。

Preview预习

123 123

1 &nbsp3 1 &nbsp3

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

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