Is it possible to adapt font size to div width with CSS?

前端 未结 5 1587
刺人心
刺人心 2020-12-15 17:47

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
相关标签:
5条回答
  • 2020-12-15 18:05

    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/

    0 讨论(0)
  • 2020-12-15 18:09

    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>
    
    0 讨论(0)
  • 2020-12-15 18:17

    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>

    0 讨论(0)
  • 2020-12-15 18:19

    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;
    }
    <div class="parent">
      This Text
    </div>

    0 讨论(0)
  • 2020-12-15 18:22
    **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>
    
    0 讨论(0)
提交回复
热议问题