简体   繁体   English

如何使用CSS根据上一个div元素的宽度调整文本大小?

[英]How can I have text resize based on the width of the preceding div element using CSS?

I have a label I'm attempting to generate. 我有一个尝试生成的标签。 It has the following structure. 它具有以下结构。

  .name-box { width: 400px; background-color: #efefef; border: 1px solid #000; overflow: hidden; white-space: nowrap; } .last-name { font-size: 26px; display:inline; overflow: hidden;} .first-name { display:inline; overflow: hidden;} 
  <div class="name-box"> <div class="last-name">McDonald-OrAReallySuperDuperLongLastName</div> <div class="first-name">David</div> </div> 

What I'm wanting to do is change the text size of the first name, based on the length of the last name. 我要做的是根据姓氏的长度更改名字的文本大小。 If the name is "Venckus-Stringfellow" and I only have a little bit of space left I'd like the text size of the first name to be around 7px . 如果名字是“ Venckus-Stringfellow”并且我只剩下一点点空间,我希望名字的文本大小在7px左右。 But if the last name is "Le", then I'd want the first name to have a text size of 26px -- granted that having a text size of 26px still allows the first name to fit on the 600px that my div has to fill the label. 但是,如果姓氏是“ Le”,那么我希望名字的文本大小为26px授予文本大小为26px的文本仍允许名字适合我的div必须达到的600px填写标签。 How can I do this with HTML/CSS (if I MUST use Javascript then that's fine, was trying to avoid it though) ? 如何使用HTML / CSS做到这一点(如果我必须使用Javascript,那就可以了,不过还是想避免它)?

Javascript that uses the length of the last name in a mathematical equation to set the first names size. 使用数学方程式中姓氏长度来设置名字大小的Javascript。 This is a very simple example and you'd need to change it if you wanted it to be exponential and you should probably set high and low bounds that it can't go below. 这是一个非常简单的示例,如果您希望它是指数级的,则需要对其进行更改,并且可能应设置不能超出的上限和下限。

 var lastNameText = document.querySelector('.last-name').textContent; var firstName = document.querySelector('.first-name'); firstName.style.fontSize = (120 / lastNameText.length) + "px"; 
 .name-box { width: 600px; } .last-name { font-size: 26px; } .first-name: { font-size: 26px; } 
 <div class="name-box"> <div class="last-name">McDonald</div> <div class="first-name">David</div> </div> 

You're going to need javascript here, unfortunately. 不幸的是,您将需要在这里使用javascript。 You can get close using viewport percentages , but that applies to the whole viewport. 您可以使用视口百分比来关闭,但这适用于整个视口。 And it would only be for one container, not a preceding container. 而且仅适用于一个容器,而不适用于先前的容器。 What you are going to need to do is create an algorithm that updates the font sizes, and run it everytime you load HTML/text into those div 's. 您需要做的是创建一个更新字体大小的算法,并在每次将HTML /文本加载到这些div运行它。

Something like this should get you started: 这样的事情应该让您入门:

function loadNames(var firstName, var lastName) {
    //GET THE LENGTH OF THE LASTNAME STRING
    var len = lastName.length;
    var factor = .7; //CUSTOMIZE THIS TO DO YOUR SIZING BASED ON THE FONT
    var fontSize = Math.ceil(factor * len); //GET THE NEW FONT SIZE, BASED ON THE LENGTH AND FACTOR, AND ROUNDED UP
    //SET THE TEXT OF THE NAMES
    $('firstName').Text(firstName);
    $('lastName').Text(lastName);
    //SET THE FONT SIZES
    $('firstName').css({ 'font-size': fontSize + 'px;' });
    $('lastName').css({ 'font-size': fontSize + 'px;' });
}

CSS solution unfortunately is not possible. 不幸的是,CSS解决方案是不可能的。 It's not possible to select partial text inputs, and there's some calculation required that cannot be done with CSS at this point, in this age. 无法选择部分文本输入,并且在这个年龄段,此时需要进行一些无法使用CSS进行的计算。

But javascript.. Were you looking for something like this OP? 但是javascript ..您是否正在寻找这样的OP?

 $(document).on("ready", function(){ var ratio = 20; $(".name").keydown(function(){ var input_length = $(this).val().length / ratio; var new_size = (2 - input_length < 1 ? 1 : 2 - input_length); $(this).css("font-size", new_size+"em"); }); }); 
 input { width: 40em; margin: 0 auto; display: block; padding: 15px; } .main-wrapper { width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main-wrapper"> <input class="name" type="text" placeholder="Enter name" /> </div> 

This is another approach how You could achieve the result. 这是您可以实现结果的另一种方法。 Maybe it's possible to do not use table at all but each div should use display:inline-block; 也许可能根本不使用表格,但是每个div应该使用display:inline-block; property. 属性。

 $scalingL = $('.last-name').width(); $scalingF = $('.first-name').width(); $('.first-name').css('font-size',($scalingL / $scalingF * 26)); 
 .name-box { width: 600px; background-color: #efefef; border: 1px solid #000; overflow: hidden; white-space: nowrap; } .last-name { font-size: 26px; overflow: hidden; display:inline-block;} .first-name { font-size: 26px; display:inline-block;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="name-box"> <table> <tr> <td> <div class="last-name">McDonald-OrAReallySuperDuperLongLastName</div> </td> </tr> </tr> <td> <div class="first-name">Something</div> </td> </tr> </table> </div> 

Try this:
            <style type="text/css">


                .name-box{
                    width:auto;
                    overflow:auto;

                }

                #first-name{

                    display:inline-block;
                    width:auto;
                    clear:both;
                    float:left;
                    font-size:1px;

                    }

                    #last-name {

                    display:inline-block;
                    width:auto;
                    clear:both;
                    float:left;
                    font-size:26px;
                }

                </style>
                <div class="name-box">
                    <div id="last-name">McDonalds</div>
                    <div id="first-name">David</div>
                </div>

                <script type="text/javascript">
                var ln = document.getElementById("last-name");
                var fn = document.getElementById("first-name"); 



                for ( i = 1; ln.offsetWidth>fn.offsetWidth; i+=.5)
                {

                    fn.style.fontSize=(i)+"px";
                }


                </script>

 <style type="text/css"> .name-box{ width:auto; overflow:auto; } #first-name{ display:inline-block; width:auto; clear:both; float:left; font-size:1px; } #last-name { display:inline-block; width:auto; clear:both; float:left; font-size:26px; } </style> <div class="name-box"> <div id="last-name">McDonalds</div> <div id="first-name">David</div> </div> <script type="text/javascript"> var ln = document.getElementById("last-name"); var fn = document.getElementById("first-name"); for ( i = 1; ln.offsetWidth>fn.offsetWidth; i+=.5) { fn.style.fontSize=(i)+"px"; } </script> 

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

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