DEV Community

Cover image for 5 CSS tips you didn't know you needed

5 CSS tips you didn't know you needed

Lynne Finnigan on November 28, 2018

Having worked in web development agencies over the years, I've picked up some tips and tricks along the way. There are some things that I use day t...
Collapse
 
quantumsheep profile image
Nathanael Demacon • Edited

The most useful trick I use in every project is:

* {
  box-sizing: border-box;
}
Enter fullscreen mode Exit fullscreen mode

Which prevent breaking box dimensions with margin, padding or borders.

Collapse
 
javamajk profile image
JavaMajk • Edited

Yup, ..to be extra safe :D


  html {
    box-sizing: border-box;
  }
  *, *:before, *:after {
    box-sizing: inherit;
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonny_goodwin profile image
Jonny Goodwin πŸš€ • Edited

I think that using box-sizing: inherit;’ can be bad. If you do this, and decide to change box-sizing on an element, all of that elements children will also have their box-sizing changed.

Unless you are sure you want that to happen, it’s probably best just to use β€˜box-sizing: border-box;’ to ensure that all elements use β€˜border-box’. If any elements need a different box-sizing, you would override it with more specificity.

Thread Thread
 
javamajk profile image
JavaMajk

Yeah indeed, things can get messy in this case.

Thread Thread
 
dan503 profile image
Daniel Tonon

I was a box-sizing: inherit; person, then I realised this is super useful:

.page-restrictor {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  box-sizing: content-box;
}

It means that when the screen is less than 1240px wide, it will have a 20px gap down either side of the screen with no need for media queries.

This technique doesn't work with box-sizing: inherit; though.

Collapse
 
giacomosorbi profile image
Giacomo Sorbi • Edited

Mh, the * selector is relatively very expensive: I would use it rather sparingly.

Thread Thread
 
equinusocio profile image
Mattia Astorino

No. The * is the less expensive. Check some talk about performances by csswizardry

Thread Thread
 
giacomosorbi profile image
Giacomo Sorbi

Googling quickly, they still seem to confirm it among the less efficient: csswizardry.com/2011/09/writing-ef...

Thread Thread
 
equinusocio profile image
Mattia Astorino • Edited

8 years old post...

Thread Thread
 
giacomosorbi profile image
Giacomo Sorbi

This one is much more resent and confirms that * is still more expensive: sitepoint.com/optimizing-css-id-se...

Collapse
 
quantumsheep profile image
Nathanael Demacon

It seems like the same thing? :(

Thread Thread
 
equinusocio profile image
Mattia Astorino • Edited

No. The second snippet allows you to define a global box-sizing but let elements to inherit or change the behaviour

Thread Thread
 
quantumsheep profile image
Nathanael Demacon

Oh ok! So your way is better for sure!

Thread Thread
 
4rontender profile image
Rinat Valiullov

It's old trick, man

box-sizing

Collapse
 
sduduzog profile image
Sdu

This should be default

Collapse
 
deadcoder0904 profile image
Akshay Kadam (A2K)

Thank you, Lynne, for that wonderful multi-line effect.

I saw it somewhere but forgot about it. Soon I'll use it on my next project.

I've made some Pens for practicing these & to simply copy-paste next time I use it. Of course, I've given credits to you on top of each CSS file. Here are the links -

1) Transitions - Button & Link

2) Background Overlay

3) Multi Line Underline Effect

4) Sticky Elements

5) Prevent Highlighting

Collapse
 
lynnewritescode profile image
Lynne Finnigan

Great, thanks!

Collapse
 
sh4hids profile image
Shahidul Islam Majumder

Multi line hover effect is really cool. Thanks for sharing the tricks.

Collapse
 
lynnewritescode profile image
Lynne Finnigan

No problem, I really like the effect too! Hopefully some of them come in handy :)

Collapse
 
lexlohr profile image
Alex Lohr

A CSS trick with an effect on JS: pointer-events: none will remove an element from the DOM event tree even if it is in the foreground, which means that if you capture click events, you'll receive the parent. If you are using event.target, you'll save yourself some steps of filtering.

Interesting fact I found out: if you add text nodes via appendChild, some browsers will handle them as own elements inside the DOM event tree.

Collapse
 
equinusocio profile image
Mattia Astorino

you need always to double check with js because removing pointer-events: none the element will be targettable even if it is eg. disabled.

Collapse
 
leob profile image
leob

Never ceases to amaze me how much "stuff" there is in CSS, it's endless. Implementing a CSS processing/browser rendering engine must be one of the most dauntingly complex pieces of engineering you can imagine.

Collapse
 
lynnewritescode profile image
Lynne Finnigan

I know, right?!

Collapse
 
leob profile image
leob • Edited

I saw a statement recently that "CSS is the most complex programming language that exists" ... I don't know if it can really be considered a programming language (in the Turing sense), but it is really insanely complex, especially because of its long history and never ending evolution, which means that there are always at least half a dozen different ways to do something.

Anyway, some really great tips and techniques here, thanks for the article! "sticky" is especially a cool one, and the transition tip is simple but very useful.

Collapse
 
puremana profile image
Jeremy

Have been using CSS for a long time now and I had never come across sticky or user select none before!

Thanks for that ☺

Collapse
 
lynnewritescode profile image
Lynne Finnigan

You're welcome :)

I think for a while position sticky didn't have much browser support, but now it does other than IE11. At least because it's css, it doesn't break anything in IE11, it simply just doesn't stick!

Collapse
 
allanjeremy profile image
Allan N Jeremy

The pseudo-selector background overlay is neat! Knew about pseudo-elements but never occurred to me to use them for the background overlay. Always added that extra inner div. Time to refactor!

The underline one was pretty cool too. Heck, they were all interesting in their own way.

Thanks for sharing Lynne

Collapse
 
lynnewritescode profile image
Lynne Finnigan

I'm glad it helped! These are things I've discovered along the way, thought it would be good to share :)

Collapse
 
thobyv profile image
Thoby V ijishakin

Wow awesome tips. Thanks for sharing.

Collapse
 
lynnewritescode profile image
Lynne Finnigan

No problem! Hope they come in handy :)

Collapse
 
jwesorick profile image
Jake Wesorick

Pretty sure you're not supposed to prevent highlighting as thats there for people with disabilities.

Collapse
 
lynnewritescode profile image
Lynne Finnigan • Edited

When I had to use this it was to prevent the highlighting when you clicked multiple times on a carousel arrow really fast. I wouldn’t apply it to text or anything like that - I would have no reason to.

I can’t find much info on this specific property and accessibility issues, where did you find that information?

Collapse
 
jwesorick profile image
Jake Wesorick

I am wrong. I was thinking of this: a11yproject.com/posts/never-remove.... As long as it still has an outline when it's focused you're safe.

Thread Thread
 
lynnewritescode profile image
Lynne Finnigan

It was worth checking again, considering how screenreaders work! Thanks for sharing the issue with removing the outline on focus πŸ™‚

Collapse
 
apixelvisuals profile image
APixel Visuals

Can also be used to prevent selection of images by accident.

Collapse
 
darksmile92 profile image
Robin Kretzschmar

user-select was new to me, thanks for sharing this! :)

Collapse
 
lynnewritescode profile image
Lynne Finnigan • Edited

No problem, it was a strange one at the time, took a while to find a solution!

Collapse
 
janphkoch profile image
Jan-Philipp

Thanks for the tipps!

Just starting out on Web Dev. I will copy your blending for the links this evening on my project :)

Collapse
 
lynnewritescode profile image
Lynne Finnigan

Awesome! Let me know what you think :)

Collapse
 
azkar_moulana profile image
Azkar Moulana • Edited

position: sticky is your friend.

Not supported by all the legacy browsers. Even chrome gives the partial support. But a very handy style rule, browser supports will be granted in near future.

Collapse
 
asto profile image
astodev

Here is a nice CSS tip (or CSS idea) for mobile design that a co-worker showed me today:

On mobile make your buttons wide so they're easy to click.

Collapse
 
shubhambattoo profile image
Shubham Battoo

Really cool tricks.

Collapse
 
lynnewritescode profile image
Lynne Finnigan

Thanks! :)

Collapse
 
gzamann profile image
Gulshan Zaman Khan

that's some really cool trickrey πŸ˜ƒ well, for background overlay example, I use blend-mode which is really easy. And for the text underline I've been using box-shadow but that hover effect is really cool! I'll try that one.

Collapse
 
dailymack profile image
Mr Daily

Cool

Collapse
 
dan503 profile image
Daniel Tonon

Hot tip:

transition: 0.2s;

=

transition: all 0.2s ease;

😁

Collapse
 
anurella profile image
Anurella

thank you

Collapse
 
lynnewritescode profile image
Lynne Finnigan

You’re welcome 😊

Collapse
 
phr3nzy profile image
Osama Adil

Beautiful! I was looking for these tips for a while now. Thanks!

Collapse
 
pflash profile image
Precious adeyinka

wow, thank you so much for this article it came in handy, I learnt newer features of css.

Collapse
 
lynnewritescode profile image
Lynne Finnigan

Glad you found it useful! 😊

Collapse
 
pflash profile image
Precious adeyinka

Yeah I did :) merry Christmas πŸŽ„

Collapse
 
codejoywins profile image
Max Jann

hey thanks i like the time delayed underline on hover

Collapse
 
flaredev profile image
Flare

a big thank you for the writer, i learned new things from this article.

Collapse
 
lynnewritescode profile image
Lynne Finnigan

That’s great to hear! Thanks :)

Collapse
 
peterdavidcarter profile image
Peter David Carter

I learned so much! πŸ™ƒ

Collapse
 
lynnewritescode profile image
Lynne Finnigan

Glad to hear it! :)

Collapse
 
maximesimoncelli profile image
Maxime Simoncelli

Really love that multiline effect, thanks for sharing !

Collapse
 
emranweb profile image
Emran

Thanks. for share your experience.

Collapse
 
lynnewritescode profile image
Lynne Finnigan

No problem!

Collapse
 
mathiu profile image
Mathiu

This is awesome, I actually never saw proper text underline with hover, it works even when you change line-height and have more than 2 lines of text.

Thanks for sharing!

Collapse
 
lynnewritescode profile image
Lynne Finnigan

Thanks :) glad to share!

Collapse
 
mlevesquedion profile image
mlevesquedion

For that last one (prevent highlighting), if you're using bulma you can use the "is-unselectable" class.