如何使文本自动缩小以适合div?

时间:2022-03-09 21:15:14

I know i can adjust the font size, but I want it to shrink to fit the div automatically when it is viewed on a different platform which reads font sizes differently for example.

我知道我可以调整字体大小,但是我希望它在不同平台上查看时自动收缩以适应div,例如,不同平台读取字体大小。

See the fiddle: http://jsfiddle.net/08pyzgx4/

看小提琴:http://jsfiddle.net/08pyzgx4/

<body>
    <div style="width:600px; height:58px; max-width: 600px; background-image:url(http://itrace.co.za/images/emailfiles/banner.png);     background-repeat:no-repeat; display:block; max-width: 600px; font-family:Arial, Helvetica, sans-serif; font-size:19px; color:#000;     text-align:center; padding-top:12px;"><b>Bla Bla Bla Bla Bla Bla </b>

        <div style="height:16px; max-height:16px; padding-left:88px; padding-right:88px; margin-top:18px; font-size:0.6em; letter-spacing:-0.02em; position:relative;"><strong>Website: </strong>www.blabla.co.za | <strong>E-mail: </strong>info@blabla.co.za | <strong>Control Centre: </strong>08600-22-blabla</div>
    </div>
</body>

2 个解决方案

#1


3  

You can do it like this....and don't forget to add js library.

你可以这样做....并且不要忘记添加js库。

this is the fiddle...http://jsfiddle.net/f6sx474j/ Try by increasing the text i checked it and its working properly.

这是小提琴... http://jsfiddle.net/f6sx474j/尝试通过增加文本我检查它和它的工作正常。

<div style="width:600px; 
height:58px;
max-width: 600px;
background-image:url(http://itrace.co.za/images/emailfiles/banner.png); 
background-repeat:no-repeat;
display:block; max-width: 600px;
font-family:Arial, Helvetica, sans-serif; 
font-size:19px; color:#000;     
text-align:center; padding-top:12px;">
<b>Bla Bla Bla Bla Bla Bla </b>
<div id="abc" style="height:16px;
 display: inline-block; 
 max-height:16px; 
 padding-left:88px;
 padding-right:88px; 
 margin-top:18px; 
 font-size:0.6em; 
 letter-spacing:-0.02em;
  position:relative;">
<strong>Website: </strong>www.blabla.co.za |
 <strong>E-mail:   </strong>
 info@blabla.co.za info@blabla.co.za info@b
 <strong>Control Centre: </strong>
 08600-22-blabla</div>
</div>

//And this is your js

//这是你的js

<script type="text/javascript">
$(function() {
var div = $('#abc');
var fontSize = parseInt(div.css('font-size'));
do {
    fontSize--;
    div.css('font-size', fontSize.toString() + 'px');
    } while (div.width() >= 400);
   });
 </script>

#2


3  

You should set width as 100% and max-width as value (600px in you case).

您应将宽度设置为100%,将max-width设置为值(在您的情况下为600px)。

This is a qucik example just to show you the right way, so now you can style it as you like.

这是一个qucik示例,只是为了向您展示正确的方式,所以现在您可以根据自己的喜好设计样式。

http://jsfiddle.net/08pyzgx4/1/

http://jsfiddle.net/08pyzgx4/1/

Remember that for scaling text, the div should always have a percentage width (and off course a max-width if you need one)

请记住,对于缩放文本,div应始终具有百分比宽度(如果需要,则偏离最大宽度)

Then just use a couple of media queries to adjust the text position on different resolutions and it should be ready

然后只需使用几个媒体查询来调整不同分辨率的文本位置,它应该准备就绪

#1


3  

You can do it like this....and don't forget to add js library.

你可以这样做....并且不要忘记添加js库。

this is the fiddle...http://jsfiddle.net/f6sx474j/ Try by increasing the text i checked it and its working properly.

这是小提琴... http://jsfiddle.net/f6sx474j/尝试通过增加文本我检查它和它的工作正常。

<div style="width:600px; 
height:58px;
max-width: 600px;
background-image:url(http://itrace.co.za/images/emailfiles/banner.png); 
background-repeat:no-repeat;
display:block; max-width: 600px;
font-family:Arial, Helvetica, sans-serif; 
font-size:19px; color:#000;     
text-align:center; padding-top:12px;">
<b>Bla Bla Bla Bla Bla Bla </b>
<div id="abc" style="height:16px;
 display: inline-block; 
 max-height:16px; 
 padding-left:88px;
 padding-right:88px; 
 margin-top:18px; 
 font-size:0.6em; 
 letter-spacing:-0.02em;
  position:relative;">
<strong>Website: </strong>www.blabla.co.za |
 <strong>E-mail:   </strong>
 info@blabla.co.za info@blabla.co.za info@b
 <strong>Control Centre: </strong>
 08600-22-blabla</div>
</div>

//And this is your js

//这是你的js

<script type="text/javascript">
$(function() {
var div = $('#abc');
var fontSize = parseInt(div.css('font-size'));
do {
    fontSize--;
    div.css('font-size', fontSize.toString() + 'px');
    } while (div.width() >= 400);
   });
 </script>

#2


3  

You should set width as 100% and max-width as value (600px in you case).

您应将宽度设置为100%,将max-width设置为值(在您的情况下为600px)。

This is a qucik example just to show you the right way, so now you can style it as you like.

这是一个qucik示例,只是为了向您展示正确的方式,所以现在您可以根据自己的喜好设计样式。

http://jsfiddle.net/08pyzgx4/1/

http://jsfiddle.net/08pyzgx4/1/

Remember that for scaling text, the div should always have a percentage width (and off course a max-width if you need one)

请记住,对于缩放文本,div应始终具有百分比宽度(如果需要,则偏离最大宽度)

Then just use a couple of media queries to adjust the text position on different resolutions and it should be ready

然后只需使用几个媒体查询来调整不同分辨率的文本位置,它应该准备就绪