最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change font size based on window height - Stack Overflow

programmeradmin3浏览0评论

Is it possible to change the CSS font-size and line-height of a headline based on the height of the browser window (not the width using media queries)?

Is it possible to change the CSS font-size and line-height of a headline based on the height of the browser window (not the width using media queries)?

Share Improve this question edited Jan 23, 2015 at 16:28 James Donnelly 129k35 gold badges215 silver badges223 bronze badges asked Jan 23, 2015 at 16:09 Blake BowmanBlake Bowman 3012 silver badges14 bronze badges 4
  • Yes - use the vh unit - css-tricks./viewport-sized-typography – Rory McCrossan Commented Jan 23, 2015 at 16:10
  • 2 Self-shaming promotion: I made a plugin for that kind of thing. You can take a look if it fit your needs. github./kagagnon/Responsive-Font – Karl-André Gagnon Commented Jan 23, 2015 at 16:17
  • @Karl-AndréGagnon Can I edit the line-height and letter spacing of the font with this method as well? – Blake Bowman Commented Jan 24, 2015 at 20:56
  • @BlakeBowman Unfortunately, not yet. It's something i'm gonna do one day, but alternatively, you can set a line-height as a percent of the font-size. – Karl-André Gagnon Commented Jan 26, 2015 at 1:33
Add a ment  | 

2 Answers 2

Reset to default 7

Yes it is. You can do this with CSS alone using the vh (viewport height) unit:

vh unit
Equal to 1% of the height of the initial containing block.

font-size: 1vh;     /* Equal to 1/100th of the viewport height. */
font-size: 50vh;    /* Equal to 1/2 of the viewport height. */
font-size: 100vh;   /* Equal to the viewport height. */

The same applies to line-height:

line-height: 1vh;     /* Equal to 1/100th of the viewport height. */
line-height: 50vh;    /* Equal to 1/2 of the viewport height. */
line-height: 100vh;   /* Equal to the viewport height. */

Demo

html, body, h1 { margin: 0; }

h1 {
  font-size: 50vh;
  line-height: 100vh;
  text-align: center;
}
<h1>Hello, world!</h1>

with jquery:

$('#mySelector').css("font-size", $(window).height()/10);

demo with resize handling: http://jsfiddle/scu5Ltfh/

发布评论

评论列表(0)

  1. 暂无评论