10 Practical CSS Tricks every developer should know

CSS Tricks & Tips

CSS is quite unpopular, might look boring and very few people are able to master it the right way. However, it is a skill that can give you a great competitive advantage against other developers. If you like solving puzzles, then you will find joy in writing CSS by looking at it as a puzzle to solve.

One of the (good) side effects of being fluent in CSS is that your html becomes smaller – you won’t need as much nodes to create a certain look. Also, you will find it a lot easier to make not only your websites but also your apps responsive. In the end, you will save a lot of time, headache and discussions with designers.

Keeping all of this in mind, we have created a list of 10 practical CSS tips which you can use in your everyday life. We are sure you will find their application in a lot of situations. Let’s dive right in then.

10 Practical CSS Tricks you should know

CSS Trick #1 – Adding … when text is too long

We have all been there – finished our nice looking design just to discover that when real data comes in, the text simply doesn’t fill in the space assigned to it. Long titles of products in an ecommerce store is a common example. Having the text over two lines will not always align with the design since that will leave us with no space for description. Check the example in the image below: 

Text too long

I would rather prefer a design like the one shown below. Here, we have enough room to place a short description, or even a buy button. 

CSS Trick #1 - Adding … when text is too long

To achieve this, we need a combination of 3 properties:

You can view example below:

CSS Trick #2 – Adding a shadow

One of the biggest advantages of CSS is that your design looks more professional. You might have spent 150 hours on a project but if you work on it just an extra 5 hours adjusting the look, your effort is visible right away. This gives you an advantage when looking for better clients or better jobs as you deliver higher quality. Shadows are one of those subtle effects that will increase your quality instantly. 

One of the most simple yet powerful effects is box-shadow. The best part is you can do it using a single property. Check the example below to see how effective this is:

CSS Trick #2 - Adding a shadow

 Now, let’s look at the values box-shadow uses:

As an extra advice, the key to have nice subtle shadows is to use rgba values instead of a regular #hex rgb. This is because with rgba values we can make our shadows have an opacity which will make them more subtle and will look a lot more professional.

Box shadows with and without RGBA

CSS Trick #3 – SVG Alignment and Color

Icons are used everywhere nowadays, be it menu icons, cart icons, favourite icons and a variety of others. We run into problems with icons since we need to have our pages work over multiple screens with multiple resolutions. SVGs come to your rescue in such scenarios! 

SVGs are perfect for icons since they look great on every resolution. However, if you want to add SVGs as the source of an image tag, there are several things you will be unable to do, like control the color. So, the best way to deal with an SVG file is to simply copy the content of the file and place it directly into the html code.

One quick way to do that is to go to a website like fontAwesome, search for the icon you like and download the file. Once you download the file, you can view the source and paste the content in your html like this:

<b class="favorite-icon"><svg>.....</svg></b>

Having done that, let’s now go ahead to create a 30×30 circular button and center our icon inside it:

The result is the following:

CSS Trick #3 - SVG Alignment and Color

Explanation:

SVGs can be colored via css which means we can use hover selectors to change the color of the icon. However, icons must be prepared for that, where the svg file typically uses fill=”currentColor”. Fortunately, icons available at fontawesome and similar places exhibit this behaviour out-of-the-box. 

Another handy tip is to use “transform: translate” to move a node according to the size of the node itself and not its parent as “top, left” properties do. Basically, we position the icon in the center, and use transform to center it using the size of the icon.

CSS Trick #4 – Perfect your Buttons

There are multiple ways of achieving the same output in CSS as is the case with any programming language. This means there might always be a better way of doing the same thing and achieving an equivalent result. 

Buttons are one of the elements which are used over almost every single page. In some cases, they even contain the purpose of the page e.g. buying an item, subscribing to an email list, answering a survey.

Given this context on buttons, let’s see what we can do to make our buttons more appealing:

  1. Size: To have a button clickable on a mobile device we should have an area of at least 40×40 px.
  2. Box-shadow: We can add a box-shadow to give a depth effect to the button.
  3. Min-Width: When the text is too short in a button, we can add a min-width to have a minimum size for the button. Also, remember to use textAlign to center the text.
  4. Hover: Hover make your buttons interactive. When you change the color of a button on mouse hover, you are able to grab the attention of the user easily.
  5. Transitions: Transitions add that little extra touch signifying that you are a pro, by adding a transition of colors
  6. Round Corners: Adding a bit of round corners will always make your buttons look more welcoming (check if this aligns with your brand guidelines though).
  7. Line-height: Are you tired of trying to vertically center the text in a button with padding? You should definitely try line-height. Line-height will set the space between lines but in the case of a single line and an element with display: block or inline-block will be the height of the element.

Let’s take a look at all of it together:

CSS Trick #4 - Perfect your Buttons

CSS Trick #5 – Button Bars

CSS Trick #5 - Button Bars

Button bars are bars where we group buttons with different actions. Essentially, they are critical for interaction. These bars contain multiple buttons and most of the times, we aren’t sure about the total number of buttons or size of each button. It is critical to be as flexible as possible while writing your CSS so that you can have the most amount of variations possible.

Now, we need to find a way of making the buttons work independent of the alignment (center, left, right), number of buttons and size of each button. Planning your css ahead of execution is the key to pull off this kind of flexibility.

Solution:

To achieve the same spacing between the bar and the buttons, you need to first understand that the space between the buttons is doubled. This is because the right margin, 5px, of the first button adds up with the left margin, 5px, of the second button giving you a total of 10px which we eventually use for top and bottom. This is why you see that the left and right margins are 5px but top and bottom are 10px. 

Finally, there is an extra horizontal space added by the padding to add that extra 5px missing between the last button and the bar. The text align will now work no matter  which option you choose.

CSS Trick #6 – Fixed Header

Headers are one of the most common elements on any website or app. Hence, it is critical to handle them correctly. Having a sticky header (making it stay at the top of the page) is something most people desire and achieve by using “position: fixed”. The problem with “position: fixed” is that there is a need to make an empty space at the beginning of the page corresponding to the size of the header.

This has several issues like when the page resizes, the height of the header might change. When this happens, you will need to adjust the space whenever the height of the header changes. Also, part of the scrollbar might be covered by the header, but this can be easily solved by using flex.

Solution:

What is important when you use this technique is that the element you want to be scrollable contains the property “flex: 1” and its overflow is set to auto. Also, the container of both the header and content should have its height defined and overflow set to hidden.

CSS Trick #6 - Fixed Header

CSS Trick #7 – Center Content

In today’s world, it is extremely important that our content runs properly on multiple devices with multiple resolutions. This can be extremely difficult to achieve at times and gets very complex when you involve media queries using the same html to create a completely different layout. 

We are going to start with the most simple scenario which is resizing the content of the page according to the size of the browser. We are also going to assume the content inside a div is text.

We don’t want our page to go extremely wide as it can become very difficult to read when each line is say, 1500px. So, we are going to limit how wide our content can be. Finally, since we are limiting the width of our content, we want to center it horizontally over the screen since it won’t cover the entire width on higher resolution.

Solution:

There are 3 crucial properties we will use here:

  • Max-width: Limits the size of our page to a maximum width
  • Margin: “0 auto” – This helps us have the same left and right margins, which will center our div horizontally with “class=page”
  • Padding: Helps us to have some space between the screen and our content
CSS Trick #7 - Center Content

CSS Trick #8 – Resize Image

Once we have dealt with resizing our page, we need to be able to resize our images. Images are special because you don’t want the image to be scaling up. If have a low resolution image, it should be kept small so that it doesn’t look blurry.

This time, instead of trying to limit the size to a certain amount of pixels, we would want the maximum size to be the original size. Meaning, if the image is 300px wide, the maximum width should be 300px.

Solution:

The solution is quite similar to the previous example, but with a key difference. The trick relies on the fact that on images, we define the maximum width as percentage. We will set max width to a certain percentage of the screen width. 

Let’s consider 2 scenarios where our image is 500px wide and max-width is set as 100%: 

1. Screen is bigger than the image

If the screen (or parent element) is 1000px wide, we max width turns out to be 1000px. Note that since the width of the image is just 500px, it will actually display as 500px.

2. Image is bigger than the screen

If the screen is 320px, the max-width is 100%, which is set as 320px. The image in this case will scale down to 320px wide (maintaining the original aspect ratio).

This creates an effect of the image resizing down on smaller screens but having its original resolution on wider screens.

CSS Trick #9 – Tooltips

Tooltip is a simple way of giving more information to the user when he/ she hover an element. This becomes necessary because sometimes there is much more information that can fit into the screen. Some of these tooltips will actually open on onClick rather than on mouseOver which allows them to work on mobile devices as well.

One of the biggest issues with tooltips is positioning since most of them need to resize according to the content. So we need something that is flexible and which position its depending to the position of the element you want to place the tooltip over.

Solution:

The solution relies on positioning the elements correctly and playing with transform to make adjustments depending on the content. As an example, we will try to emulate the tooltip of engagement info on instagram shown below:

CSS Trick #9 - Tooltips

The first thing you would have noticed is that the position of the tooltip is absolute. We use bottom and left to align the tooltip to the button. However, it still isn’t centered. To complete the trick, we must use transform. Transforms are calculated based on the size of the node to which the transform belongs.

In this example, we have also created an element from css that will render in html. For that, we have used :after and set the content as empty. Finally, we have used borders to create a triangle.

CSS Trick #10 – Zoom Effect

CSS is all about giving final touches to your project. If you have invested your time and effort in building a product but missed to give those extra touches, the perceived value of your entire product falls down. That’s why making your app look great is not only essential for visual appeal but also, in a saturated/ competitive market, it might be the difference between you and your competition. So, let’s give a cool touch to your project by using animations.

Avatars are used in a lot of places. Let’s create something with an effect on top of it. When we hover over an avatar, we want to make a light zoom over the image. This will make your app look great.

Solution:

The solution is extremely simple. First, we need to create our avatar using an image tag since we will be scaling the image using transform and transition to perform the animation. This trick relies on one important fact. When you use transform, only that element is affected. We will not change the position of the rest of the elements on the screen in anyway. This is also a great way of creating effects without worrying about how the rest of the elements will flow since they won’t be affected at all.

CSS Trick #10 - Zoom Effect

I hope you enjoyed the CSS tricks we discussed and that they will help you make your page look better and coding life easier.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Receive insights and detailed articles across technology, design, remote working and freelancing.

You May Also Like