本文翻译自:Font scaling based on width of container

I'm having a hard time getting my head around font scaling. 我很难理解字体缩放。

I currently have this site with a body font-size of 100%. 我目前有一个网站的正文font-size为100%。 100% of what though? 不过是100% This seems to compute out at 16 pixels. 这似乎可以计算出16个像素。

I was under the impression that 100% would somehow refer to the size of the browser window, but apparently not because it's always 16 pixels whether the window is resized down to a mobile width or full blown widescreen desktop. 我给人的印象是100%会以某种方式引用浏览器窗口的大小,但是显然不是因为将窗口缩小到移动宽度还是全屏宽屏桌面总是16像素。

How can I make the text on my site scale in relation to its container? 如何使网站上的文本相对于其容器而言? I tried using em , but this doesn't scale either. 我尝试使用em ,但这也无法扩展。

My reasoning is that things like my menu become squished when you resize, so I need to reduce the px font-size of .menuItem among other elements in relation to the width of the container. 我的理由是,当您调整大小时,菜单之类的东西会变得狭窄,因此我需要根据容器的宽度来减少.menuItempx font-size (For example, in the menu on a large desktop, 22px works perfectly. Move down to tablet width and 16px is more appropriate.) (例如,在大型桌面上的菜单中, 22px可以完美工作。向下移动至平板电脑宽度,则16px更合适。)

I'm aware I can add breakpoints, but I really want the text to scale as well as having extra breakpoints, otherwise I'll end up with hundreds of breakpoints for every 100 pixels decrease in width to control the text. 我知道我可以添加断点,但是我确实希望文本能够缩放以及具有额外的断点,否则我将每减少100像素的宽度就会得到数百个断点来控制文本。


#1楼

参考:https://stackoom.com/question/15N3H/基于容器宽度的字体缩放


#2楼

100% is relative to the base font size, which, if you haven't set it, would be the browser's user-agent default. 100%相对于基本字体大小,如果未设置,则为浏览器的用户代理默认值。

To get the effect you're after, I would use a piece of JavaScript code to adjust the base font size relative to the window dimensions. 为了获得想要的效果,我将使用一段JavaScript代码来相对于窗口尺寸调整基本字体大小。


#3楼

Inside your CSS, try adding this at the bottom changing the 320 pixels width for wherever your design starts breaking: 在CSS内,尝试将其添加到底部,以将320像素的宽度更改为您的设计开始中断的位置:

    @media only screen and (max-width: 320px) {body { font-size: 1em; }}

Then give the font-size in "px" or "em" as you wish. 然后根据需要在“ px”或“ em”中指定字体大小。


#4楼

EDIT: If the container is not the body CSS Tricks covers all of your options in Fitting Text to a Container . 编辑:如果容器不是正文, CSS Tricks将在“将文本适合容器”中涵盖您的所有选项。

If the container is the body, what you are looking for is Viewport-percentage lengths : 如果容器是主体,则您要查找的是视口百分比长度 :

The viewport-percentage lengths are relative to the size of the initial containing block . 视口百分比长度相对于初始包含块的大小。 When the height or width of the initial containing block is changed, they are scaled accordingly. 更改初始包含块的高度或宽度时,将对其进行相应缩放。 However, when the value of overflow on the root element is auto, any scroll bars are assumed not to exist. 但是,当根元素上的溢出值为auto时,则假定不存在任何滚动条。

The values are: 值是:

  • vw (% of the viewport width) vw (视口宽度的百分比)
  • vh (% of the viewport height) vh (视口高度的百分比)
  • vi (1% of the viewport size in the direction of the root element's inline axis) vi (在根元素的内联轴方向上的视口大小的1%)
  • vb (1% of the viewport size in the direction of the root element's block axis) vb (视口大小在根元素的块轴方向上的1%)
  • vmin (the smaller of vw or vh ) vminvwvh的较小者)
  • vmax (the larger or vw or vh ) vmax (较大或vwvh

1 v* is equal to 1% of the initial containing block. 1 v *等于初始包含块的1%。

Using it looks like this: 使用它看起来像这样:

p {font-size: 4vw;
}

As you can see, when the viewport width increases, so does the font-size, without needing to use media queries. 如您所见,当视口宽度增加时,字体大小也会增加,而无需使用媒体查询。

These values are a sizing unit, just like px or em , so they can be used to size other elements as well, such as width, margin, or padding. 这些值是大小单位,就像pxem ,因此它们也可以用于调整其他元素的大小,例如宽度,边距或填充。

Browser support is pretty good, but you'll likely need a fallback, such as: 浏览器支持相当不错,但是您可能需要一个备用,例如:

p {font-size: 16px;font-size: 4vw;
}

Check out the support statistics: http://caniuse.com/#feat=viewport-units . 查看支持统计信息: http : //caniuse.com/#feat=viewport-units 。

Also, check out CSS-Tricks for a broader look: Viewport Sized Typography 另外,请查看CSS技巧以获得更广泛的外观: 视口大小的版式

Here's a nice article about setting minimum/maximum sizes and exercising a bit more control over the sizes: Precise control over responsive typography 这是一篇有关设置最小/最大尺寸以及对尺寸进行更多控制的好文章: 精确控制响应式排版

And here's an article about setting your size using calc() so that the text fills the viewport: http://codepen.io/CrocoDillon/pen/fBJxu 以下是有关使用calc()设置大小以使文本填充视口的文章: http : //codepen.io/CrocoDillon/pen/fBJxu

Also, please view this article, which uses a technique dubbed 'molten leading' to adjust the line-height as well. 另外,请查看这篇文章,该文章使用一种称为“熔化引导”的技术来调整行高。 Molten Leading in CSS 熔融领先CSS


#5楼

But what if the container is not the viewport (body)? 但是,如果容器不是视口(主体)怎么办?

This question is asked in comment by Alex under the accepted answer . 亚历克斯在已接受的答案下发表了评论。

That fact does not mean vw cannot be used to some extent to size for that container. 这个事实并不意味着vw在某种程度上不能用于该容器的大小。 Now to see any variation at all one has to be assuming that the container in some way is flexible in size. 现在要看到所有变化,必须假定容器在某种程度上具有灵活性。 Whether through a direct percentage width or through being 100% minus margins. 通过直接百分比width还是通过100%减去边距。 The point becomes "moot" if the container is always set to, let's say, 200px wide--then just set a font-size that works for that width. 如果容器始终设置为200px宽,则该点变为“模拟”,然后设置适合该宽度的font-size

Example 1 例子1

With a flexible width container, however, it must be realized that in some way the container is still being sized off the viewport . 但是,对于宽度可变的容器,必须认识到,在某种程度上,该容器的尺寸仍然超出视口As such, it is a matter of adjusting a vw setting based off that percentage size difference to the viewport, which means taking into account the sizing of parent wrappers. 因此,要根据视口的百分比大小差异来调整vw设置,这意味着要考虑父包装的大小。 Take this example : 举个例子

div {width: 50%;border: 1px solid black;margin: 20px;font-size: 16px;/* 100 = viewport width, as 1vw = 1/100th of thatSo if the container is 50% of viewport (as here)then factor that into how you want it to size.Let's say you like 5vw if it were the whole width,then for this container, size it at 2.5vw (5 * .5 [i.e. 50%])*/font-size: 2.5vw;
}

Assuming here the div is a child of the body , it is 50% of that 100% width, which is the viewport size in this basic case. 假设divbody的子级,则为该100%宽度的50% ,这是此基本情况下的视口大小。 Basically, you want to set a vw that is going to look good to you. 基本上,您想设置一个对您来说看起来不错的vw As you can see in my comment in the above CSS content, you can "think" through that mathematically with respect to the full viewport size, but you don't need to do that. 正如您在上述CSS内容的评论中所看到的,您可以相对于整个视口大小在数学上“思考”,但是您不需要这样做。 The text is going to "flex" with the container, because the container is flexing with the viewport resizing. 文本将随着容器“弯曲”,因为容器随着视口的调整而弯曲。 UPDATE: here's an example of two differently sized containers . 更新: 这是两个大小不同的容器的示例 。

Example 2 例子2

You can help ensure viewport sizing by forcing the calculation based off that. 通过基于此强制进行计算,可以帮助确保视口大小。 Consider this example : 考虑这个例子

html {width: 100%;} /* Force 'html' to be viewport width */
body {width: 150%; } /* Overflow the body */div {width: 50%;border: 1px solid black;margin: 20px;font-size: 16px;/* 100 = viewport width, as 1vw = 1/100th of thatHere, the body is 150% of viewport, but the container is 50%of viewport, so both parents factor  into how you want it to size.Let's say you like 5vw if it were the whole width,then for this container, size it at 3.75vw(5 * 1.5 [i.e. 150%]) * .5 [i.e. 50%]*/font-size: 3.75vw;
}

The sizing is still based off viewport, but is in essence set up based off the container size itself. 大小调整仍基于视口,但本质上是根据容器大小本身设置的。

Should Sizing of the Container Change Dynamically... 容器的尺寸应动态变化...

If sizing of the container element ended up changing dynamically its percentage relationship either via @media break points or via JavaScript, then whatever the base "target" was would need recalculation to maintain the same "relationship" for text sizing. 如果容器元素的大小最终通过@media断点或JavaScript动态更改其百分比关系,则无论基本的“目标”是什么,都需要重新计算以保持相同的“关系”以进行文本大小调整。

Take example #1 above. 以上面的示例1为例。 If the div was switched to 25% width by either @media or JavaScript, then at the same time, the font-size would need to adjust in either the media query or by JavaScript to the new calculation of 5vw * .25 = 1.25 . 如果通过@media或JavaScript将div切换为25%宽度,则同时, font-size将需要在媒体查询或JavaScript中调整为新的5vw * .25 = 1.25计算。 This would put the text size at the same size it would have been had the "width" of the original 50% container been reduced by half from viewport sizing, but has now been reduced due to a change in its own percentage calculation. 这将使文本大小与将原来的50%容器的“宽度”从视口大小减小一半时的大小相同,但由于其自身百分比计算的更改而现在减小了。

A Challenge 一个挑战

With the CSS3 calc() function in use, it would become difficult to adjust dynamically, as that function does not work for font-size purposes at this time. 使用CSS3 calc()函数时,动态调整将变得困难,因为该函数目前不适用于font-size So you could not do a pure CSS 3 adjustment if your width is changing on calc() . 因此,如果您的宽度在calc()上更改,则无法进行纯CSS 3调整。 Of course, a minor adjustment of width for margins may not be enough to warrant any change in font-size , so it may not matter. 当然,对边距宽度的微小调整可能不足以保证font-size发生任何变化,因此可能没有关系。


#6楼

Try http://simplefocus.com/flowtype/ . 尝试http://simplefocus.com/flowtype/ 。 This is what I use for my sites, and it has worked perfectly. 这是我在网站上使用的,并且运行良好。

基于容器宽度的字体缩放相关推荐

  1. css圆在中心根据宽度缩放_根据CSS中的容器宽度重新缩放字体

    css圆在中心根据宽度缩放 Introduction: 介绍: Dealing with fonts is a very interesting thing to do as fonts bring ...

  2. html文字大小自动适应宽度,三种字体大小自适应容器宽度的方法

    618项目中遇到过这样一个问题,移动端各种机型屏幕宽度,各页面中的标题字数是不定的,设计师根据375宽的屏设计的字体大小为20px,在iPhone5中320的屏宽下某些页面由于标题文字长了些就出现了标 ...

  3. konva文字大小自适应容器宽度

    简单记录konva字体缩放的使用方法 //Konva使用的基本案例 //第一步:创建舞台 var stage = new Konva.Stage({container: 'container', // ...

  4. java 字体宽度_Java字体大小从宽度

    我正在寻找一种从宽度推断Java AWT字体大小的方法.例如,我知道我想在100像素内写'hello world'.我知道我在Font.PLAIN风格中使用字体"Times",我希 ...

  5. 基于Python使用ffmpeg批量缩放图片

    基于Python使用ffmpeg批量缩放图片 一.前言 ​ 笔者因为项目原因或者个人撰文需要,经常要写大量技术文档,文档中通常需要配图,但配图就有一个比较讨厌的问题:截图大小不一,宽度通常要手动调整, ...

  6. JAVA 使用Itext模板生成pdf,解决图片插入,文本域超出字体缩放,半自动换行

    1.前言 前一段时间遇到一个制作Pdf的业务,自己下来摸索了一下,基本上解决.将其中遇到的几个问题及解决方法做以记录,仅供大家参考. 首先在这里对于刚接触该类型业务的同学说明下,ItexPdf支持使用 ...

  7. SVG —— 基于XML语法的可缩放矢量图形

    SVG -- 基于XML语法的可缩放矢量图形 SVG Scalable Vector Graphics 可缩放的矢量图形,基于XML语法的可缩放矢量图形 JPG PNG canvas的图片都是位图,放 ...

  8. docker制作镜像篇(基于容器)

    docker制作镜像可以有两种方式: 一.基于容器(使用busybox制作http镜像) 1.首先运行一个容器 2.在容器当中配置自己的http,添加web目录,增加主页文件等. 3.查看原busyb ...

  9. 汉得宣布开源:基于容器的企业级应用 PaaS 平台

    开发四年只会写业务代码,分布式高并发都不会还做程序员? >>>   2018年5月20日,Choerodon猪齿鱼正式发布 0.5.0 版本,同时汉得公司宣布Choerodon猪齿鱼 ...

最新文章

  1. matlab 如何被c 调用函数调用函数调用,c 调用 matlab engine 自定义函数
  2. 函数指针,以及用函数指针的好用之处(回调函数)
  3. 地图画指定区域_零基础学CAD绘制一张桌子为例,使亲们更好地熟悉三维绘图环境...
  4. [leetcode] 68.二叉树的最近公共祖先
  5. 如何使用示例从Java中的类路径加载资源
  6. linux中网页播放音乐,Linux_在Linux系统下播放网页中的背景音乐技巧,在Linux中的firefox浏览许多网页 - phpStudy...
  7. Arduino笔记-调节呼吸灯频率实验
  8. java远程关机_通过jsch实现对linux服务器的shell客户端远程控制关机完整示例代码分享...
  9. Python——字典生成式
  10. HashSet和HashMap的区别 1
  11. PaddleNLP Taskflow
  12. Linux内核 之 menuconfig各个选项介绍[转]
  13. WPS简历模板的图标怎么修改_重装后这桌面图标谁看得见啊?教你怎么修改桌面图标大小...
  14. Android—Gradle教程(三)
  15. 计算机路由器无线级联配置,不同品牌无线路由器 无线级联 配置案例
  16. 计算机图形学-五角星的画法(转)
  17. windows xp系统安装教程
  18. 服务器计算技术解决远程接入速度困扰
  19. 感知机学习:鸢尾花二分类
  20. 分割 fasta 文件

热门文章

  1. 一个进程仅存在给定类型的一个挂起信号,同一进程同样类型的其他信号不被排队
  2. Android老項目出现javax/xml/bind/JAXBException异常问题解决
  3. 让我来教你 PHP 函数调用
  4. Android.mk 文件语法详解
  5. Android 之surfaceView (画动态圆圈)
  6. Java网络编程笔记1
  7. (0050)iOS开发之钥匙串存储
  8. swift_028(Swift 的协议)
  9. swift_014(Swift 的控制流)
  10. 开发过程中快速抓包并解析