DEV Community

Cover image for My 5 favorite CSS tricks I learned this year

My 5 favorite CSS tricks I learned this year

Uriel Bitton on June 27, 2020

This year has been very productive for me as i developed numerous UI/UX apps and websites. On the way i've picked up some useful tricks to enhance ...
Collapse
 
ben profile image
Ben Halpern

box-sizing: border-box; is a life changer πŸ˜„

Collapse
 
viallymboma profile image
vially

Most definitely...I learnt it yesterday...and it completely changed my level of understanding of CSS and HTML...was really great

Collapse
 
urielbitton profile image
Uriel Bitton

Haha I know right!

Collapse
 
yannik_sc profile image
Yannik_Sc

A tip for vertical align: although transform/translate works, I would not recommend it for frequent centering as it forces the GPU to render your element. This is especially bad if you have many child elements.
A better way is to use display: flex with justify-content and align-items.

Collapse
 
squidbe profile image
squidbe • Edited

Beat me to the punch. 😊
To the OP, note that align-items is for vertical alignment while justify-content is for horizontal. This is assuming your flex-direction is "row".

Collapse
 
ozanbolel profile image
Ozan Bolel • Edited

I wouldn't recommend vertically centering a div by position absolute. Often times browsers render it blurry. You should use flexbox instead, it's easier and reliable. If you are not able to use flexbox due to browser support, in my opinion, display table is a better solution.

Collapse
 
urielbitton profile image
Uriel Bitton

why do they render absolute position blurry. Do you have a sources to back this up?

Collapse
 
rshelnutt profile image
Rob • Edited

This is because of subpixel rendering from the browser when using the transform property - it's actually a longstanding "issue" faced by front-end devs for years (albeit, working as intended). When working in subpixel ranges (1.875px, 10.5px, etc) with transforms, the browser calculates the draw differently than flex.

This method works ok for images and other media elements that will receive some sort of rendered anti-aliasing from the browser naturally, but when it comes to pixel perfect UI elements (borders, buttons, fonts etc) that anti-aliasing is performed on the entire element, again, when it shouldn't be. Some tricks like using the transform z-scaling, transform rotate with fractional degrees, and backface-visibility:hidden can sometimes mitigate the issue, but its pretty hit-or-miss and isn't always supported across all browsers.

TLDR; Use transformation centering for media elements and select structure layouts, it's great to have in your toolkit. Items containing UI elements are more safely centered vertically with flex.

Collapse
 
louislam profile image
Louis Lam

It is not caused by position absolute. But css transform makes elements blurry in some conditions.

stackoverflow.com/questions/320345...

Collapse
 
pavelloz profile image
PaweΕ‚ Kowalski

I dont know about blurriness, but flexbox is the way to go.

Collapse
 
urielbitton profile image
Uriel Bitton • Edited

youre right flexbox works better. But i learned this trick a while ago and its helped me a lot so thats why I mentioned it :)

Collapse
 
uxguy profile image
UX-Guy

For the vertical alignment, you mistakenly transform translateX, instead of Y.
.container {
position:absolute;
top: -50%;
transform: translateY(50%);
margin: auto;
}

Collapse
 
urielbitton profile image
Uriel Bitton

Ah yes thanks for pointing out! I will edit it :)

Collapse
 
alvaromontoro profile image
Alvaro Montoro

Also, those values will push the .container outside of the screen. I should be:

    top: 50%;
    transform: translateY(-50%);
Thread Thread
 
urielbitton profile image
Uriel Bitton

Right i believe i edited that :)

 
vyckes profile image
Kevin Pennekamp

It depends. Yes calc() is more expensive to run, but most browsers are shipped with highly optimized engines. On the other end, shipping more CSS to users impacts the loading time of the website. It's a balancing act for the users!

Thread Thread
 
urielbitton profile image
Uriel Bitton

of course. Usually one doesn't use calc for widths on all elements. I use it like max 2-3 elements per stylesheet

Collapse
 
iotapp profile image
Derek Flynn

Hey, thanks for these tips! I didn’t know you could assign CSS variables 0_o

Collapse
 
vyckes profile image
Kevin Pennekamp

CSS variables and calc together are quite powerful! You can achieve fluidity with them

Collapse
 
urielbitton profile image
Uriel Bitton

nice article !

Collapse
 
urielbitton profile image
Uriel Bitton

You're welcome man. Enjoy!

Collapse
 
jrmoynihan profile image
jrmoynihan

Two words for you:
1) SCSS
2) Grid

Collapse
 
wulforr profile image
Shaurya Vardhan Singh

For vertical alignment make sure that parent element has position : relative

Collapse
 
jpalala profile image
jose palala

You could also use flexbox to center once "arranged" by column