I remember sitting in the back seat of our family’s Subaru station wagon. I was six and this was long before child carseats were a thing. My dad was at the wheel and my mom played 20 Questions with me while we drove to some vacation spot I can’t even remember.
It was my mom’s turn as she and I played 20 Questions. She had an object in mind and I was asking the questions.
“Is it big?” I asked.
“Relative to what?” my mom replied.
This was the pattern throughout the entire game. I asked a question and my mom answered with a question about relativity.
“You can ask questions when it’s my turn to think of an object,” I’d say.
“If you’re asking if the thing is as big as a mountain, the answer is no. But next to ant, yes, it is big.”
This year has been a long stretch of re-learning what it means to think relatively. How long of a stretch? It’s relative, I suppose. But as 2020 comes to a close, I can almost hear my mom asking me the same question, whether it’s at work or in my personal life.
“Relative to what?” she asks.
My mind jumps to relative units in CSS when I hear the word “relative.” I’m sure many of you are the same. I reach for things like em
, rem
and %
all the time in my work. I only started using them to be cool when I first learned about them (whenever that was). I didn’t even know there was a real difference between em
and rem
. I thought they had something to do with retina screens.
Of course, that was a long time ago. (How long? It’s relative.) I now know that relative units are relative to the thing they reference. 2rem
is going to evaluate one way on a site with a root font size of 16px
and another for a site at 24px
. Same deal with percentages. 50%
inside a 400px
container means something different than 50%
inside a 1200px
container.
So, now, when I find myself assigning size values to elements, I first have a little dialogue with my mom.
“This element is 5.25em
.”
“Relative to what?” she asks.
I’ve also learned that thinking relatively requires a little perspective. And, no, this has nothing to do with CSS perspective
(although I could probably try to make that connection). Thinking in relative terms means momentarily stepping out of your own shoes and seeing things from something else’s vantage point.
I say “something” because I think about this most when writing code. Whenever I’m working on the pieces of a component, I have to be mindful of the context—or perspective—of where they sit. Why? Because the pieces mean different things in different contexts and those contexts are relative to the component that contains them.
When is an <h2>
just an <h2>
? Hardly ever. It might be the post name. Or perhaps the heading for a widget. Maybe it’s the heading for a card component. As front-enders, we name those things according to the perspective of the component. From the perspective of a post, an <h2>
means (and probably looks) something different from the perspective of, say, a card.
.post {}
.post__title {}
.widget {}
.widget__title {}
.card {}
.card__title {}
Naming things is hard. I often find myself thinking, “Ack! What the heck should I call this thing?”
“Relative to what?” my mom interjects.
I could go on and on. The truth is that thinking in terms of relativity is just as important to the code we write as it is to a game of 20 Questions, or even our personal lives. And in a year where we’ve been upended by so many competing forces, thinking along these lines can offer solace and wisdom in the midst of what has been stressful and frustrating for many of us—relatively, of course.
- “This is stressing me out.” Relative to what?
- “I have so much to do.” Relative to when?
- “I suck as JavaScript.” Relative to whom?
- “I hate the place I work.” Relative to where?
It’s easy to get caught up in absolutes. Relativity forces us to see things differently.
Thanks, your post is a thing of beauty relative to all the useful posts about css because you make a wonderful (and even more useful) connection to life. Please say thanks to your mom
It was a refreshing reading this, Geoff.
Really, somehow I was just asking myself a question (a doubt, I guess) today about work and your mom made the answer so much clear.
2020 has been extremely hard for all of us at every front, personal or professional. And now that we have endured through it (luckily? Don’t know), we all will have some doubts, some questions, but your mother’s wisdom will surely be of help to a lot of the folks reading this article (just like me).
“Relative to what?”
You missed the opportunity to mention
position: relative;
here :-)Geoff, you missed a point, as stated by the previous comment, too.
Even more, not sure if you ever wondered why when using position the people of W3C working group made us to have something as in Greek language (where Yes is actually heard as Nay and No is Ochee in other words, kinda vice-versa…. ).
This is because when we set position of an inner element, div, p, you name it, we call it “absolute” which is quite absurd if thinking that actually this element’s position is computed by the browser as RELATIVE to its container /or previous sibling/, not other way around…
And, to add insult to injury, the container is considered in today’s syntax as having a Relative position toward its child – instead of Absolute as it should….
Poor choice of language, in my own opinion, which defeats the very meaning of the semantics – which (the semantics) in my opinion might be the best way of expressing our understanding and intentions when writing code.
And if you try to tell me that naming it as Relative maybe was intended (correct!) as a way of positioning it on its own ancestor, I will tell you that in the same manner of, say, Flex, the position naming ought to having been set. In other words, one same div can have flex: 1 for its own container but also display: flex for its own children.
Both contained AND containing (others), right?
Therefore your rule of “Everything Is Relative” , unfortunately was absurdly changed into … the same plus “Or Absolute“.
It’s no wonder that sometimes (or quite often) smart people choose to invent their own languages /libraries/ when seeing such lack of care in regard of the even most fundamental concepts.
Many languages employ error handling, but look how graciously does that Go versus Node. But when creating a true [naming] error and forcing us all developers to use it in CSS day by day ?!? I find that quite outrageous :)
What do you think?
Wouldn’t be better just to put position:absolute for the parent /big brother/ and position:relative for its child /small brother/ (followed by any top or left stuff as needed, of course)?
I would say to the CSS creators a great line from a nice movie: [by calling a child Absolute instead of Relative] that was really stupid for how smart guys you are!
Thanks :)
What do I think? I’m perfectly happy with
position
keyword values. The exact semantics don’t bother me, as long as we know what our offset values are relative to and how they evaluate.