Created
March 25, 2019 20:29
-
-
Save osvik/bf1682b7c72baec3a4ece37b79dcb92e to your computer and use it in GitHub Desktop.
Responsive elements based on parent column width
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> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Responsive elements based on parent column width</title> | |
</head> | |
<body> | |
<h1>Responsive elements based on column width</h1> | |
<!-- START FLEXIBLE CONTAINER --> | |
<style> | |
.f0t434 { | |
font-size: 14px; | |
} | |
.f435t619 { | |
font-size: 20px; | |
} | |
.f620t877 { | |
font-size: 30px; | |
} | |
.f878tmax { | |
font-size: 42px; | |
} | |
</style> | |
<div id="flexibleContainer"> | |
This is a test | |
</div> | |
<script> | |
/** | |
* Adds a class accordingly to colum width | |
*/ | |
document.addEventListener("DOMContentLoaded", function(t) { | |
var containerEl = document.getElementById("flexibleContainer"); | |
var elwidth = containerEl.offsetWidth; | |
if (elwidth < 435) { | |
containerEl.classList.add("f0t434"); | |
} else if (elwidth >= 435 && elwidth < 620) { | |
containerEl.classList.add("f435t619"); | |
} else if (elwidth >= 620 && elwidth < 878) { | |
containerEl.classList.add("f620t877"); | |
} else if (elwidth >= 878) { | |
containerEl.classList.add("f878tmax"); | |
} | |
}); | |
</script> | |
<!-- END FLEXIBLE CONTAINER --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment