简体   繁体   English

基于Bootstrap中容器宽度的字体缩放

[英]Font scaling based on width of container in Bootstrap

I wonder how to scale a block of text (with font-scaling) in Bootstrap Framework? 我想知道如何在Bootstrap Framework中缩放文本块(使用字体缩放)? Width of columns is the main difficulty, because it's fluid. 列宽是主要困难,因为它是可变的。

如何缩放文本块

Bootstrap use CSS @media for Grid system [ link ] so you can use media queries with Bootstrap's dimensions to set another CSS properties. Bootstrap将CSS @media用于Grid系统[ 链接 ],因此您可以使用具有Bootstrap尺寸的媒体查询来设置其他CSS属性。 Check out the fiddle and resize the bar between js and display window to see the effect of header font-size changing. 检查小提琴并调整js和显示窗口之间的栏的大小,以查看更改标题字体大小的效果。

 <div class="container"> <h1 class="header">header</h1> <div class="row"> <div class="colDiv col-sm-6 col-md-4 col-lg-3"> <p>ColumnA</p> </div> <div class="colDiv col-sm-6 col-md-8 col-lg-9"> <p>ColumnB</p> </div> </div> </div> 

EDIT: 编辑:

ok so I wrote a little little JS function to do that. 好的,所以我写了一点JS函数来做到这一点。 You set six parameters (in order) to manipulate the fontSize: 您可以设置六个参数(依次)来操纵fontSize:

alignFont(getElement,defaultSize,defaultWidth,acceleration,min,max)
  • getElement - set element the font-size you want to manipulate within getElement-设置元素要在其中操作的字体大小
  • defaultSize - the default (start) size of font defaultSize-字体的默认(开始)大小
  • defaultWidth - the width of window for which the defaultSize of font is set defaultWidth-设置了字体的defaultSize的窗口的宽度
  • acceleration - 1 is normal, 2 - increase two times faster, .5 - increase slower 加速度 -1正常,2-加快两倍,.5-缓慢
  • min - the minimal fontSize (the font size cannot be smaller than this value) min-最小fontSize(字体大小不能小于此值)
  • max - the maximal fontSize (the font size cannot be higher than this value) max-最大fontSize(字体大小不能大于此值)

And the example (resize the bar between js box and display box) : FIDDLE 和示例(调整js框和显示框之间的栏的大小)FIDDLE

  var a = document.getElementById('flexibleHeaderA'); var b = document.getElementById('flexibleHeaderB'); var c = document.getElementById('flexibleHeaderC'); var d = document.getElementById('flexibleHeaderD'); var e = document.getElementById('flexibleHeaderE'); var settingA = alignFont.bind(this, a, 24, 600, 1, 20, 30); var settingB = alignFont.bind(this, b, 24, 600, 1.5, 12, 60); var settingC = alignFont.bind(this, c, 30, 600, 1, 15, 35); var settingD = alignFont.bind(this, d, 22, 1000, .5, 12, 30); var settingE = alignFont.bind(this, e, 16, 80, 1, 12, 26); settingA(); settingB(); settingC(); settingD(); settingE(); window.addEventListener('resize', function() { settingA(); settingB(); settingC(); settingD(); settingE(); }); function alignFont(getElement, defaultSize, defaultWidth, acceleration, min, max) { var cWidth = document.body.offsetWidth; var newSize = (Math.pow(cWidth / defaultWidth, acceleration)) * defaultSize; var limitSize = newSize >= max ? max : newSize <= min ? min : newSize; getElement.style.fontSize = limitSize + "px"; } 
 h3 { border: dotted 1px #33aaff; margin: 10px; text-align: center; } 
 <h3 id="flexibleHeaderA">Hello I'm flexible header</h3> <h3 id="flexibleHeaderB">Hello I'm flexible header</h3> <h3 id="flexibleHeaderC">Hello I'm flexible header</h3> <h3 id="flexibleHeaderD">Hello I'm flexible header</h3> <h3 id="flexibleHeaderE">Hello I'm flexible header</h3> 

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

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