为不同的字母设置适当的边距?

时间:2017-09-19 07:05:10

标签: javascript html css

有些字母在显示时会比其他字母占用更大的空间,那么如何在下面的情况下使空白区域得到适当的自动调整?

在这段代码中,自动调整'#re1'之间的差距的方法是什么?和'#re2?所以我不会遇到以下情况,

enter image description here

enter image description here

如果我根据字母表设置边距' m'那么当我输入' a'时,其间的空格对我来说太大了。



function tx() {
  var x = document.getElementById("123").value;
  document.getElementsByClassName("re")[0].innerHTML = x;
  document.getElementsByClassName("re")[1].innerHTML = x;
}

#re1 {
  width: 200px;
  word-wrap: break-word;
  height: 20px;
}

#re2 {
  width: 150px;
  word-wrap: break-word;
  height: 20px;
  margin-top: 3px;
}

<!doctype html>
<html>

<head>

  <body>
    <textarea id="123" onKeyUp="tx();" onKeyPress="tx();" maxlength="23"></textarea><br>
    <div class="out">
      <div class="re" id="re1">1</div>
      <div class="re" id="re2">2</div>
    </div>
  </body>

</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

使用line-height

function tx() {
  var x = document.getElementById("123").value;
  document.getElementsByClassName("re")[0].innerHTML = x;
  document.getElementsByClassName("re")[1].innerHTML = x;
}
#re1 {
  width: 200px;
  word-wrap: break-word;
  line-height: 110%;
}

#re2 {
  width: 150px;
  word-wrap: break-word;
  line-height: 110%;
}
<!doctype html>
<html>

<head>

</head>

<body>
  <textarea id="123" onKeyUp="tx();" onKeyPress="tx();" maxlength="23"></textarea><br>
  <div class="out">
    <div class="re" id="re1">1</div>
    <div class="re" id="re2">2</div>
  </div>
</body>

</html>

答案 1 :(得分:0)

试试这个代码段:

#re1 {
position:relative;      //add
width: 200px;           //remove
word-wrap: break-word;
height: 20px;
}

答案 2 :(得分:0)

删除高度属性

&#13;
&#13;
function tx() {
  var x = document.getElementById("123").value;
  document.getElementsByClassName("re")[0].innerHTML = x;
  document.getElementsByClassName("re")[1].innerHTML = x;
}
&#13;
#re1 {
  width: 200px;
  word-wrap: break-word;
}

#re2 {
  width: 150px;
  word-wrap: break-word;
  margin-top: 3px;
}
&#13;
<!doctype html>
<html>

<head>

  <body>
    <textarea id="123" onKeyUp="tx();" onKeyPress="tx();" maxlength="23"></textarea><br>
    <div class="out">
      <div class="re" id="re1">1</div>
      <div class="re" id="re2">2</div>
    </div>
  </body>

</html>
&#13;
&#13;
&#13;