简体   繁体   English

在不影响 div 大小的情况下,将文本完美地放入 div(高度和宽度)内

[英]Fit text perfectly inside a div (height and width) without affecting the size of the div

I apologise in advance as I know this question has come up many times before but I just can't seem to find the right solution (and believe me I've tried a few!)我提前道歉,因为我知道这个问题之前已经出现过很多次,但我似乎找不到正确的解决方案(相信我,我已经尝试了一些!)

Basically it's the old " Fit text perfectly inside a div without affecting the size of the div ".基本上它是旧的“在 div 内完美匹配文本而不影响 div 的大小”。 And unless I'm a complete numpty, I believe CSS has no way of doing this.除非我是一个完整的 numpty,否则我相信 CSS 无法做到这一点。 So what I mean basically is rather than doing something like:所以我的意思基本上是而不是做类似的事情:

#someDiv {
font-size: 12px;
}

or...或者...

#someDiv {
font-size: 1em;
}

...I want to be able to do something like this: ...我希望能够做这样的事情:

#someDiv {
font-size: fluid;
}

...meaning that whatever text this div contains, scale it to fit perfectly from left to right and top to bottom with no overflow or whitespace. ...意味着无论这个 div 包含什么文本,缩放它以从左到右、从上到下完美地适合,没有溢出或空白。

After trawling through countless websites looking for this CSS solution, I've now accepted that CSS isn't capable of this ...so, enter jQuery.在浏览无数网站寻找这个 CSS 解决方案之后,我现在已经接受 CSS 不能做到这一点......所以,输入 jQuery。

I've found several jQuery solutions online but they will only scale the text to fit the width, but I want it to scale to the height as well.我在网上找到了几个 jQuery 解决方案,但它们只会缩放文本以适应宽度,但我希望它也缩放到高度。 So effectively I want to say to jQuery:如此有效地我想对 jQuery 说:

"jQuery, find $(this) div and whatever text is inside it I want you to scale it so that it fills the entire height and width of the div as tightly as possible". “jQuery,找到 $(this) div 以及其中的任何文本,我希望您缩放它,使其尽可能紧密地填充 div 的整个高度和宽度”。

In case I haven't explained myself very well, I've attached a graphic explaining the problem I'm facing and the solution I'm looking for.如果我没有很好地解释自己,我附上了一张图表,解释了我面临的问题和我正在寻找的解决方案。

Any help would be much appreciated.任何帮助将非常感激。 Thank you.谢谢你。

在此处输入图片说明

Here's the same answer, but in Javascript这是相同的答案,但在 Javascript 中

var autoSizeText;

autoSizeText = function() {
  var el, elements, _i, _len, _results;
  elements = $('.resize');
  console.log(elements);
  if (elements.length < 0) {
    return;
  }
  _results = [];
  for (_i = 0, _len = elements.length; _i < _len; _i++) {
    el = elements[_i];
    _results.push((function(el) {
      var resizeText, _results1;
      resizeText = function() {
        var elNewFontSize;
        elNewFontSize = (parseInt($(el).css('font-size').slice(0, -2)) - 1) + 'px';
        return $(el).css('font-size', elNewFontSize);
      };
      _results1 = [];
      while (el.scrollHeight > el.offsetHeight) {
        _results1.push(resizeText());
      }
       return _results1;
    })(el));
  }
  return _results;
};

$(document).ready(function() {
  return autoSizeText();
});

By the way...if you ever need to convert coffeescript to javascript, just go to js2coffee.org顺便说一句...如果您需要将 coffeescript 转换为 javascript,请访问js2coffee.org

I was wanting something similar myself recently:我最近自己也想要类似的东西:

<div class='container'>
    <div class='no-resize'>This text won't be resized and will go out of the div.</div>
    <div class='resize'>This text will be resized and wont go out of the div.</div>
</div>

And

.no-resize, .resize {
    width: 100px;
    height: 50px;
    border: 1px solid #000;
    color: #000;
    float: left;
    margin-left: 10px;
    font-size: 15px
}

Fiddler at jsfiddle.net/mn4rr/1/ . jsfiddle.net/mn4rr/1/ 上的提琴手。

I don't have enough reputation to comment on the accepted answer so I made an answer.我没有足够的声誉来评论接受的答案,所以我做了一个答案。 If height has a css3 transition on it the autoSizeText() and $.resizeText answers above have issues.如果height上有 css3 过渡,则上面的autoSizeText()$.resizeText答案有问题。 I made a short jquery plugin to fix this.我做了一个简短的 jquery 插件来解决这个问题。

$.fn.resizeText = function (options) {

    var settings = $.extend({ maxfont: 40, minfont: 4 }, options);

    var style = $('<style>').html('.nodelays ' +
    '{ ' +
        '-moz-transition: none !important; ' +
        '-webkit-transition: none !important;' +
        '-o-transition: none !important; ' +
        'transition: none !important;' +
    '}');

    function shrink(el, fontsize, minfontsize)
    {
        if (fontsize < minfontsize) return;

        el.style.fontSize = fontsize + 'px';

        if (el.scrollHeight > el.offsetHeight) shrink(el, fontsize - 1, minfontsize);
    }

    $('head').append(style);

    $(this).each(function(index, el)
    {
        var element = $(el);

        element.addClass('nodelays');

        shrink(el, settings.maxfont, settings.minfont);

        element.removeClass('nodelays');
    });

    style.remove();
}

To use this plugin you only need to call要使用这个插件,你只需要调用

 $(selector).resizeText();

I hope this saves someone else some time troubleshooting.我希望这可以为其他人节省一些时间进行故障排除。 I wasted a good 20 minutes scratching my head wondering why the code above was causing an infinite loop on my page.我浪费了 20 分钟摸不着头脑,想知道为什么上面的代码会在我的页面上造成无限循环。

I solved this by making a jQuery plugin, it's here: http://jsfiddle.net/c9YNz/2/ (Updated to deal with resizing windows)我通过制作一个 jQuery 插件解决了这个问题,它在这里: http : //jsfiddle.net/c9YNz/2/ (更新以处理调整窗口大小)

The code for the plugin just shrinks the text down to 0.01em size and then grows it to fit, here's the plugin code:该插件的代码只是将文本缩小到 0.01em 大小,然后将其增大以适应,这是插件代码:

$.fn.resizeText = function () {
    var width = $(this).innerWidth();
    var height = $(this).innerHeight();
    var html =  $(this).html();
    var newElem = $("<div>", {
        html: html,
        style: "display: inline-block;overflow:hidden;font-size:0.1em;padding:0;margin:0;border:0;outline:0"
    });

    $(this).html(newElem);
    $.resizeText.increaseSize(10, 0.1, newElem, width, height);

    $(window).resize(function () {
        if ($.resizeText.interval)
            clearTimeout($.resizeText.interval)

        $.resizeText.interval = setTimeout(function () {
            elem.html(elem.find("div.createdResizeObject").first().html());
            elem.resizeText();
        }, 300);
    });
}

$.resizeText = {
    increaseSize: function (increment, start, newElem, width, height) {
        var fontSize = start;

        while (newElem.outerWidth() <= width && newElem.outerHeight() <= height) {
            fontSize += increment;
            newElem.css("font-size", fontSize + "em");
        }

        if (newElem.outerWidth() > width || newElem.outerHeight() > height) {
            fontSize -= increment;
            newElem.css("font-size", fontSize + "em");
            if (increment > 0.1) {
                $.resizeText.increaseSize(increment / 10, fontSize, newElem, width, height);
            }
        }
    }
};

Then if you have this html:那么如果你有这个 html:

<div class="resizeText" style="width:1200px;height:400px;">
Insert text from slipsum here.
</div>

You call it just like this:你可以这样称呼它:

$(document).ready(function () {
    $(".resizeText").resizeText();
});

It's not the best way to do it, but it's enough for you to be going on with, I would imagine (plus it works).这不是最好的方法,但它足以让你继续下去,我想(加上它有效)。

Here's a slightly modified version of the given answers.这是给定答案的略微修改版本。

Requirements :要求

  1. The class containing text has the "resize" class.包含文本的类具有“调整大小”类。
  2. it contains height and width (%, px, whatever...)它包含高度和宽度(%、px 等等)

What changes in this version ?这个版本有什么变化?

  1. I added resizing ( by binding the event resize to the function) autoSizeText.我添加了调整大小(通过将事件调整大小绑定到函数)autoSizeText。 This is far from ideal, but if we don't resize a lot, it should be ok.这远非理想,但如果我们不大量调整大小,应该没问题。
  2. In previous versions, text is only getting smaller.在以前的版本中,文本只会变得更小。 Now it tries to find the biggest font without going outside the div.现在它尝试在不超出 div 的情况下找到最大的字体。

Note:笔记:

I don't claim the code is production ready.我不声称代码已准备好生产。 But if you find something you can enhance, please share it with the community.但是,如果您发现可以增强的东西,请与社区分享。

 var autoSizeText = function () { var el, elements, _i, _len; elements = $('.resize'); if (elements.length < 0) { return; } for (_i = 0, _len = elements.length; _i < _len; _i++) { el = elements[_i]; dichoFit = function (el) { diminishText = function () { var elNewFontSize; elNewFontSize = (parseInt($(el).css('font-size').slice(0, -2)) - 1) + 'px'; return $(el).css('font-size', elNewFontSize); }; augmentText = function () { var elNewFontSize; elNewFontSize = (parseInt($(el).css('font-size').slice(0, -2)) + 1) + 'px'; return $(el).css('font-size', elNewFontSize); }; diminishText(); while (el.scrollHeight < el.offsetHeight) { augmentText(); } augmentText(); while (el.scrollHeight > el.offsetHeight) { diminishText(); } } dichoFit(el); } }; $(document).ready(function () { autoSizeText(); $(window).resize(function resizeText(){ autoSizeText() }) });
 .sizable{ width: 50vw; height: 50vh; border: 2px solid darkred; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='sizable resize'> I am a sizable div and I have explicit width and height. </div>

Sorry late answer but can saves time of many, I write this code which works perfectly during resize of window.抱歉,回答晚了,但可以节省很多时间,我编写了这段代码,它在调整窗口大小期间完美运行。

function fitTextInDiv(){
    if($this.find('.text-wrapper').height() > $this.find('.aligned-text').height() ){
        while( $this.find('.text-wrapper').height() > $this.find('.aligned-text').height() ) {
             $this.find('.aligned-text').css('font-size', (parseFloat($this.find('.aligned-text').css('font-size')) + 0.1) + "px" );
        }
    }
    else if($this.find('.text-wrapper').height() < $this.find('.aligned-text').height() ){
        while( $this.find('.text-wrapper').height() < $this.find('.aligned-text').height() ) {
            $this.find('.aligned-text').css('font-size', (parseFloat($this.find('.aligned-text').css('font-size')) - 0.1) + "px" );
        }

     }
}

Where text-wrapper is parent and aligned-text is child div.其中 text-wrapper 是父级,aligned-text 是子级 div。 You must specify height and width of parent element.您必须指定父元素的高度和宽度。 In resize you can set height and width of parent element by using jquery then call this function.在调整大小时,您可以使用 jquery 设置父元素的高度和宽度,然后调用此函数。

I write this code because I tried my many plugins but they don't support resizing window.我写这段代码是因为我尝试了很多插件,但它们不支持调整窗口大小。

I got the same problem and the solution is basically use javascript to control font-size.我遇到了同样的问题,解决方案基本上是使用 javascript 来控制字体大小。 Check this example on codepen:在 codepen 上查看此示例:

https://codepen.io/ThePostModernPlatonic/pen/BZKzVR

try to resize it尝试调整它的大小

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
<style>
</style>
</head>
<body>
<div style="height:100vh;background-color: tomato;" id="wrap">        
  <h1 class="quote" id="quotee" style="padding-top: 56px">Because too much "light" doesn't <em>illuminate</em> our paths and warm us, it only blinds and burns us.</h1>
</div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var multiplexador = 3;
  initial_div_height = document.getElementById ("wrap").scrollHeight;
  setInterval(function(){ 
    var div = document.getElementById ("wrap");
    var frase = document.getElementById ("quotee");
    var message = "WIDTH div " + div.scrollWidth + "px. "+ frase.scrollWidth+"px. frase \n";
    message += "HEIGHT div " + initial_div_height + "px. "+ frase.scrollHeight+"px. frase \n";           
    if (frase.scrollHeight < initial_div_height - 30){
      multiplexador += 1;
      $("#quotee").css("font-size", multiplexador); 
    }
    console.log(message);          
  }, 10);
</script>
</html>

Here's my version.这是我的版本。 This is build with ES6 on a React project.这是在 React 项目上使用 ES6 构建的。

export function autoSizeText(container, attempts=30) {
  let setChildrenToInheritFontSize = ( el ) => {
    el.style.fontSize = 'inherit';
    _.each( el.children, (child) => {
      setChildrenToInheritFontSize( child );
    });
  };


  let resizeText = (el) => {
    attempts--;
    let elNewFontSize;
    if( el.style.fontSize === "" ||  el.style.fontSize === "inherit" ||  el.style.fontSize === "NaN" ){
      elNewFontSize = '32px'; // largest font to start with
    } else {
      elNewFontSize = (parseInt(el.style.fontSize.slice(0, -2)) - 1) + 'px';
    }
    el.style.fontSize = elNewFontSize;

    // this function can crash the app, so we need to limit it
    if( attempts <= 0 ) {
      return;
    }

    if ( (el.scrollWidth > el.offsetWidth) || (el.scrollHeight > el.offsetHeight)) {
      resizeText(el);
    }
  };
  setChildrenToInheritFontSize( container );
  resizeText(container);
}

I recently had a max width div that was receiving text from a text input field and I had to make the text fit in that div, so here is how I solved it (simple JS, no plugin):我最近有一个从文本输入字段接收文本的最大宽度 div,我必须使文本适合该 div,所以这是我解决它的方法(简单的 JS,没有插件):

 function addName(){ let maxWidth = 550; let defaultFontSize = 50; let entry = document.getElementById('nameInput').value; let nameDiv = document.getElementById('name'); let fontSize = nameDiv.style["font-size"]; fontSize = fontSize.replace('px',''); fontSize = parseInt(fontSize); nameDiv.innerText = entry; if (nameDiv.clientWidth > maxWidth) { fontSize = fontSize - 2.5; nameDiv.style["font-size"] = `${fontSize}px`; } else if (nameDiv.clientWidth < maxWidth && fontSize < defaultFontSize ) { fontSize = fontSize + 2.5; nameDiv.style["font-size"] = `${fontSize}px`; } }
 #name { height: 70px; line-height: 70px; text-align: center; overflow: hidden; width: auto; display: table; /*THAT DOES THE MAGIC*/ margin: auto; border: 1px solid black /* THE BORDER IS FOR TEST PURPOSES TO SEE WHAT'S HAPPENING WITH THE DIV*/ }
 <input type="text" id="nameInput" oninput="addName()"> <div id="name" style="font-size: 50px;"></div>

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

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