使用css和html根据文本长度动态更改字体大小

use*_*399 29 html javascript css

我需要将用户输入的文本显示为固定大小的div.我想要的是自动调整字体大小,以便文本尽可能填充框.

我可能想要从最大字体大小开始,虽然文本太大而不适合容器,但缩小字体大小直到它适合并且字体必须显示为单行.是否可以仅使用css和html执行此操作.

put*_*nde 25

说你有这个:

<div id="outer" style="width:200px; height:20px; border:1px solid red;">
    <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mollis dui felis, vel vehicula tortor cursus nec</div>
</div>
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

$(document).ready(function () {
    resize_to_fit();
});

function resize_to_fit(){
    var fontsize = $('div#outer div').css('font-size');
    $('div#outer div').css('fontSize', parseFloat(fontsize) - 1);

    if($('div#outer div').height() >= $('div#outer').height()){
        resize_to_fit();
    }
}
Run Code Online (Sandbox Code Playgroud)

工作样本

$(document).ready(function() {
  resize_to_fit();
});

function resize_to_fit() {
  var fontsize = $('div#outer div').css('font-size');
  $('div#outer div').css('fontSize', parseFloat(fontsize) - 1);

  if ($('div#outer div').height() >= $('div#outer').height()) {
    resize_to_fit();
  }
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="outer" style="width:200px; height:20px; border:1px solid red;">
  <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mollis dui felis, vel vehicula tortor cursus nec</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这需要 jquery 运行,操作从不声明 jquery。 (6认同)

Tha*_*dra 9

根据此处最佳答案的逻辑,我创建了一个无 jQuery 版本

const input = document.querySelector('input');
const output = document.querySelector('.output');
const outputContainer = document.querySelector('.container');

function resize_to_fit() {
  let fontSize = window.getComputedStyle(output).fontSize;
  output.style.fontSize = (parseFloat(fontSize) - 1) + 'px';
  
  if(output.clientHeight >= outputContainer.clientHeight){
    resize_to_fit();
  }
}

function processInput() { 
  output.innerHTML =this.value;
  output.style.fontSize = '100px'; // Default font size
  resize_to_fit();
}

input.addEventListener('input', processInput);
Run Code Online (Sandbox Code Playgroud)
<input type="text" placeholder="Add text input here" style="margin: 10px; width:50%;">

<div id="outer" class="container" 
style="width:80%; height:100px; border:2px solid red; font-size:20px;">
  
<div class="output" style="word-break: break-all; word-wrap: break-word;">
</div>

</div>
Run Code Online (Sandbox Code Playgroud)


gre*_*gor 8

无需迭代字体大小。您只需要计算文本的当前大小和容器的大小并应用缩放。

const text_el = document.querySelector("#text");
const container_el = document.querySelector("#container");

text_el.addEventListener("input", function(e) {
  container_el.children[0].textContent = this.value;
  resize2fit(container_el)
});

function resize2fit(el) {
  if (!el.parentElement) return;
  el.style.setProperty("--font-size", "1em");
  const {width: max_width, height: max_height} = el.getBoundingClientRect();
  const {width, height} = el.children[0].getBoundingClientRect();
  el.style.setProperty("--font-size", Math.min(max_width/width, max_height/height)+"em");
}

window.addEventListener("resize", function(e) {
    resize2fit(container_el);
});
resize2fit(container_el);
Run Code Online (Sandbox Code Playgroud)
#container {
  width: 100%;
  height: 100px;
  font-size: var(--font-size, 1em);
  white-space: nowrap;
  border: 1px solid blue;
}
#container > * {
  display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)
<input id="text" value="lorem ipsum" />
<div id="container">
  <span>lorem ipsum</span>
</div>
Run Code Online (Sandbox Code Playgroud)


cfs*_*cfs 2

获取文本字符串的像素大小是一件很难的事情,并且在很大程度上取决于浏览器和用户的设置,因此可能很难想出一个适用于所有情况的解决方案。

“显示用户输入的文本”是指用户正在文本框中输入内容吗?如果是这样,您可以附加一个按键侦听器来检查文本值的长度,然后进行font-size相应的调整。这是一个示例,但您可能需要更改长度和字体大小以满足您的需求:

工作演示

$('#text').on('keypress', function(e) {
    var that = $(this),
        textLength = that.val().length
    ;

    if(textLength > 30) {
        that.css('font-size', '5px');
    } else if(textLength > 20) {
        that.css('font-size', '10px');
    } else if(textLength > 10) {
        that.css('font-size', '15px');
    }
});
Run Code Online (Sandbox Code Playgroud)