CSS : Is it possible to adapt font size to div wid

2019-02-05 23:43发布

I have a div with 70% width, and here’s what i want to do:

  • put some words in that div
  • adapt font size so that those words occupy all div width

Is this possible with only css? Here is my code:

.parent {
  width:70%;
  height:auto;
  min-height:100px;
  background:red;
  font-size:10vw;
}
Please help me to change the font-size dynamically to the parent class
<hr>
<div class="parent">
  This Text
</div>

Note: the div size is responsive, this is important. Thanks in advance!

5条回答
戒情不戒烟
2楼-- · 2019-02-06 00:08

Yes but I think you need to use JS(jQuery).

JS:

$(function(){

var box = $('.box');
var boxWith = box.width();

$('.box h1').css('font-size',boxWith);

});

https://jsfiddle.net/moongod101/sdfaatp8/

查看更多
老娘就宠你
3楼-- · 2019-02-06 00:10

Perhaps not quite what you're looking for, but something - You can make both the font and the element relative to view width.

p {
  font-size: 5vw;
  background-color: red;
  width: 50vw;
}
<p>
  I'm a P!
</p>

查看更多
时光不老,我们不散
4楼-- · 2019-02-06 00:13

You just have to add display: inline; in your code

.parent {
  width:70%;
  height:auto;
  min-height:100px;
  background:red;
  font-size:10vw;
  display: inline;
}
<!DOCTYPE html>
<html>
<head>
<style>
.parent {
  width:70%;
  height:auto;
  min-height:100px;
  background:red;
  font-size:10vw;
  display: inline;
}
</style>
</head>
<body>

<div class="parent">
  This Text
</div>

</body>
</html>

查看更多
成全新的幸福
5楼-- · 2019-02-06 00:17
**You can change font-size:10vh;**
        .parent {
      width:70%;
      height:auto;
      min-height:100px;
      background:red;
      color:white;
      font-size:10vh;
    }
    Please help me to change the font-size dynamically to the parent class
    <hr>
    <div class="parent">
      Text
    </div>
查看更多
倾城 Initia
6楼-- · 2019-02-06 00:19

Here, I Updated it.... JS Fiddel

try to use

css

.parent 
 {
      width:70%;
      height:auto;
      min-height:100px;
      background:red;
      font-size:10vw;          
  }



span{
      display:inline-block;
      transform:scale(1.8,1);
      -webkit-transform:scale(3,1);
 }

HTML

<div class="parent">
  <span>This Text</span>
 </div>
查看更多
登录 后发表回答