Memor.の 2013-04-17 09:37 采纳率: 100%
浏览 666
已采纳

基于容器宽度的字体缩放

I'm having a hard time getting my head around font scaling.

I currently have this site with a body font-size of 100%. 100% of what though? This seems to compute out at 16px.

I was under the impression that 100% would somehow refer to the size of the browser window, but apparently not because it's always 16px whether the window is resized down to a mobile width or full blown widescreen desktop.

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.

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. (E.g in the menu on a large desktop, 22px works perfectly. Move down to tablet width and 16px is more appropriate.)

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 100px decrease in width to control the text.

转载于:https://stackoverflow.com/questions/16056591/font-scaling-based-on-width-of-container

  • 写回答

30条回答 默认 最新

  • 旧行李 2013-11-06 14:40
    关注

    EDIT: If the container is not the body CSS Tricks covers all of your options here: https://css-tricks.com/fitting-text-to-a-container/

    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.

    The values are:

    • vw (% of the viewport width)
    • vh (% of the viewport height)
    • vi (1% of the viewport size in the direction of the root element's inline axis)
    • vb (1% of the viewport size in the direction of the root element's block axis)
    • vmin (the smaller of vw or vh)
    • vmax (the larger or vw or vh)

    1 v* is equal to 1% of the initial containing block.

    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 was width, margin, or padding.

    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.

    Also, check out CSS-Tricks for a broader look: http://css-tricks.com/viewport-sized-typography/

    Here's a nice article about setting min/max sizes and exercising a bit more control over the sizes: http://madebymike.com.au/writing/precise-control-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

    Also, please view this article, which uses a technique dubbed 'molten leading' to adjust the line-height as well. https://css-tricks.com/molten-leading-css/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)