Created
August 28, 2021 09:02
-
-
Save A973C/3caeaf6819495e2efdc61e0269571f8e to your computer and use it in GitHub Desktop.
FlexText - Responsive font size based on container DIV size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="flexFont"> | |
<h2>This is FlexText:<br>Autoscaling by DIV width</h2> | |
<div class='smaller'>... scaled by 0.7em</div> | |
</div> | |
<div class="flexFont"> | |
<p>Font size scales parallel to the container width.<br>Change window size to test.<p> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flexFont = function () { | |
var divs = document.getElementsByClassName("flexFont"); | |
for(var i = 0; i < divs.length; i++) { | |
var relFontsize = divs[i].offsetWidth*0.05; | |
divs[i].style.fontSize = relFontsize+'px'; | |
} | |
}; | |
window.onload = function(event) { | |
flexFont(); | |
}; | |
window.onresize = function(event) { | |
flexFont(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@font-face { | |
font-family: "San Francisco"; | |
font-weight: 200; | |
src: url("//applesocial.s3.amazonaws.com/assets/styles/fonts/sanfrancisco/sanfranciscodisplay-thin-webfont.woff2"); | |
} | |
body, html { | |
height:100%; | |
width:100%; | |
font-family: "San Francisco"; | |
font-weight: 200 | |
} | |
.flexFont { | |
height:8em; | |
width:75%; | |
background-color:#eeeeee; | |
padding:1%; | |
margin: 10px; | |
} | |
.smaller { | |
font-size: 0.7em; | |
background-color:red; | |
width: 10em; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment