Created
July 2, 2017 17:45
-
-
Save duderamos/5d31b10ce1363f4581576b0dd7a3c9f1 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<head> | |
<title>autosizing</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<style> | |
div { border: 1px solid #aaa; } | |
.w70 { width: 70%; } | |
.w20 { width: 20%; } | |
</style> | |
</head> | |
<body> | |
<div class="w70"> | |
<span class="autosize"> | |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent imperdiet enim eu tellus porttitor, vel efficitur libero faucibus. Quisque magna dolor, cursus id justo vel, lobortis vestibulum ligula. Donec efficitur egestas odio, non convallis sapien vestibulum eget. Fusce scelerisque mauris urna, et pretium erat faucibus posuere. | |
</span> | |
</div> | |
<div class="w20"> | |
<span class="autosize"> | |
[email protected] | |
</span> | |
</div> | |
<script> | |
$(".autosize").each(function() { | |
var textbox = $(this); | |
var container = textbox.parent(); | |
var fontSize = parseInt(textbox.css("font-size")); | |
while(textbox.width() > container.width()) | |
textbox.css("font-size", fontSize -= 0.5); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source https://stackoverflow.com/a/28485608