<div class="wrapper">
<div class="sidebar">
<ul>
<li><li>
<li><li>
<li><li>
<li><li>
<li><li>
<li><li>
<li><li>
</ul>
</div>
<div>
<h1 class="title">Change the browser's font size</h1>
<p>notice how the breakpoint scales with browser setting</p>
<p class="desc">Window width: <span id="js-width"></span></p>
<div class="rem-container">
<h2 class="rem">One REM is this many pixels:</h2>
<span class="box"></span>
<span class="box-size"></span>
</div>
</div>
</div>
// define root font size here, it is assumed that most browsers default to 16px as their default font size
$html-font-size: 16px;
@function stripUnit($value) {
@return $value / ($value * 0 + 1);
}
@function rempx($pxValue) {
@return #{stripUnit($pxValue) / stripUnit($html-font-size)}rem;
}
html, body {
// important not to reassign 1rem to 10px
// the font-size must stay 100% (or just not declared at all)
// font-size: 100%;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
font-family: Arial, Helvetica, sans-serif;
min-height: 100vh;
background: #C5AAB7;
display: grid;
place-items: center;
align-content: center;
justify-items: center;
transition: grid .5s, background .3s ease, color .3s;
font-size: 1rem;
grid-template-columns: 200px 1fr;
column-gap: rempx(20px);
// *********************
// you write a pixel value, get a scaling rem value
@media only screen and (min-width: rempx(1000px)) {
// *********************
grid-template-columns: min(#{rempx(500px)}, 30%) 1fr;
background: #496DDB;
color: white;
}
}
.title {
font-size: 2em;
margin-bottom: rempx(8px);
}
.title + p {
margin-bottom: rempx(16px);
}
.desc {
font-size: 1em;
margin-bottom: rempx(60px);
}
.rem {
font-size: 1.5em;
margin-bottom: rempx(6px);
}
.sidebar {
height: 100vh;
width: 100%;
ul {
display: flex;
flex-direction: column;
height: 100%;
justify-content: stretch;
align-items: stretch;
li {
height: 100%;
&:nth-of-type(odd) {
background: white;
}
}
}
}
.rem-container {
display: flex;
align-items: center;
width: fit-content;
}
.rem {
font-size: 1em;
}
.box {
display: inline-block;
width: 1rem;
height: 1rem;
background: black;
color: white;
margin-left: rempx(20px);
font-size: 1rem;
}
.box-size {
margin-left: rempx(8px);
display: inline-block;
}
View Compiled
const width = document.querySelector('#js-width');
const box = document.querySelector('.box');
const boxSize = document.querySelector('.box-size');
const calculateWidth = () => {
width.innerText = `${parseInt(window.innerWidth)} px`;
}
calculateWidth();
window.onresize = calculateWidth
boxSize.append(Math.round(box.getBoundingClientRect().width))
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.