2017-03-01 16 views
3

CSSだけでは、1行の行を折り返さずにdivの幅と高さに合わせることが可能ですか?動的なサイズのフォントサイズ

vhとvwユニットは有望に見えますが、答えのようには見えません。

+0

なし、ありません。同様のことをするJS/jQueryプラグインがあります – Johannes

+0

良い質問はこちらhttp://stackoverflow.com/questions/16056591/font-scaling-based-on-width-of-container –

答えて

0

子要素の高さが親要素の高さに収まるまで、javaScriptを使用してフォントサイズを小さくしたり小さくしたり(大きくしたり大きくしたりする)ことができます。テキストのfont-sizeを変更し、コンテナのwidthheightを変更してください。

var samp = document.getElementById('samp'); 
 

 
fitFont(samp); 
 

 

 
function fitFont(elem){ 
 
    var child = elem.children[0]; 
 
    var getFontSize = parseFloat(window.getComputedStyle(child).getPropertyValue('font-size')); 
 
    
 
    while(child.offsetHeight>elem.clientHeight){ 
 
    getFontSize -= .1; 
 
    child.style.fontSize = getFontSize + 'px'; 
 
    if(child.offsetHeight<=elem.clientHeight){ 
 
     child.style.visibility = 'visible'; 
 
     return; 
 
    } 
 
    } 
 
    while(child.offsetHeight<elem.clientHeight){ 
 
    getFontSize += .1; 
 
    child.style.fontSize = getFontSize + 'px'; 
 
    if(child.offsetHeight>=elem.clientHeight){ 
 
     child.style.visibility = 'visible'; 
 
     return; 
 
    }  
 
    } 
 
    
 
}
#samp { 
 
    background-color:white; 
 
    width:280px; 
 
    height:100px; 
 
    border:solid 2px #33aaff; 
 
} 
 

 
#samp span { 
 
    display: inline-block; 
 
    visibility:hidden; 
 
    font-size:50px; 
 
}
<div id="samp"> 
 
    <span>text is bigger now and goes outside of div text is bigger now and goes outside of div text is bigger now and goes outside of div text is bigger now and goes outside of div 
 
    </span> 
 
</div>

0

あなたが取得するつもりだ最も近いものはVMINにVWとVHの値に外容器&テキストコンテナ、およびテキストコンテナのフォントサイズを設定しています。

それ以外

.outer { 
 
    width: 100vw; 
 
    height: 100vh; 
 
} 
 

 
.text { 
 
    width: 30vw; 
 
    height: 30vh; 
 
    border: solid 1px #269abc; 
 
    font-size: 10vmin; 
 
}
<div class='outer'> 
 

 
    <p class='text'>This is text</p> 
 

 
</div>

、JavaScriptを使用するつもりが必要だ/ jQueryの

+0

'ラッピングなし'おっとっと。短い答え - いいえ。現在できません。 CSSの変数は動作しています。 https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables – Inkdot

関連する問題