DEV Community

Cover image for CSS Tips and Tricks #01
relativelyrehan for XenoX

Posted on • Updated on

CSS Tips and Tricks #01

For the last couple of months, we at Skynox Tech's Twitter account, sharing a series of tweets known as #CSStricks. We have shared a lot of tricks that otherwise one could not find in online tutorials. Today we decided to compile all those tips and tricks under one hood so that you can find all those in a single place. These tweets will not only improve your CSS game but also helps you save a ton of codes line and help your own touch to your projects.

So here are the tips and tricks of CSS:-

1. Smooth Scrolling

When you visit some websites and try to go to different sections, it scrolls smoothly to that section. It is not some top tier coding, but just one line in your CSS can help you achieve it.

2. Truncate text using pure CSS

Sometimes the text is not consistent in cards and makes them look chaotic. Use this trick to limit the text on cards using just CSS.

3. Input Caret Color

Want to give a little personal touch to your site. Use this css trick to change the color of the input field-caret to give it a custom color of your liking.

4. Drop Shadow and Box Shadow

Do you know that you can use "Drop shadow" for the transparent images?
It will give a much better shadow effect.

5. Drop Cap Letter

You might have seen some blogs with the first letter being huge and the text is wrapped around it. That can be achieved with this simple trick on your text block.

6. Center that Div

Centering your div element can be daunting sometimes, but not when you have this trick in your toolbox. You can center any div with just a couple of lines of CSS.

7. No value, No problem

Display links when the element has no text value but the href attribute has a link. Pretty neat, right?

8. Inherit box sizing

Let "box-sizing" be inherited from HTML. This makes it easier to change "box-sizing in plugins" or other components that leverage other behavior.

Bonus tips:-

** Bonus 1: Better User Experience **

With the modern web technologies enhancements, one of the key focus points is user experience. Even if your website has the best designs but poor user experience, you will not be able to retain them. This is a cool user experience trick to mute videos on autoplay and let the user decide if they want to hear it.

autoplay

** Bonus 2: Selection pseudo class **

Most of the time when you visit any site and try to copy some text, it is just the blue color selection with white text. Use selection pseudo-class to give a personal touch to text selection on your websites. This will give them a personal touch and make your site stand out!

selection

That's it, folks!
These are the all tricks I have to share for now. If you want to see more such content and tips and tricks of other web technologies like react, ES6, etc., do follow us on Twitter at @XenoxDev. We share a lot of cool content and resources to learn web development so make sure to give a follow.

Until then 👋👋

Top comments (9)

Collapse
 
andrewbridge profile image
Andrew Bridge

Nice! I didn't know you could change the caret colour! Will use that one in the future.

I'm struggling to understand the first bonus tip though. As far as I can tell, your code will simply hide all videos that aren't muted by default, which makes them impossible to be muted or controlled by the user. Surely the solution is to ensure the muted attribute is applied in the first place?

Demo: jsfiddle.net/h7t0nsmb/1/

The other one I'm unsure of is #7. What's the scenario where you have a link with a URL but no text? Besides breaking text selection, it would also cause accessibility issues for users of screen readers, so it doesn't seem like a good idea.

Demo: jsfiddle.net/tks2uhyq/

With that said, the techniques used to achieve it are lesser known, and could be useful! So still a useful one! Thanks!

Collapse
 
aoussiadmehdi profile image
Mehdi Aoussiad

Nice tips, I didn't know about the input caret color.

Collapse
 
manasp profile image
Manas P

Me too...

Collapse
 
fullrx profile image
Dmitry Maltsev

Thanks. Helpful

Collapse
 
dome68 profile image
Domenico

Thanks, really helpful!!!

Collapse
 
amaljoy258 profile image
Amal Joy

🔥 Tips @rreel

Collapse
 
tranduclinh197 profile image
Trần Đức Lĩnh

Can you add demo in post, Dảk.

Collapse
 
omril321 profile image
Omri Lavi

Thank you! Tip #3 (Input caret color) and bonus #2 (selection pseudo class) ware new to me.

Collapse
 
techsaq profile image
Mohd Saquib

I just used custom caret color few days back 👍👍👍