Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

Solving All CSS Layout Issues — any screen, any root font size, without JS

Joana Borges Late
Level Up Coding
Published in
20 min readMar 13, 2023

Photo by Hal Gatewood on Unsplash

Convention

Foreword

It is just fluid boxes inside stacked up divisions!

The layout in pixels

Problems of the layout in pixels

What if the root font size was already doubled (or tripled)?

The VERY scary voyage to the realm of the REM

“1rem” means a constant *number of pixels* - that is unpredictable by the developer.

Keep thinking “px” but run “rem”

<style>
.my-class {
width: calc(320rem / 16);
}
</style>

Why the IPhone 5 examples?

Come on… who uses “rem”?

Inspecting a MDN webpage

So… it is just to use “rem” instead of “px” for layout?

The solution

DISCLAIMER

The CSS template

<style>
.-division {
min-height: 2rem;
display: inline-flex;
flex-direction: row;
padding: 20px 0;
flex-wrap: wrap;
justify-content: space-around;
row-gap: 20px;
}
.-box {
min-height: 2rem;
max-width: min(100%, 50rem);
align-self: stretch;
padding: 15px 5px;
overflow: auto;
font-size: 1rem;
}
</style>
<style>
/* px based */
@media (min-width: 360px) {
.-box {
padding-left: 10px;
padding-right: 10px;
}
}
@media (min-width: 412px) {
.-box {
font-size: 1.0625rem;
}
}
@media (min-width: 480px) {
.-box {
font-size: 1.125rem;
}
}
@media (min-width: 560px) {
.-box {
font-size: 1.1875rem;
}
}
@media (min-width: 640px) {
.-box {
padding: 20px 3%;
}
}
@media (min-width: 640px) {
.-box {
font-size: 1.25rem;
}
}
</style>
<style>
/* rem based */
@media (min-width: 50rem) {
.-thin2, .-thin3, .-thin4 {
width: 45%;
}
}
@media (min-width: 75rem) {
.-thin3 {
width: 30%;
}
}
@media (min-width: 100rem) {
.-division {
padding-top: 30px;
padding-bottom: 30px;
row-gap: 30px;
}
.-thin4 {
width: 22.5%;
}
}
</style>
<style> 
/* part of the 'reset' template */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
div {
display: inline-block;
width: 100%;
}
</style>

Writing CSS — the golden rules

Divisions and boxes

Thin box, fat box

The samples

Single box division

Layout for a single box
<div class="-division -navy">
<div class="-box -dash-grey"></div>
</div>

Two box division

Layout for 2 boxes
<div class="-division -navy">
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
</div>

Three box division — A

Layout for 3 boxes — A
<div class="-division -navy">
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
</div>

Three box division — B

Layout for 3 boxes — B
<div class="-division -navy">
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
</div>

Four box division — A

Layout for 4 boxes - A
<div class="-division -navy">
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
</div>

Four box division — B

Layout for 4 boxes - B
<div class="-division -navy">
<div class="-box -dash-grey -thin4"></div>
<div class="-box -dash-grey -thin4"></div>
<div class="-box -dash-grey -thin4"></div>
<div class="-box -dash-grey -thin4"></div>
</div>

Five box division — A

Layout for 5 boxes — A
<div class="-division -navy">
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
</div>

Five box division — B

Layout for 5 boxes — B
<div class="-division -navy">
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
</div>

Six box division — A

Layout for 6 boxes — A
<div class="-division -navy">
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
<div class="-box -dash-grey -thin2"></div>
</div>

Six box division — B

Layout for 6 boxes — B
<div class="-division -navy">
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
<div class="-box -dash-grey -thin3"></div>
</div>

Seven (or more) box division

Acknowledging the device screen

@media (min-width: 640px) {
.-box {
font-size: 1.25rem;
}
}

The width of the single box

max-width: min(100%, 50rem);

No to margins - Yes to paddings and row gaps

.-division {
min-height: 2rem;
display: inline-flex;
flex-direction: row;
padding: 20px 0;
flex-wrap: wrap;
justify-content: space-around;
row-gap: 20px;
}
@media (min-width: 100rem) {
.-division {
padding-top: 30px;
padding-bottom: 30px;
row-gap: 30px;
}
}
@media (min-width: 50rem) {
/* 2 thin boxes per row */
.-thin2, .-thin3, .-thin4 {
width: 45%;
}
}
@media (min-width: 75rem) {
/* 3 thin boxes per row */
.-thin3 {
width: 30%;
}
}
@media (min-width: 100rem) {
/* 4 thin boxes per row */
.-thin4 {
width: 22.5%;
}
}

Small screen, big font size

Iphone5 screen —64px root font size

Having all boxes of the row with the same height

And how about the content inside the boxes?

So far, so good…

Breaking the sacrosanct “rem” based layout

Setting font sizes in Chrome
Breaking the “rem” based layout

Setting the minimum font size of the browser to a greater value than the value of its default font size, breaks a “rem” based layout because the text grows while everything else don’t — even media queries ignore that the default font size became obsolete.

The root font size and more browser nonsense

<style>
/* DON'T use this! Respect people with sight issues */
:root {
font-size: 20px;
}
</style>

Developing for the web as you wish

Web As You Wish — webpage editor

Stay tuned

Level Up Coding

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (6)